欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安卓动画相关 学习笔记

程序员文章站 2022-05-05 14:21:48
...

1.帧动画

把连续图片放入 drawable 目录 以及在该目录创建创建一个xml 文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:oneshot="false" >

    <item
        android:drawable="@drawable/a1001"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1002"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1003"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1004"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1005"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1001"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1006"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1007"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1008"
        android:duration="200"/>

    <item
        android:drawable="@drawable/a1009"
        android:duration="200"/>
    <item
        android:drawable="@drawable/a1010"
        android:duration="200"/>        
</animation-list>

程序片段

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.iv);
		iv.setBackgroundResource(R.drawable.gril_list);
		an = (AnimationDrawable) iv.getBackground();
	}
	
	public void play(View view) {
		an.start();
	}

##2. 程序创建动画

	//平移
	public void playTranslate(View view)
	{
		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f, 
				Animation.RELATIVE_TO_PARENT, 1.0f, 
				Animation.RELATIVE_TO_PARENT, 0.0f, 
				Animation.RELATIVE_TO_PARENT, 1.0f);
		ta.setDuration(2000);
		ta.setRepeatCount(2);
		ta.setRepeatMode(Animation.REVERSE);
		lanucher.startAnimation(ta);
	}
	
	//透明度
	public void alpha(View view)
	{
		AlphaAnimation an = new AlphaAnimation(0.0f, 1.0f);
		an.setDuration(2000);
		an.setRepeatCount(2);
		an.setRepeatMode(Animation.REVERSE);
		lanucher.startAnimation(an);
	}
	
	//缩放
	public void scale(View view)
	{
		ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, 
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f);
		sa.setDuration(2000);
		sa.setRepeatCount(2);
		sa.setRepeatMode(Animation.REVERSE);
		lanucher.startAnimation(sa);
	}
	
	//旋转
	public void rotate(View view)
	{
		RotateAnimation ra = new RotateAnimation(0, 360, 				
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f);
		ra.setDuration(2000);
		ra.setRepeatCount(2);
		ra.setRepeatMode(Animation.REVERSE);
		lanucher.startAnimation(ra);		
	}
	
	//动画集合
	public void set(View view)
	{
		AnimationSet set = new AnimationSet(false);
		
		RotateAnimation ra = new RotateAnimation(0, 360, 				
				Animation.RELATIVE_TO_SELF, 0.5f, 
				Animation.RELATIVE_TO_SELF, 0.5f);
		ra.setDuration(2000);
		ra.setRepeatCount(2);
		ra.setRepeatMode(Animation.REVERSE);
		
		TranslateAnimation ta = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f, 
				Animation.RELATIVE_TO_PARENT, 1.0f, 
				Animation.RELATIVE_TO_PARENT, 0.0f, 
				Animation.RELATIVE_TO_PARENT, 1.0f);
		ta.setDuration(2000);
		ta.setRepeatCount(2);
		ta.setRepeatMode(Animation.REVERSE);
		lanucher.startAnimation(ta);
		
		set.addAnimation(ra);
		set.addAnimation(ta);
		lanucher.startAnimation(set);
	}

##3. xml配置动画

  1. 透明度
<?xml version="1.0" encoding="utf-8"?>
<alpha  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0" 
    android:toAlpha="0.0"
    android:fillBefore="true"
    android:duration="2000"
    >
</alpha>
  1. 旋转
<?xml version="1.0" encoding="utf-8"?>
<rotate 
    android:fromDegrees="0" 
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"
    xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>
  1. 缩放
<?xml version="1.0" encoding="utf-8"?>
<scale 
    android:fromXScale="0.2" 
    android:toXScale="2.0" 
    android:fromYScale="0.2"
    android:toYScale="2.0"
    android:fillAfter="true"
    android:repeatCount="2"
    xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
  1. 平移
<?xml version="1.0" encoding="utf-8"?>
<translate 
    android:fromXDelta="20%"
    android:toXDelta="50%p"
    android:fromYDelta="20%"
    android:toYDelta="50%p"
    android:duration="2000"
    android:repeatCount="2"
    android:repeatMode="reverse"
    xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
  1. 集合
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale 
    android:fromXScale="0.2" 
    android:toXScale="2.0" 
    android:fromYScale="0.2"
    android:toYScale="2.0"
    android:fillAfter="true"
    android:repeatCount="2">
</scale>
<rotate 
    android:fromDegrees="0" 
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000">    
</rotate>
</set>

程序片段

	private ImageView xiv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xml_main);
		xiv = (ImageView) findViewById(R.id.xiv);
	}
	
	//透明度
	public void alpha(View view)
	{
		Animation am = AnimationUtils.loadAnimation(this, R.anim.alpha);
		xiv.startAnimation(am);
	}

转载于:https://my.oschina.net/u/729139/blog/480964