AndroidStudio中使用开源框架android-gif-drawable
2016-01-29 11:21
585 查看
一般情况下,我们在Android项目中只能使用.png,.jpg等格式的静态图片,诸如.gif格式的动态图片是加载不了的。
有了android-gif-drawable这个开源框架,在项目中使用gif动态图片不再是问题。 android-gif-drawable的
开源地址是:https://github.com/koral–/android-gif-drawable。其主要作用就是可以展示gif格式的图片及动画,还可以对动画进行监听操作。开源库中封闭了GifImageView,GifImageButton,GifTextView等控件。和我们普通的ImageView,ImageButton,TextView使用方法基本一样,只要在布局文件中添加这些控件,然后就可以直接设置gif图片作为背景等。
首先在AndroidStudio中配置相关依赖库代码,在配置文件build.gradle文件中添加:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+' }
布局文件,以GifImageView和GifTextView为例:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <pl.droidsonroids.gif.GifImageView android:id="@+id/test_gif" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" /> <pl.droidsonroids.gif.GifTextView android:layout_margin="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/test" android:text="这个TextView的背景好酷" android:textColor="#FFFFFF" /> </LinearLayout>
代码:
public class MainActivity extends AppCompatActivity { private GifImageView test_gif; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test_gif = (GifImageView) findViewById(R.id.test_gif); //设置图片数据 test_gif.setImageResource(R.drawable.test); final android.widget.MediaController mediaController = new android.widget.MediaController(this); mediaController.setMediaPlayer((GifDrawable) test_gif.getDrawable()); test_gif.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mediaController.show(); Snackbar.make(test_gif, "可以点击哦", Snackbar.LENGTH_LONG).show(); } }); } }
其中项目中的图片是我在百度上随便找的一张gif格式 的图片,运行效果就是手机上有两张动态图片,其中一张上有文字“这个TextView的背景好酷”。
相关文章推荐
- Android之传感器(一)光照传感器
- android项目中出现的错误
- 详解Android自定义控件属性TypedArray以及attrs
- android实现仿QQ登陆界面的多账号保存
- 4000 android 有确认和取消按钮的 AlertDialog 弹出的使用说明
- Android Studio导入Fresco
- Android实现计时与倒计时(限时抢购)的几种方法
- Android开发常用的adb命令整理
- Android检测手机中存储卡及剩余空间大小的方法(基于Environment,StatFs及DecimalFormat)
- Android SQLiteDatabase 数据库存储
- Android ImageView(scaleType属性)图片按比例缩放
- Android 字符串资源
- Android连续点两下Back退出程序
- android-Planning for Multiple Touchscreen Sizes,Providing Descendant and Lateral Navigation
- Android Fragment生命周期深入探究
- RecyclerView完全解析
- Android基于AudioManager、PhoneStateListener实现设置黑名单功能
- 关于Android的浅杀
- 推荐几款实用的Android Studio 插件
- Xamarin开发Android之创建一个项目并调试