樣式,主題和動畫

樣式---------定義在values種的style.xml裏,和主題不同,樣式的作用範圍主要是在控件上,旨在抽出不同佈局裏不同控件相同風格的一組屬性。


主題-------主要是寫在清單文件裏,有基於應用的,也有基於Activity的,包括一些背景色和是否全屏等等,具體可以看系統SDK的platforms下的value文件夾下。


動畫分爲幾種


幀動畫,首先創建一個xml文件gril.xml,添加節點

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"
    >
    <item
        android:drawable="@drawable/girl_1"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_2"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_3"
        android:duration="200"/>


</animation-list>
oneshot 設置成true就是隻播放一次,false循環播放



activity裏代碼如下

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.iv);
		// 把xml文件的動畫資源設置爲iv背景
		iv.setBackgroundResource(R.drawable.girl);
		// 獲取設置的動畫資源。 執行可能需要花費一定的時間
		mAnimationDrawable = (AnimationDrawable) iv.getBackground();
		
	}

	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			mAnimationDrawable.start();
			return true;
		}
		return super.onTouchEvent(event);
	}


補間動畫

旋轉,位移,縮放,透明度動畫



//透明度動畫
	public void alpha(View view){
		AlphaAnimation  aa = new AlphaAnimation(0.0f, 1.0f);      //完全透明-》完全不透明
		aa.setDuration(2000);  //播放時間
		aa.setRepeatCount(1);  //重複次數1,共播放2次,-1代表無限次,永遠不停
		aa.setRepeatMode(Animation.REVERSE);   //重複類型,逆序
		aa.setFillAfter(true);  //是否還原
		iv.startAnimation(aa); 
	}
	//位移動畫
	public void trans(View view){
		TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, 
				Animation.RELATIVE_TO_PARENT, 0.5f, 
				Animation.RELATIVE_TO_PARENT, 0.0f, 
				Animation.RELATIVE_TO_PARENT, 0.0f);  //開始類型(相對父親或相對自己),x軸開始點,x軸結束點
		ta.setDuration(2000);
		ta.setRepeatCount(1);
		ta.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(ta);
	}


	//縮放動畫
	public void scale(View view){
		ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);    //開始x,縮放後x,開始y,縮放後y,相對自己,自己中心點,相對自己,0.5y就是自己的中心點
		sa.setDuration(2000);
		sa.setRepeatCount(1);
		sa.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(sa);
	}
	
	//旋轉動畫
	public void rotate(View view){
		RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 0.0f);   //相對於自己的左上角轉360度
		ra.setDuration(2000);
		ra.setRepeatCount(1);
		ra.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(ra);
	}



	//動畫組合
	public void set(View view){
		AnimationSet set = new AnimationSet(false);
		TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, 
				Animation.RELATIVE_TO_PARENT, 0.5f, 
				Animation.RELATIVE_TO_PARENT, -0.5f, 
				Animation.RELATIVE_TO_PARENT, 0.5f);
		ta.setDuration(2000);
		ta.setRepeatCount(1);
		ta.setRepeatMode(Animation.REVERSE);
		ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		sa.setDuration(2000);
		sa.setRepeatCount(1);
		sa.setRepeatMode(Animation.REVERSE);
		RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		ra.setDuration(2000);
		ra.setRepeatCount(1);
		ra.setRepeatMode(Animation.REVERSE);
		set.addAnimation(ra);
		//set.addAnimation(ta);
		set.addAnimation(sa);
		iv.startAnimation(set);
	}

組合的時候就放在set集合裏,開始的false代表自顧自得速度。

代碼也可以用xml文件來定義,

這樣就可以直接用AnimationUtils.loadAnimation(Context,R.anim.xx);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章