android gif动画
16lz
2021-01-23
分解gif图片的工具很多,我用的Photoshop。
用Photoshop打开gif图片,然后文件--->导出--->渲染视频即可。
相应设置点击渲染即可。然后会出来4张图片
然后把图片们放进资源文件夹中,我放在了res/raw中;
在drawable中建立一个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="@raw/gj1"
- android:duration="100"/>
- <item
- android:drawable="@raw/gj2"
- android:duration="100"/>
- <item
- android:drawable="@raw/gj3"
- android:duration="100"/>
- <item
- android:drawable="@raw/gj4"
- android:duration="100"/>
- animation-list>
android:oneshot:true播放一次,false重复播放
android:duration:事件间隔
然后在需要用到的控件中
- <ImageView
- android:id="@+id/iv_gj"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/gif_gj" />
java代码:
- package com.example.giftest;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- private ImageView iv_gj;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- iv_gj = (ImageView) findViewById(R.id.iv_gj);
- }
- /*
- * 如果想让动画一开始就播放,可以重写此方法
- */
- @Override
- public void onWindowFocusChanged(boolean hasFocus) {
- /* 触发动画 */
- AnimationDrawable ad = (AnimationDrawable) iv_gj.getBackground();
- //如果xml中是这样设置的:android:src="@drawable/gif_gj",则使用下面的方法 //AnimationDrawable ad = (AnimationDrawable) iv_gj.getDrawable();
- ad.stop();
- ad.start();
- /* ***** */
- super.onWindowFocusChanged(hasFocus);
- }
- }
更多相关文章
- Android 4.1源码编译找不到资源文件解决办法
- 从Android读取Unity assets下的图片并保存为Bitmap格式
- 开机动画(闪动的ANDROID字样的动画图片)
- Android图文详解属性动画
- Android开机动画过程
- Android 根文件系统分析
- Android中的AndroidManifest.xml文件