android之帧动画实现

         今天实现了一个帧动画的例子,

       首先在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() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!animationDrawable.isRunning()){
				animationDrawable.start();
				}
			}
		});
        button2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(animationDrawable.isRunning()){
				animationDrawable.stop();
				}
			}
		});
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(checkedId==radioButton1.getId()){
					//设置单次播放
					animationDrawable.setOneShot(true);
				}else if(checkedId==radioButton2.getId()){
					//设置循环播放
					animationDrawable.setOneShot(false);
				}
				//设置播放后重新启动
				animationDrawable.stop();
				animationDrawable.start();
			}
		});
        //监听的进度条修改透明度
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				//设置动画Alpha值
				animationDrawable.setAlpha(progress);
				//通知imageView 刷新屏幕
				imageView1.postInvalidate();
			}
		});
    }
}

   运行效果如图所示:

   

     进度条seekbar是设置透明度的。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值