Android 中TransitionDrawable的使用
2014-04-30 21:27
357 查看
1、TransitionDrawable是什么
一个TransitionDrawable是一个特殊的Drawable对象,可以实现两个drawable资源之间淡入淡出的效果。
2、TransitionDrawable的Xml文件定义
<transition>节点下的每个<item>代表一个drawable资源。只能有两个<item>。先前转换调用startTransition()。向后,调用 reverseTransition()。
下面是实例:
1、xml方式使用
在layout中使用:
在代码中控制:
下面举个使用TransitionDrawable 的例子,创建一个Android工程,然后再这个工程的基础上修改,修改过程如下: 1、去掉layout/main.xml中的TextView,增加ImagView,如下:
2、创建一个XML文件,命名为expand_collapse.xml,内容如下:
需要3张png图片,存放到res\drawable目录下,3张图片分别命名为:my_image.png、image_expand.png、image_collapse.png。
3、修改Activity中的代码,内容如下:
一个TransitionDrawable是一个特殊的Drawable对象,可以实现两个drawable资源之间淡入淡出的效果。
2、TransitionDrawable的Xml文件定义
<transition>节点下的每个<item>代表一个drawable资源。只能有两个<item>。先前转换调用startTransition()。向后,调用 reverseTransition()。
下面是实例:
1、xml方式使用
transition.xml: <?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/image01"/> <item android:drawable="@drawable/image02"/> </transition>
在layout中使用:
<ImageView android:id="@+id/imgView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/transition" />
在代码中控制:
ImageView imageView = (ImageView) findViewById(R.id.imgView); TransitionDrawable transitionDrawable = (TransitionDrawable) imageView.getDrawable(); transitionDrawable.startTransition(3000)
下面举个使用TransitionDrawable 的例子,创建一个Android工程,然后再这个工程的基础上修改,修改过程如下: 1、去掉layout/main.xml中的TextView,增加ImagView,如下:
<ImageView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:tint=”#55ff0000″ android:src=”@drawable/my_image”/>
2、创建一个XML文件,命名为expand_collapse.xml,内容如下:
<?xml version=”1.0″ encoding=”UTF-8″?> <transition xmlns:android=”http://schemas.android.com/apk/res/android”> <item android:drawable=”@drawable/image_expand”/> <item android:drawable=”@drawable/image_collapse”/> </transition>
需要3张png图片,存放到res\drawable目录下,3张图片分别命名为:my_image.png、image_expand.png、image_collapse.png。
3、修改Activity中的代码,内容如下:
LinearLayout mLinearLayout; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLinearLayout = new LinearLayout(this); ImageView i = new ImageView(this); i.setAdjustViewBounds(true); i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mLinearLayout.addView(i); setContentView(mLinearLayout); Resources res = getResources(); TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.expand_collapse); i.setImageDrawable(transition); transition.startTransition(10000); }
相关文章推荐
- Android Drawable - Transition Drawable使用详解(附图)
- Android TransitionDrawable ImageView过度效果使用实例
- Android TransitionDrawable ImageView过度效果使用实例
- Android 中TransitionDrawable的使用
- Android使用TransitionDrawable渐变切换多张图片
- android transitiondrawable基本使用
- Android学习15--使用(Drawable)资源
- Android笔记之TextView中使用Drawable
- 创建Material Design风格的Android应用--使用Drawable
- android中drawable资源文件的使用
- android修改控件外观(使用drawable资源)
- Android Shape Drawable 静态使用和动态使用(圆角,渐变实现)
- android bitmapdrawable基本使用
- Android中View绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中AnimationDrawable使用的简单实例
- Android 使用LayerDrawable自定制SeekBar的外观
- Android基础之drawable标签的使用下
- 使用TransitionDrawable实现两张图片的过渡效果
- Android Drawable Resource学习(七)、TransitionDrawable
- android textview drawableleft使用时遇到的问题