AnimationDrawable類:幀動畫類

來源:互聯網
上載者:User

個人呢寫的比較的簡單,只是為了練習使用

代碼如下

java代碼

package mars.com;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;import android.widget.Toast;public class Demo_Activity extends Activity {private Button button1;private Button button2;private ImageView image;private AnimationDrawable ad;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);image = (ImageView) findViewById(R.id.image);ad = (AnimationDrawable) image.getBackground();// 聲明幀動畫對象button1.setOnClickListener(new OnClickListener() {public void onClick(View v) {ad.addFrame(getResources().getDrawable(R.drawable.p0), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p1), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p2), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p3), 100);// 添加幀ad.addFrame(getResources().getDrawable(R.drawable.p4), 100);// 添加幀ad.setAlpha(100);// 設定透明度ad.setOneShot(true);// 設定是否迴圈播放ad.start();// 開始Toast.makeText(Demo_Activity.this,"幀數:" + ad.getNumberOfFrames(), Toast.LENGTH_SHORT);// 擷取幀數}});button2.setOnClickListener(new OnClickListener() {public void onClick(View v) {ad.stop();// 結束}});}}

main.xml代碼如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="執行動畫" />    <Button        android:id="@+id/button2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="停止動畫" />    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@anim/anim" /></LinearLayout>

設定檔anim.anim.xml如下,實際上我並不知道xml代碼是怎麼寫的,不太懂,有人懂的話留個言,謝謝

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="true" >    <item        android:drawable="@drawable/p0"        android:duration="50">    </item>    <item        android:drawable="@drawable/p1"        android:duration="50">    </item>    <item        android:drawable="@drawable/p2"        android:duration="50">    </item>    <item        android:drawable="@drawable/p3"        android:duration="50">    </item>    <item        android:drawable="@drawable/p4"        android:duration="50">    </item></animation-list>

OK,然後把自己弄好的圖片放在自己的drawable檔案夾下就行了,這裡就不傳圖片了,怪麻煩的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.