android 動畫--幀動畫--仿美團載入中小人
阿新 • • 發佈:2018-11-13
1 把資源圖片放到drawable中
2 在drawable中寫動畫的xml檔案animation01
Animation01.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" //false為連續播放 true為只播放一次
>
<item android:drawable="@drawable/progress_loading_image_01" android:duration="100"></item> //播放的時間
<item android:drawable="@drawable/progress_loading_image_02" android:duration="100"></item>
<item android:drawable="@drawable/progress_loading_image_03" android:duration="100"></item>
<item android:drawable="@drawable/progress_loading_image_04" android:duration="100"></item>
</animation-list>
3佈局檔案中
<LinearLayout 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:orientation="vertical"
>
<Button
android:id="@+id/but_animationStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放遊遊動畫" />
<Button
android:id="@+id/but_animationEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止遊遊動畫" />
<ImageView
android:id="@+id/image_animation"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/animation01"/>
</LinearLayout>
4 程式碼中引入動畫
public class MainActivity extends Activity {
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button but_start=(Button) findViewById(R.id.but_animationStart);
Button but_end=(Button) findViewById(R.id.but_animationEnd);
final ImageView imageAnima=(ImageView) findViewById(R.id.image_animation);
but_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imageAnima.setImageResource(R.drawable.animation01);
animationDrawable = (AnimationDrawable) imageAnima.getDrawable();
animationDrawable.start();
imageAnima.setBackgroundColor(android.graphics.Color.parseColor("#00ffffff"));
}
});
but_end.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
animationDrawable = (AnimationDrawable) imageAnima.getDrawable();
animationDrawable.stop();
}
});
}
}