android旋转动画和平移动画详解,补充说一下如果制作gif动画放到csdn博客上
2014-07-13 12:38
441 查看
先上效果图:
我这里用的是GifCam来制作的gif动画,可以在http://download.csdn.net/detail/baidu_nod/7628461下载,
制作过程是先起一个模拟器,然后把GifCam的框拖到模拟器上面,点击Rec的new先,然后点击Rec,然后就save到本地成gif文件
这里做一个左右旋转,上下旋转,和左右移动的动画,先自己建立一个View的类,作为操作的对象:
左右旋转动画:
如果是上线旋转就把camera.rotateY(degrees)改成camera.rotateX(degrees)
如果是移动的话
然后主程序这样来调用:
我这里用的是GifCam来制作的gif动画,可以在http://download.csdn.net/detail/baidu_nod/7628461下载,
制作过程是先起一个模拟器,然后把GifCam的框拖到模拟器上面,点击Rec的new先,然后点击Rec,然后就save到本地成gif文件
这里做一个左右旋转,上下旋转,和左右移动的动画,先自己建立一个View的类,作为操作的对象:
public class MyView extends View { private Paint mPaint; int width = 0; int height = 0; public MyView(Context context, AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); mPaint.setStrokeWidth(5); mPaint.setColor(Color.RED); this.setBackgroundColor(Color.RED); width = context.getResources().getDimensionPixelSize(R.dimen.width); height = context.getResources().getDimensionPixelSize(R.dimen.height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //width 300 height 300 canvas.drawLine(0, 0, width, 0, mPaint); canvas.drawLine(width, 0, width, height, mPaint); canvas.drawLine(width, height, 0, height, mPaint); canvas.drawLine(0, height, 0, 0, mPaint); canvas.save(); } }
左右旋转动画:
public class RotateLeftRightAnimation 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; private InterpolatedTimeListener listener; public RotateLeftRightAnimation(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 static interface InterpolatedTimeListener { public void interpolatedTime(float interpolatedTime); } public void setInterpolatedTimeListener(InterpolatedTimeListener listener) { this.listener = listener; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { if (listener != null) { listener.interpolatedTime(interpolatedTime); } final float fromDegrees = mFromDegrees; float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); boolean overHalf = (interpolatedTime > 0.5f); if (overHalf) { degrees = degrees - 180; } 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)); } <span style="color:#ff0000;">camera.rotateY(degrees); //这个Y轴旋转就是左右旋转</span> camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY);//这两句的意思是把View移到原点后旋转完再移动到现在的位置 } }
如果是上线旋转就把camera.rotateY(degrees)改成camera.rotateX(degrees)
如果是移动的话
<span style="color:#330033;">public class MoveAnimation extends Animation { private Camera mCamera; private float mMoveDistance; private InterpolatedTimeListener listener; public MoveAnimation(float moveDistance) { mMoveDistance = moveDistance; } public static interface InterpolatedTimeListener { public void interpolatedTime(float interpolatedTime); } public void setInterpolatedTimeListener(InterpolatedTimeListener listener) { this.listener = listener; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { if (listener != null) { listener.interpolatedTime(interpolatedTime); } final Camera camera = mCamera; final Matrix matrix = t.getMatrix(); camera.save(); camera.getMatrix(matrix); camera.restore(); matrix.postTranslate(mMoveDistance, 0); } }</span>
然后主程序这样来调用:
final MyView myView = (MyView) findViewById(R.id.myview); Button btn = (Button) findViewById(R.id.btn_move); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MoveAnimation anim = new MoveAnimation(200); anim.setDuration(500); myView.startAnimation(anim); } }); Button btn_up_down_rotate = (Button) findViewById(R.id.btn_up_down_rotate); btn_up_down_rotate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RotateUpDownAnimation anim = new RotateUpDownAnimation(0, 180, v.getWidth() / 2, v.getHeight() / 2, 0, false); anim.setDuration(500); myView.startAnimation(anim); } }); Button btn_left_right_rotate = (Button) findViewById(R.id.btn_left_right_rotate); btn_left_right_rotate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RotateLeftRightAnimation anim = new RotateLeftRightAnimation(0, 180, v.getWidth() / 2, v.getHeight() / 2, 0, false); anim.setDuration(500); myView.startAnimation(anim); } });
相关文章推荐
- android旋转动画和平移动画具体解释,补充说一下假设制作gif动画放到csdn博客上
- Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里
- Android基础-简单的动画实现 *补间动画 *透明度渐变 *旋转动画 *位移动画 *如果需要几个效果整合到一起,需要将所有效果全部放到set里
- android动画基础--旋转移动平移缩放
- Android模仿iPhone View旋转刷新数据动画详解 推荐
- Android之ViewFlipper:平移、旋转、伸缩、翻页四种动画效果
- [置顶] 我的Android进阶之旅------>介绍一款集录制与剪辑为一体的屏幕GIF 动画制作工具 GifCam
- 【android】LayoutInflater.inflate方法的详解及xml根元素的布局参数不起作用的问题 - u011494050的专栏 - 博客频道 - CSDN.NET
- Android 动画之RotateAnimation应用详解(旋转动画效果 )(转载)
- Android动画之渐变动画(Tween Animation)详解 (渐变、缩放、位移、旋转)
- Android 通过Animate硬编码实现简单的平移、旋转、缩放及透明度动画过程
- android动画1-平移和旋转
- 我的Android进阶之旅------>介绍一款集录制与剪辑为一体的屏幕GIF 动画制作工具 GifCam
- Android动画复习(包括缩放动画、平移动画、旋转动画、透明度动画)
- Android仿饿了么加入购物车旋转控件自带闪转腾挪动画的按钮效果(实例详解)
- Android 补间动画,具体介绍了透明度、平移,旋转,缩放4种动画效果的实现,简单易懂
- 【Android 应用开发】OpenGL ES 2.0 -- 制作 3D 彩色旋转三角形 - 顶点着色器 片元着色器 使用详解
- 【Android 应用开发】OpenGL ES 2.0 -- 制作 3D 彩色旋转三角形 - 顶点着色器 片元着色器 使用详解
- Android模仿iPhone View旋转刷新数据动画详解
- Android动画-旋转、平移、放缩和渐变