TransitionDrawable&StateListDrawable的使用
2017-04-10 18:09
405 查看
这个SDK里面的一段代码:
比较适合来做一个简单的动画(比如文字的渐变放大效果等)
这个expand_collapse.xml文件放到Drawable文件夹当中:
当然在测试的时候需要准备2张图片image_expand,image_collapse
http://xueinsz.iteye.com/blog/767585
http://www.vogella.com/tutorials/AndroidDrawables/article.html
根据Button状态(normal,focused,pressed)显示不同背景图片
1. 在res/drawable目录下添加一个xml文件,用来描述Button在不同状态下对应的不同图片。我这里给该xml文件命名为btn_background.xml
2. 在res/layout目录下,对应的layout xml文件中,将Button的android:background属性设置为btn_background即可。
也可以代码实现:
Drawable资源:StateListDrawable,PaintDrawable,ShapeDrawable,NinePatchDrawable,BitmapDrawable
http://www.cnblogs.com/xirihanlin/archive/2010/06/14/1758145.html
LayerDrawable层叠样式layer-list
http://gundumw100.iteye.com/admin/blogs/896923
代码实现ColorStateList及StateListDrawable
http://blog.csdn.net/sodino/article/details/6797821
StateListAnimator 介绍
http://www.open-open.com/lib/view/open1471219964817.html
比较适合来做一个简单的动画(比如文字的渐变放大效果等)
Resources res = getResources(); TransitionDrawable transition = (TransitionDrawable) res .getDrawable(R.drawable.expand_collapse); ImageView image = (ImageView) findViewById(R.id.toggle_image); image.setImageDrawable(transition); //当间隔一秒后显示 transition.startTransition(1000);
这个expand_collapse.xml文件放到Drawable文件夹当中:
<?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>
当然在测试的时候需要准备2张图片image_expand,image_collapse
http://xueinsz.iteye.com/blog/767585
<?xml version="1.0" encoding="utf-8"?> <transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/first_image" /> <item android:drawable="@drawable/second_image" /> </transition>
final ImageView image = (ImageView) findViewById(R.id.image); final ToggleButton button = (ToggleButton) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(final View v) { TransitionDrawable drawable = (TransitionDrawable) image.getDrawable(); if (button.isChecked()) { drawable.startTransition(500); } else { drawable.reverseTransition(500); } } });
http://www.vogella.com/tutorials/AndroidDrawables/article.html
根据Button状态(normal,focused,pressed)显示不同背景图片
1. 在res/drawable目录下添加一个xml文件,用来描述Button在不同状态下对应的不同图片。我这里给该xml文件命名为btn_background.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/btn_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/btn_normal" /> <!-- focused --> <item android:drawable="@drawable/btn_normal" /> <!-- default --> </selector>
2. 在res/layout目录下,对应的layout xml文件中,将Button的android:background属性设置为btn_background即可。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_background" /> </LinearLayout>
也可以代码实现:
Integer[] mButtonState = { R.drawable.defaultbutton, R.drawable.focusedpressed, R.drawable.pressed }; Button mButton = (Button) findViewById(R.id.button); mButton.setBackgroundDrawable(myButton.setbg(mButtonState)); public StateListDrawable setbg(Integer[] mImageIds) { StateListDrawable bg = new StateListDrawable(); Drawable normal = this.getResources().getDrawable(mImageIds[0]); Drawable selected = this.getResources().getDrawable(mImageIds[1]); Drawable pressed = this.getResources().getDrawable(mImageIds[2]); bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed); bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected); bg.addState(View.ENABLED_STATE_SET, normal); bg.addState(View.FOCUSED_STATE_SET, selected); bg.addState(View.EMPTY_STATE_SET, normal); return bg; }
Drawable资源:StateListDrawable,PaintDrawable,ShapeDrawable,NinePatchDrawable,BitmapDrawable
http://www.cnblogs.com/xirihanlin/archive/2010/06/14/1758145.html
LayerDrawable层叠样式layer-list
http://gundumw100.iteye.com/admin/blogs/896923
代码实现ColorStateList及StateListDrawable
http://blog.csdn.net/sodino/article/details/6797821
StateListAnimator 介绍
http://www.open-open.com/lib/view/open1471219964817.html
相关文章推荐
- StateListDrawable资源和ShapeDrawable资源的使用
- StateListDrawable与<selector>的各种状态
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android入门——选择器selelctor之ColorStateList和StateListDrawable使用小结
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- StateListDrawable资源的使用
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- android 代码编写selector--StateListDrawable使用
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- StateListDrawable的使用
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android Drawable - State List Drawable使用详解(附图)
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解