前置条件:
1.首先在res/drawable目录下放入几张图片(注意图片不能用阿拉伯数字命名这里以one,two,three,four,否则在R.java文件中会报错)。
2.同样在res/drawable目录下建立animation1.xml文件,文件主要用来控制帧动画的循环次数:
android:oneshot="false"
另外还有控制播放的图片:
<item android:drawable="@drawable/one" android:duration="150"></item>
<item android:drawable="@drawable/two" android:duration="150"></item>
<item android:drawable="@drawable/three" android:duration="150"></item>
<item android:drawable="@drawable/four" android:duration="150"></item>
放在节点<animation-list> </animation-list>的中间
3.main.xml中是对界面的布局,包括p_w_picpathview,和两个button
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/p_w_picpathView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:src="@drawable/four"
android:contentDescription="帧动画图片"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearlayout1"
android:orientation="horizontal"
>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/p_w_picpathView1"
android:layout_below="@+id/p_w_picpathView1"
android:layout_marginTop="34dp"
android:text="@string/Begin" />
<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_alignParentRight="true"
android:text="@string/End" />
</RelativeLayout>
4.接下来就是对mainactivity.java的编写
package com.example.bujiandonghua;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView p_w_picpathView=(ImageView)findViewById(R.id.p_w_picpathView1);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//设置资源文件获取的来源
p_w_picpathView.setImageResource(R.drawable.animation1);
// // 获取AnimationDrawable对象
animationDrawable=(AnimationDrawable)p_w_picpathView.getDrawable();
animationDrawable.start();
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
p_w_picpathView.setImageResource(R.drawable.animation1);
animationDrawable=(AnimationDrawable)p_w_picpathView.getDrawable();
animationDrawable.stop();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。