安卓動畫基礎講解

weixin_33816946發表於2016-01-06


//逐幀動畫
/**
* 1.加入單張圖片
* 2.生成movie.xml整個圖片
* 3.程式碼中使用圖片movie.xml
*/
iv=(ImageView) findViewById(R.id.iv);
// iv.setImageResource(R.drawable.movie);//為iv載入六張圖片
// AnimationDrawable ad=(AnimationDrawable) iv.getDrawable();//得到圖片給動畫圖片
// ad.start();

DisplayMetrics dm = new DisplayMetrics();
getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;//得到螢幕寬度

//補間動畫,平移
//1.通過程式碼直接寫
// Animation translate=new TranslateAnimation(0, width, 0, 0);
// translate.setDuration(2000);
// translate.setFillAfter(true);//動畫完成後,留在原位

// translate.setRepeatCount(2);//重複2次
// translate.setStartOffset(1000);//等待1秒後再動

// iv.startAnimation(translate);

//2.呼叫xml檔案
Animation translate=AnimationUtils.loadAnimation(this, R.anim.push_to_right);
iv.startAnimation(translate);
//iv.setAnimation(translate);也行

//動畫監聽事件
translate.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
Intent intent=new Intent(MainActivity.this,SecondAty.class);
startActivity(intent);
finish();

}
});

}

 

// 旋轉動畫
// 角度從0到360,pivotXType取誰的X值(介面的?就自身這個圖片) pivotYType
// Animation rotator = new RotateAnimation(0, 360,
// RotateAnimation.RELATIVE_TO_SELF, 0.5f,
// RotateAnimation.RELATIVE_TO_SELF, 0.5f);
// rotator.setDuration(3000);
// rotator.setFillAfter(true);
// iv.startAnimation(rotator);

// 漸變
// Animation alpha = new AlphaAnimation(0, 0.9f);//從看不見到看到90%
// alpha.setDuration(3000);
// alpha.setFillAfter(true);
// iv.startAnimation(alpha);

// 拉伸
// fromX, toX, fromY, toY平移動畫的四個引數,x軸伸縮尺寸,y軸伸縮尺寸
//ScaleAnimation.RELATIVE_TO_SELF, 3f//X軸座標是自己圖片寬度的3倍
Animation scale = new ScaleAnimation(0, 0.3f, 0, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 3f,
ScaleAnimation.RELATIVE_TO_SELF, 3f);
scale.setDuration(3000);
scale.setFillAfter(true);
iv.startAnimation(scale);

相關文章