`
布衣Sao年
  • 浏览: 7724 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

android动画总结

 
阅读更多

目前android有三种动画类型:tween动画、frame动画和property动画

1、tween动画:

AlphaAnimation
渐变透明度动画效果
ScaleAnimation
渐变尺寸伸缩动画效果
TranslateAnimation
画面转换位置移动动画效果
RotateAnimation
画面转移旋转动画效果
可以在工程下res文件夹->anim文件夹(需要自己新建)下新建xml文件进行定义动画比如:
<span style="font-family:KaiTi_GB2312;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <translate />
    <alpha/>
    <scale/>
    <rotate/>
</set></span>
再在java文件中调用Animation animation = AnimationUtils.loadAnimation(context,resid)方法获取到。
当然也可以在ja件中动态实例化对应的动画:
AlphaAnimation animation_Alpha= new AlphaAnimation(0.1f, 1.0f);

2、frame动画

可以将XML文件放在/res/drawable/目录下
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/pic_state1" android:duration="200" />
    <item android:drawable="@drawable/pic_state2" android:duration="200" />
    <item android:drawable="@drawable/pic_state3" android:duration="200" />
</animation-list>
然后在java文件中获取到:
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setBackgroundResource(R.drawable.drawable_anim);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
再用AnimationDrawable的start和stop方法控制动画开始和停止。
需要注意几个问题:
  1. 要在代码中调用Imageview的setBackgroundResource方法,如果直接在XML布局文件中设置其src属性当触发动画时会出问题。

  2. SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start(),或者利用handler.post()方法。

3、property动画

字面意思理解是属性动画,也就是说在执行动画的过程中可以改变当前执行动画对象的某个属性来控制动画的执行。这个动画在android3.0以后出来的,弥补了tween动画的某些缺陷,可以实现类似3D的旋转等等。
我实现了一个按钮的移动,当然按钮的监听会跟着按钮一起移动,而用tween动画移动后按钮监听还是在按钮的初始位置
上代码:
/**
 * teen动画和property动画的实例
 * @author hand
 *
 */
public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	/**
	 * 用tween动画移动按钮,点击事件只发生在按钮原来的位置
	 * @param v
	 */
	public void clickBtn1(View v){
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_x);
		findViewById(R.id.tween_animation_btn).startAnimation(animation);
	}
	/**
	 * 用property动画移动按钮,点击事件跟随按钮位置变化而变化
	 * @param v
	 */
	@SuppressLint("NewApi")
	public void clickBtn2(View v){
		Button btn = (Button)findViewById(R.id.property_animation_btn);
		Animator animator = AnimatorInflater.loadAnimator(this,R.animator.translate2);
		animator.setTarget(btn);
		animator.start();
	}

}
其中R.animator.translate2在res/animator(自己新建一个)目录下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <objectAnimator 
        android:duration="2000"
        android:propertyName="x"
        android:valueFrom="0"
        android:valueTo="450">
    </objectAnimator>

</set>
activity_main.xml文件如下;
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.hd.property_animation_demo.MainActivity"
    tools:ignore="MergeRootFrame" >

    <Button 
        android:id="@+id/tween_animation_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tween动画"
        android:onClick="clickBtn1"/>
    <Button 
        android:id="@+id/property_animation_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="property动画"
        android:onClick="clickBtn2"/>
</RelativeLayout>

以上主要是property_animation_btn实现了点击自己移动,而上面的tween_animation_btn的按钮是用tween动画实现的移动,大家可以试试,两者区别明显:
图片不能上传了,就简单说一下:用tween动画实现移动以后,按钮停在动画结束为止,可以按钮监听停留在原位置没有随着按钮移动而移动;而property动画按钮监听随着按钮移动而移动到了动画结束的位置。
源码很简单,关键理解property动画的应用




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics