需求
想做一個(gè)動(dòng)畫忌卤,一個(gè)會(huì)跑的小人扫夜,從屏幕右側(cè)跑道右側(cè),于是做了個(gè)嘗試驰徊,上圖:
實(shí)現(xiàn)步驟
要完成這樣需要三步:
- 做一個(gè) 幀動(dòng)畫 (frame animation)笤闯,由多張圖片組成,組成小人連續(xù)跑動(dòng)的樣子棍厂。
- 做一個(gè) 位移動(dòng)畫 使得小人 從左到右產(chǎn)生移動(dòng)颗味。
- 在onStart里啟動(dòng)動(dòng)畫
第一步,描述 “人物動(dòng)作的變化”的動(dòng)畫
準(zhǔn)備多個(gè)動(dòng)作的圖片牺弹,寫個(gè)xml animation :
<?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/loading_iv_00"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_01"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_02"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_03"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_04"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_05"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_06"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_07"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_08"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_09"
android:duration="60">
</item>
<item
android:drawable="@drawable/loading_iv_10"
android:duration="60">
</item>
</animation-list>
代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageResource(R.anim.loading);
imageView1.setVisibility(View.GONE);
mAnimationDrawable = (AnimationDrawable) imageView1.getDrawable();
mAnimationDrawable.setOneShot(false);
}
第二步浦马,位移動(dòng)畫
代碼:
Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.2f,
Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
translate.setDuration(3000);
translate.setRepeatCount(Animation.INFINITE);
這句話的意思時(shí),相對(duì)于 父容器 的x坐標(biāo)移動(dòng)张漂,y軸不改變晶默,一直循環(huán)
第三步,啟動(dòng)
啟動(dòng)動(dòng)畫即可航攒,代碼:
package com.example.demo_run;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imageView1;
AnimationDrawable mAnimationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageResource(R.anim.loading);
imageView1.setVisibility(View.GONE);
mAnimationDrawable = (AnimationDrawable) imageView1.getDrawable();
mAnimationDrawable.setOneShot(false);
}
@Override
protected void onStart() {
startAnimation();
super.onStart();
}
private void startAnimation() {
mAnimationDrawable.start();
Animation translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.2f,
Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
translate.setDuration(3000);
translate.setRepeatCount(Animation.INFINITE);
imageView1.startAnimation(translate);
imageView1.setVisibility(View.VISIBLE);
}
}
頁(yè)面布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3F99C3"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>