标签:android http io ar os sp java on 文件
将Animation设置为ImageView的backgrond即可
MainActivity.java:
import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { Button start, stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.button1); stop = (Button) findViewById(R.id.button2); ImageView img = (ImageView) findViewById(R.id.imageView1); final AnimationDrawable ani = (AnimationDrawable) img.getBackground(); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ani.start(); //开启动画播放 } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ani.stop(); } }); } }
对应的xml布局文件 activity_main.xml :
<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" tools:context="${relativePackage}.${activityClass}" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@anim/myanimation" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="30dp" android:layout_marginLeft="51dp" android:text="开始" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="58dp" android:layout_toRightOf="@+id/button1" android:text="暂停" /> </RelativeLayout>
自定义的逐帧动画资源 myanimation.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/mm1" android:duration="1000"/> <item android:drawable="@drawable/mm2" android:duration="1000"/> <item android:drawable="@drawable/mm3" android:duration="1000"/> <item android:drawable="@drawable/mm4" android:duration="1000"/> <item android:drawable="@drawable/mm5" android:duration="1000"/> </animation-list>
标签:android http io ar os sp java on 文件
原文地址:http://my.oschina.net/u/1413857/blog/352423