A transitiondrawable is a special drawable object that can fade in and out between two drawable resources.
<Transition> Each <item> under a node represents a drawable resource. There can be only two <item>. Previous conversion callstartTransition(). Backward, callreverseTransition().
-
The file is located:
-
res/drawable/filename.xml
File name as resource ID
-
Compilation Resource Type:
-
Point
TransitionDrawablePointer
-
Resource reference:
-
In Java:
R.drawable.filename
In XML:
@[package:]drawable/filename
-
Syntax:
-
<?xml version="1.0" encoding="utf-8"?><transitionxmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@[package:]drawable/drawable_resource" android:id="@[+][package:]id/resource_name" android:top="dimension" android:right="dimension" android:bottom="dimension" android:left="dimension" /></transition>
-
Element:
-
-
<transition>
-
Required.Must be used as the root node and contain one or more <item> elements.
Attribute:
-
xmlns:android
-
String type,
Required.The namespace that defines the XML file, which must be
"http://schemas.android.com/apk/res/android".
-
<item>
-
Defines a drawable used in a transitiondrawable. Required
<Transition> subnode. Acceptable
<Bitmap> subnode.
Attribute:
-
android:drawable
-
Drawable Resource. Required. References A drawable resource.
-
android:id
-
Resource ID. The unique identifier of a drawable resource.
Use "@ + ID/Name"To define a new resource ID for this item. Available
View.findViewById()Or
Activity.findViewById()To retrieve and modify this item.
-
android:top
-
Integer. Distance from the top
-
android:right
-
Integer. Distance from the right side
-
android:bottom
-
Integer. Distance from the bottom
-
android:left
-
Integer. Distance from left
-
Example:
-
Save the XML file as follows:
res/drawable/transition.xml:
<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/on" /> <item android:drawable="@drawable/off" /></transition>
Use the following in the layout file:
<ImageButton android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/transition" />
And the following code performs a 500 ms transition from the first item to the second:
ImageButton button = (ImageButton) findViewById(R.id.button);TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();drawable.startTransition(500);
-
Reference
-
The following is an example:
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>
Use in layout:
<ImageView android:id="@+id/imgView" android:src="@drawable/transition" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Control in code:
ImageView imageView = (ImageView) findViewById(R.id.imgView); TransitionDrawable transitionDrawable = (TransitionDrawable) imageView.getDrawable(); transitionDrawable.startTransition(3000);
The following is an example: to achieve the fade-in and fade-out effect of multiple image loops.
Package COM. example. drawabletest; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable; import android. graphics. drawable. transitiondrawable; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. widget. imageview; public class mainact Ivity extends activity {private int change = 0; private int [] IDs = new int [] {R. drawable. image1, R. drawable. image2, R. drawable. image3, R. drawable. image4, R. drawable. image5}; private drawable [] drawables; private imageview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); imageview = (imageview) findv Iewbyid (R. id. imgview);/* obtain the appropriate drawable resource */bitmapfactory. options opts = new bitmapfactory. options (); opts. injustdecodebounds = true; bitmapfactory. decoderesource (getresources (), R. drawable. image1, opts); opts. insamplesize = computesamplesize (OPTs,-1,500*500); opts. injustdecodebounds = false; drawables = new drawable [IDs. length]; try {for (INT I = 0; I <IDs. length; I ++) {// For Loop, load 5 drawable Resources Bitmap BMP = bitmapfactory. decoderesource (getresources (), IDS [I], opts); drawables [I] = new bitmapdrawable (BMP) ;}} catch (exception e) {e. printstacktrace ();} // enable the thread and change transition new thread (New myrunnable ()). start () ;}// handle transition changes private handler = new handler (new handler. callback () {public Boolean handlemessage (Message MSG) {int duration = MSG. arg1; transitiondrawable transitiondr Awable = NULL; transitiondrawable = new transitiondrawable (New drawable [] {drawables [Change % IDs. Length], // implement from 0 1 2 3 4 5 0 1 2... This constantly changes drawables [(Change + 1) % IDs. length]}); change ++; imageview. setimagedrawable (transitiondrawable); transitiondrawable. starttransition (duration); Return false ;}}); // thread, to send messages, so that transition keeps changing private class myrunnable implements runnable {public void run () {While (true) {int duration = 3000; // The change interval message = handler. obtainmessage (); message. arg1 = duration; handler. sendmessage (Message); try {Thread. sleep (duration);} catch (interruptedexception e) {e. printstacktrace () ;}}}// calculate the appropriate image size public static int computesamplesize (bitmapfactory. options options, int minsidelength, int maxnumofpixels) {int initialsize = values (options, minsidelength, maxnumofpixels); int roundedsize; If (initialsize <= 8) {roundedsize = 1; while (roundedsize <initialsize) {roundedsize <= 1 ;}} else {roundedsize = (initialsize + 7)/8*8;} return roundedsize;} // calculate the appropriate image size Private Static int computeinitialsamplesize (bitmapfactory. options options, int minsidelength, int maxnumofpixels) {double W = options. outwidth; double H = options. outheight; int lowerbound = (maxnumofpixels =-1 )? 1: (INT) math. Ceil (math. SQRT (w * H/maxnumofpixels); int upperbound = (minsidelength =-1 )? 128: (INT) math. min (math. floor (W/minsidelength), math. floor (H/minsidelength); If (upperbound <lowerbound) {// return the larger one when there is no overlapping zone. return lowerbound;} If (maxnumofpixels =-1) & (minsidelength =-1) {return 1;} else if (minsidelength =-1) {return lowerbound;} else {return upperbound ;}}}