您好,登錄后才能下訂單哦!
前置條件:
1.首先在res/drawable目錄下放入幾張圖片(注意圖片不能用阿拉伯?dāng)?shù)字命名這里以one,two,three,four,否則在R.java文件中會報錯)。
2.同樣在res/drawable目錄下建立animation1.xml文件,文件主要用來控制幀動畫的循環(huán)次數(shù):
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>
放在節(jié)點<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
//設(shè)置資源文件獲取的來源
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;
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。