• Android---63---Android中的动画效果


    Android中有四种动画效果:

    AlphaAnimation:透明度动画效果

    ScaleAnimation:缩放动画效果

    TranslateAnimation:位移动画效果

    RotateAnimation:旋转动画效果

    1.透明动画效果

    public class MainActivity extends Activity {
    
    	private AlphaAnimation aa;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		aa = new AlphaAnimation(1, 0);
    		aa.setDuration(3000);
    		// 设置监听事件
    		aa.setAnimationListener(new AnimationListener() {
    
    			@Override
    			public void onAnimationStart(Animation animation) {
    				Toast.makeText(MainActivity.this, "start", 0).show();
    			}
    
    			@Override
    			public void onAnimationRepeat(Animation animation) {
    				Toast.makeText(MainActivity.this, "repeat", 0).show();
    			}
    
    			@Override
    			public void onAnimationEnd(Animation animation) {
    				Toast.makeText(MainActivity.this, "end", 0).show();
    			}
    		});
    		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				v.startAnimation(aa);
    			}
    		});
    
    	}
    }


     

    通过xml配置文件 完毕该动画效果

    创建xml文件:右键res->选择android xml file

    创建完毕之后会在res文件夹下生成一个anim文件夹

    aa.xml:

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


     

    Activity中仅仅须要 声明 AlphaAnimation 并在Button的监听事件中 调用

    v.setAnimation (AnimationUtils.loadAnimation(MainActivity.this,R.anim.aa));


     

    效果是一样的。

    2. 缩放动画效果

    public class MainActivity extends Activity {
    
    	private ScaleAnimation sa;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		sa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f,
    				Animation.RELATIVE_TO_SELF, 0.5f);
    		sa.setDuration(2000);
    		
    		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				v.startAnimation(sa);
    			}
    		});
    	}
    }


     

    通过xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" >
    
    </scale>


     

    3. 位移动画效果

    public class MainActivity extends Activity {
    
    	private TranslateAnimation ta;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		ta = new TranslateAnimation(0, 200, 0, 200);
    		ta.setDuration(2000);
    
    		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				v.startAnimation(ta);
    				// v.startAnimation(AnimationUtils.loadAnimation(
    				// MainActivity.this, R.anim.ta));
    			}
    		});
    	}
    }


     

    xml:

    <?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="200"
        android:toYDelta="200" >
    
    </translate>


     

    4. 旋转动画效果

    public class MainActivity extends Activity {
    
    	private RotateAnimation ra;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		// ra = new RotateAnimation(0, 360);
    		// ra = new RotateAnimation(0, 360, 50, 150);
    		ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
    				Animation.RELATIVE_TO_SELF, 0.5f);
    		ra.setDuration(2000);
    
    		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				v.startAnimation(ra);
    			}
    		});
    	}
    }


     

    动画混合:

    AnimationSet

    public class MainActivity extends Activity {
    
    	AnimationSet as ;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		as = new AnimationSet(true);
    		as.setDuration(2000);
    		
    		AlphaAnimation aa = new AlphaAnimation(0, 1);
    		aa.setDuration(2000);
    		
    		TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 200);
    		ta.setDuration(2000);
    		
    		
    		as.addAnimation(ta);
    		as.addAnimation(aa);
    		
    		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				v.startAnimation(as);
    			}
    		});
    	}
    }
    


     

    创建Xml文件选择set:

    <?xml version="1.0" encoding="utf-8"?

    > <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:shareInterpolator="true" > <alpha android:fromAlpha="0" android:toAlpha="1" /> <translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200" android:toYDelta="200" /> </set>


     

  • 相关阅读:
    2009年放假时间安排
    省钱方便网上手机充话费
    为啥不能嵌入html?
    超出套餐流量的GPRS流量费竟然要贵100倍!怎么没有人管呢!这个价格怎么定的呢!
    2008汶川加油!2008中国加油!!
    thinkpad X200 破音特别厉害!郁闷啊!千万不要买水货!
    送走2008,迎接新的2009!
    "上海启明星电子商务有限公司"偷偷扣你的电话钱
    从公司到凯虹
    供应二级新疆细绒棉150吨
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6816080.html
Copyright © 2020-2023  润新知