The transition effect that realizes the fade and fade of two pictures transition drawable the way to achieve the dynamic over effect between two pictures.
Run as follows: The first one is the initial interface, the second is the over-the-middle interface, and the third is the end of the transition
Create a new Android project for Transitiondrawable.
Directory structure:
The main interface Activity_main.xml code is as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <button android:layout_width= "match_parent" android:layout_height= "Wrap_content " android:text= "@string/button_text" android:layout_centerinparent= "true" android:onclick= "Change" android:background= "@drawable/transition" android:layout_marginbottom= "20DP"/></relativelayout>
The main interface is a button with transitiondrawable applied.
Transition is under the Drawalbe folder, and its code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><transition xmlns:android= "http://schemas.android.com/apk/res/ Android "><item android:drawable=" @color/red "></item><item android:drawable=" @color/green "> </item></transition>
The Strings.xml code is as follows:
<string name= "App_name" >TransitionDrawable</string> <string name= "Hello_world" >hello world! </string> <string name= "action_settings" >Settings</string> <string name= "Button_ Text > Transition effects </string>
The Colors.xml code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <color name= "Red" > #ff0000 </color> <color name= "green" > #00ff00 </color> </resources>
The Mainactivity.java code is as follows
Package Com.shen.transitiondrawable;import Android.app.activity;import Android.graphics.drawable.transitiondrawable;import Android.os.bundle;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.widget.button;public class MainActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} public void Change (View v) {TranSitiondrawable drawable = (transitiondrawable) ((Button) v). Getbackground ();d rawable.starttransition (2000);}}
SOURCE download
Extended reading:
Http://www.tuicool.com/articles/uiaYBrU
Http://adk.tumblr.com/post/13069401302/outofmemoryerror-imageview-and-transitiondrawable
Android transitiondrawable imageview Over-effects usage example