android图片推拉门动画效果
2014-03-26 13:52
302 查看
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;
public class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
private final boolean mReverse;//翻转标志
private Camera mCamera;
/**
* 初始化方法
* @param fromDegrees 开始的旋转角度
* @param toDegrees 结束的旋转角度
* @param centerX 中心X坐标
* @param centerY 中心Y坐标
* @param depthZ Z轴
* @param reverse 翻转状态的标识 为true时表示从0—90度翻转,false表示从90-0度翻转
*/
public Rotate3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
//三轴旋转
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
//绕Y轴旋转
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
如何使用:
import android.app.Activity;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class Rotate3dActivity extends Activity {
private FrameLayout mContainer;
private ImageView imageView1;
private ImageView imageView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 测试用的view
*/
mContainer = new FrameLayout(this);
mContainer.setBackgroundColor(Color.BLACK);
imageView1=new ImageView(this);
imageView1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView1.setId(20);
imageView1.setImageResource(R.drawable.back);
//第1个图片的按键监听
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView1,mContainer);
}
});
mContainer.addView(imageView1);
imageView2 =new ImageView(this);
imageView2.setId(30);
imageView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView2.setImageResource(R.drawable.front);
//第2个图片的按键监听
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView2,mContainer);
}
});
mContainer.addView(imageView2);
setContentView(mContainer);
}
public void startRotation3D(float start,float end,final View rotateView,final View containerView){
//获得中心点
final float centerX=containerView.getWidth()/2.0f;
final float centerY =containerView.getHeight() / 2.0f;
//开始翻转,将当前界面从0°翻转到90°
Rotate3dAnimation rotation=new Rotate3dAnimation(start, end, centerX, centerY, 200.0f, true);
rotation.setDuration(500);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
containerView.post(new Runnable() {
public void run() {
//判断哪个图片需要隐藏
Log.e("sC", String.valueOf(rotateView.getId()));
if(rotateView.getId() == 20){
imageView1.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
}else if (rotateView.getId() == 30) {
imageView2.setVisibility(View.GONE);
imageView1.setVisibility(View.VISIBLE);
}
//第二次翻转,将新页面从90°翻转到0°
Rotate3dAnimation rotatiomAnimation = new Rotate3dAnimation(-90, 0, centerX, centerY, 200.0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation.setInterpolator(new DecelerateInterpolator());
containerView.startAnimation(rotatiomAnimation);
}
});
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationStart(Animation arg0) {
}
});
containerView.startAnimation(rotation);
}
}
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;
public class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
private final boolean mReverse;//翻转标志
private Camera mCamera;
/**
* 初始化方法
* @param fromDegrees 开始的旋转角度
* @param toDegrees 结束的旋转角度
* @param centerX 中心X坐标
* @param centerY 中心Y坐标
* @param depthZ Z轴
* @param reverse 翻转状态的标识 为true时表示从0—90度翻转,false表示从90-0度翻转
*/
public Rotate3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
if (mReverse) {
//三轴旋转
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
//绕Y轴旋转
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
如何使用:
import android.app.Activity;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class Rotate3dActivity extends Activity {
private FrameLayout mContainer;
private ImageView imageView1;
private ImageView imageView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 测试用的view
*/
mContainer = new FrameLayout(this);
mContainer.setBackgroundColor(Color.BLACK);
imageView1=new ImageView(this);
imageView1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView1.setId(20);
imageView1.setImageResource(R.drawable.back);
//第1个图片的按键监听
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView1,mContainer);
}
});
mContainer.addView(imageView1);
imageView2 =new ImageView(this);
imageView2.setId(30);
imageView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
imageView2.setImageResource(R.drawable.front);
//第2个图片的按键监听
imageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startRotation3D(0, 90, imageView2,mContainer);
}
});
mContainer.addView(imageView2);
setContentView(mContainer);
}
public void startRotation3D(float start,float end,final View rotateView,final View containerView){
//获得中心点
final float centerX=containerView.getWidth()/2.0f;
final float centerY =containerView.getHeight() / 2.0f;
//开始翻转,将当前界面从0°翻转到90°
Rotate3dAnimation rotation=new Rotate3dAnimation(start, end, centerX, centerY, 200.0f, true);
rotation.setDuration(500);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
containerView.post(new Runnable() {
public void run() {
//判断哪个图片需要隐藏
Log.e("sC", String.valueOf(rotateView.getId()));
if(rotateView.getId() == 20){
imageView1.setVisibility(View.GONE);
imageView2.setVisibility(View.VISIBLE);
}else if (rotateView.getId() == 30) {
imageView2.setVisibility(View.GONE);
imageView1.setVisibility(View.VISIBLE);
}
//第二次翻转,将新页面从90°翻转到0°
Rotate3dAnimation rotatiomAnimation = new Rotate3dAnimation(-90, 0, centerX, centerY, 200.0f, false);
rotatiomAnimation.setDuration(500);
rotatiomAnimation.setInterpolator(new DecelerateInterpolator());
containerView.startAnimation(rotatiomAnimation);
}
});
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationStart(Animation arg0) {
}
});
containerView.startAnimation(rotation);
}
}
相关文章推荐
- Android实现图片点击预览效果(zoom动画)
- android中类似于gif 实现图片的动画效果
- Android用两张图片实现简单动画效果
- Android浏览图片,点击放大至全屏效果(包含点击前位置到全屏的动画)
- [Android实例] app引导页(背景图片切换加各个页面动画效果)(申明:来源于网络)
- Android中如何使用rotate实现图片不停旋转的效果与动画的停止
- android图片渐隐渐现动画效果
- android图片切换ImageSwichter的动画切换效果
- 基于jQuery实现图片推拉门动画效果的两种方法
- 剖析Android动画(图片闪烁、左右摇摆、上下晃动等效果) .
- Android app引导页(背景图片切换加各个页面动画效果)
- [Android]ScaleViewPager--仿“想去”客户端图片展示效果--中轴旋转ViewPager动画实现
- Android动画(图片闪烁、左右摇摆、上下晃动等效果)
- android图片切换ImageSwichter的动画切换效果
- android app引导页(背景图片切换加各个页面动画效果)
- [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- Android 动画--> 图片切换效果
- Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- Android之ViewFlipper实现手动+自动切换图片(附加动画效果)
- Android 仿美团网,大众点评购买框悬浮效果,仿美团详情页,可下拉放大图片,向上滚动图片,松手有动画