Android TransitionDrawable&StateListDrawable的使用
這個SDK裏麵的一段代碼:比較適合來做一個簡單的動畫(比如文字的漸變放大效果等)
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:andro> <item android:drawable="@drawable/image_expand" /> <item android:drawable="@drawable/image_collapse" /> </transition>
根據Button狀態(normal,focused,pressed)顯示不同背景圖片
1. 在res/drawable目錄下添加一個xml文件,用來描述Button在不同狀態下對應的不同圖片。我這裏給該xml文件命名為btn_background.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:andro> <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:andro 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
https://www.cnblogs.com/xirihanlin/archive/2010/06/14/1758145.html
LayerDrawable層疊樣式layer-list
https://gundumw100.iteye.com/admin/blogs/896923
代碼實現ColorStateList及StateListDrawable
https://blog.csdn.net/sodino/article/details/6797821
最後更新:2017-04-02 16:47:36