位移,也就是移動,可以幫助我們實現一個空間或者佈局的移動效果,在動畫裏面可謂是應用最多的一個吧。下面我們來仔細研究一下這個動畫。
實現過程依然有兩個,一個是自定義anim文件。如下:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0"
android:toYDelta="45%p"
android:fillAfter="true"
android:duration="2000">
</translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="45%p"
android:toYDelta="45%p"
android:toXDelta="45%p"
android:fillAfter="true"
android:duration="2000">
</translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="45%p"
android:toXDelta="0"
android:fromYDelta="45%p"
android:toYDelta="0"
android:fillAfter="true"
android:duration="2000">
</translate>
以上是三種效果的動畫文件,有需要的盆友們可以分別運行看看效果,屬性改的多了自然也就知道每個屬性分別代表什麼意思了。以下給出activity中的代碼,至於xml的代碼,我想只要接觸過的都會寫,很簡單,最下方會有界面圖片展示。public class TranslateAnimationDemoActivity extends Activity implements
OnClickListener, AnimationListener {
private ImageView mImageView;
private TranslateAnimation translateAnim1;
private TranslateAnimation translateAnim2;
private TranslateAnimation translateAnim3;
private int t = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.btn_translate).setOnClickListener(this);
mImageView = (ImageView) findViewById(R.id.imageview);
}
public void onClick(View v) {
startTranslateAnimationJavaCode();
// stertTranslateAnimationXml();
// android:fromXDelta="45%p" 動畫的初始x位置
// android:toXDelta="0" 動畫結束時x的位置
// android:fromYDelta="45%p" 動畫開始時y的位置 20% 相對於自身 20%p相對於屏幕
// android:toYDelta="0" 動畫結束時Y的位置
// android:fillAfter="true" 動畫播放後的位置
// android:duration="2000" 動畫播放時間
// 動畫設置監聽
// setListen();
}
private void stertTranslateAnimationXml() {
translateAnim1 = (TranslateAnimation) AnimationUtils.loadAnimation(
this, R.anim.todown);
mImageView.setAnimation(translateAnim1);
translateAnim2 = (TranslateAnimation) AnimationUtils.loadAnimation(
this, R.anim.toright);
// mImageView.setAnimation(translateAnim2);
translateAnim3 = (TranslateAnimation) AnimationUtils.loadAnimation(
this, R.anim.totop);
// mImageView.setAnimation(translateAnim3);
}
private void setListen() {
translateAnim1.setAnimationListener(this);
translateAnim2.setAnimationListener(this);
translateAnim3.setAnimationListener(this);
}
private void startTranslateAnimationJavaCode() {
// 動畫移動相當於自己大小的多少 3f 代表組件的3倍寬 或 高 移動
TranslateAnimation translateAnim = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
10f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 10f);
translateAnim.setDuration(2000);
mImageView.setAnimation(translateAnim);
}
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
if (t == 1) {
mImageView.startAnimation(translateAnim2);
t = 2;
} else if (t == 2) {
mImageView.startAnimation(translateAnim3);
t = 3;
} else if (t == 3) {
mImageView.startAnimation(translateAnim1);
t = 1;
} else {
Log.e("TranslateAnimationDemoActivity", "見鬼了");
}
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
}
圖片設置的有點小,還望廣大博友加以理解,當時現成看到的圖片就直接用了。
裏面涉及幾種效果,可以把註銷掉的內容還原,把原來的動畫效果註釋掉繼續運行,你會看到不一樣的結果。應該這裏面還涉及了動畫的監聽,可以監聽到動畫的開始,結束,以及重放。具體的在以後單獨說明。有不好之處還請見諒,以後會多多修改。
完畢!