现在的位置: 首页 > 综合 > 正文

Android界面布局中使用GIF动画

2013年09月11日 ⁄ 综合 ⁄ 共 1263字 ⁄ 字号 评论关闭

在res目录下新建anim动画文件夹,新建一个animation.xml

<?xml version="1.0" encoding="UTF-8"?> 
<animation-list android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
 
    <!-- duration为每张逐帧播放间隔,
    	 oneshot 为false 代表循环播放,设置为true 即播放一次即停止。 -->
    <item android:duration="300" android:drawable="@drawable/img0" /> 
    <item android:duration="300" android:drawable="@drawable/img1" /> 
    <item android:duration="300" android:drawable="@drawable/img2" /> 
    <item android:duration="300" android:drawable="@drawable/img3" /> 
</animation-list>  

1.点击播放动画

public class animActivity extends Activity implements OnClickListener {  
    ImageView iv = null;  
 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
 
        iv = (ImageView) findViewById(R.id.iv);  
        iv.setOnClickListener(this);  
    }  
 
    @Override  
    public void onClick(View v) {  
        AnimationDrawable anim = null;  
        Object ob = iv.getBackground();  
        anim = (AnimationDrawable) ob;  
        anim.stop();  
        anim.start();  
    }  
}  

2.自动播放

在Values 文件下新建一个styles .xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<resources> 
    <style name="animStyle" parent="@android:style/Widget.ProgressBar.Large"> 
        <item name="android:indeterminateDrawable">@anim/animation</item> 
    </style> 
</resources>   

在XML中就可以通过设置它的样式使其为我们工作

<ProgressBar style="@style/animStyle" 
    android:layout_width="100dp" 
    android:layout_height="100dp" />

抱歉!评论已关闭.