android之幀動畫實現

來源:互聯網
上載者:User

         今天實現了一個幀動畫的例子,

       首先在res/anim下建立一個frame.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/girl_1" android:duration="100"/>     <item android:drawable="@drawable/girl_2" android:duration="100"/>      <item android:drawable="@drawable/girl_3" android:duration="100"/>       <item android:drawable="@drawable/girl_4" android:duration="100"/>        <item android:drawable="@drawable/girl_5" android:duration="100"/>         <item android:drawable="@drawable/girl_6" android:duration="100"/>          <item android:drawable="@drawable/girl_7" android:duration="100"/>           <item android:drawable="@drawable/girl_8" android:duration="100"/>            <item android:drawable="@drawable/girl_9" android:duration="100"/>             <item android:drawable="@drawable/girl_10" android:duration="100"/>              <item android:drawable="@drawable/girl_11" android:duration="100"/>    </animation-list> 

     引用的是drawable下面的圖片,duration是設定時間是100毫秒

  

     看下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" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="播放動畫" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="停止動畫" />    </LinearLayout>      <RadioGroup          android:id="@+id/radioGroup1"         android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:orientation="horizontal"        >    <RadioButton        android:id="@+id/radioButton1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="單次播放" />    <RadioButton        android:id="@+id/radioButton2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="迴圈播放" />   </RadioGroup>   <TextView       android:id="@+id/textView1"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="拖動進度條修改透明度(0 - 255)之間" />   <SeekBar       android:id="@+id/seekBar1"       android:layout_width="match_parent"       android:layout_height="wrap_content" />   <ImageView       android:id="@+id/imageView1"       android:layout_width="200dip"       android:layout_height="200dip"       android:background="@anim/frame" /></LinearLayout>

   

     下面是控制幀動畫的主要代碼:

  

public class FrameDemoActivity extends Activity {    /** Called when the activity is first created. */private Button button1,button2;private RadioGroup radioGroup;private RadioButton radioButton1,radioButton2;private SeekBar seekBar;private ImageView imageView1;private AnimationDrawable animationDrawable;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                button1=(Button) this.findViewById(R.id.button1);        button2=(Button) this.findViewById(R.id.button2);        radioGroup=(RadioGroup) this.findViewById(R.id.radioGroup1);        radioButton1=(RadioButton) this.findViewById(R.id.radioButton1);        radioButton2=(RadioButton) this.findViewById(R.id.radioButton2);        seekBar=(SeekBar) this.findViewById(R.id.seekBar1);        imageView1=(ImageView) this.findViewById(R.id.imageView1);        //通過ImageView對象拿到背景顯示的AnimationDrawable        animationDrawable=(AnimationDrawable) imageView1.getBackground();              button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(!animationDrawable.isRunning()){animationDrawable.start();}}});        button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(animationDrawable.isRunning()){animationDrawable.stop();}}});        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(checkedId==radioButton1.getId()){//設定單次播放animationDrawable.setOneShot(true);}else if(checkedId==radioButton2.getId()){//設定迴圈播放animationDrawable.setOneShot(false);}//設定播放後重新啟動animationDrawable.stop();animationDrawable.start();}});        //監聽的進度條修改透明度        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stub//設定動畫Alpha值animationDrawable.setAlpha(progress);//通知imageView 重新整理螢幕imageView1.postInvalidate();}});    }}

   運行效果:

   

     進度條seekbar是設定透明度的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.