canvas教程

Android动画之渐变动画(Tween Animation)详解 (渐变、缩放、位移(2)

字号+ 作者:H5之家 来源:H5之家 2016-04-30 14:00 我要评论( )

//初始化 Translate动画translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);//初始化 Alpha动画alphaAnimation = new AlphaAnimation(0.1f, 1.0f);//动画集AnimationSet set = new AnimationS

//初始化 Translate动画 translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f); //初始化 Alpha动画 alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //动画集 AnimationSet set = new AnimationSet(true); set.addAnimation(translateAnimation); set.addAnimation(alphaAnimation); //设置动画时间 (作用到每个动画) set.setDuration(1000); this.startAnimation(set);

是不是觉得很简单呢?

附上整个View类的代码吧。

package com.yfz.view; import com.yfz.R; import android.content.Context; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; public class TweenAnim extends View { //Alpha动画 - 渐变透明度 private Animation alphaAnimation = null; //Sacle动画 - 渐变尺寸缩放 private Animation scaleAnimation = null; //Translate动画 - 位置移动 private Animation translateAnimation = null; //Rotate动画 - 画面旋转 private Animation rotateAnimation = null; public TweenAnim(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Log.e("Tween", "onDraw"); //加载一个图片 canvas.drawBitmap(((BitmapDrawable)getResources().getDrawable(R.drawable.gallery_photo_5)).getBitmap(), 0, 0, null); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Log.e("Tween", "onKeyDown"); return true; } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Log.e("Tween", "onKeyDown"); switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: Log.e("Tween", "onKeyDown - KEYCODE_DPAD_UP"); alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //设置动画时间 alphaAnimation.setDuration(3000); this.startAnimation(alphaAnimation); break; case KeyEvent.KEYCODE_DPAD_DOWN: Log.e("Tween", "onKeyDown - KEYCODE_DPAD_DOWN"); rotateAnimation = new RotateAnimation(0f, 360f); rotateAnimation.setDuration(1000); this.startAnimation(rotateAnimation); break; case KeyEvent.KEYCODE_DPAD_LEFT: Log.e("Tween", "onKeyDown - KEYCODE_DPAD_LEFT"); //初始化 scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f); //设置动画时间 scaleAnimation.setDuration(500); this.startAnimation(scaleAnimation); break; case KeyEvent.KEYCODE_DPAD_RIGHT: Log.e("Tween", "onKeyDown - KEYCODE_DPAD_RIGHT"); //初始化 translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f); //设置动画时间 translateAnimation.setDuration(1000); this.startAnimation(translateAnimation); break; case KeyEvent.KEYCODE_DPAD_CENTER: Log.e("Tween", "onKeyDown - KEYCODE_DPAD_CENTER"); //初始化 Translate动画 translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f); //初始化 Alpha动画 alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //动画集 AnimationSet set = new AnimationSet(true); set.addAnimation(translateAnimation); set.addAnimation(alphaAnimation); //设置动画时间 (作用到每个动画) set.setDuration(1000); this.startAnimation(set); break; default: break; } return true; } }

在Activity中调用该类时,需要注意一定setFocusable(true), 否则焦点在Activity上的话,onKeyUp方法是不会生效的。

调用该View的代码:

TweenAnim anim = new TweenAnim(Lesson_11.this); anim.setFocusable(true); setContentView(anim);

上面通过Java代码,实现了4中不同的Tween动画,其实在Android中完全可以通过 XML文件来实现动画。这种方式可能更加简洁、清晰,也更利于重用。
下面我们分别对这几种动画改用xml来实现。

首先是AlphaAnimation。

alpha_anim.xml:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="2000" /> </set>

不用解释了吧。

RotateAnimation

rotate_anim.xml:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="500" /> </set>

ScaleAnimation

scale_anim.xml:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android游戏开发学习②焰火绽放效果实现方法

    Android游戏开发学习②焰火绽放效果实现方法

    2016-05-08 10:00

  • 13问13答全面学习Android View绘制

    13问13答全面学习Android View绘制

    2016-05-07 17:00

网友点评
h