ClipDrawable refers to capturing an "image clip" from another bitmap ". Use the <clip.../> element in the XML file to define the ClipDrawable object. You can specify the following three attributes:
Android: drawable: Specifies the truncated source Drawable object.
Android: clipOrientation: Specifies the direction of the screenshot. It can be set to horizontal or vertical.
Android: gravity: Specifies the alignment mode when intercepting
When using the ClipDrawable object, you can call the setLevel (int level) method to set the size of the captured area. When the level is 0, the captured Image segment is blank. When the level is 10000, capture the entire image.
Through the above description, we found that ClipDrawable can be used to control the area size of the captured image, so that the program can continuously call the setLevel method and change the level value to make the image expand slowly.
First, we define a ClipDrawable object:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Clip
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: drawable = "@ drawable/muller"
Android: clipOrientation = "horizontal"
Android: gravity = "center">
</Clip>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Clip
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: drawable = "@ drawable/muller"
Android: clipOrientation = "horizontal"
Android: gravity = "center">
</Clip>
Next, simply set the layout file so that the image can be displayed on the screen (pay attention to the selection of the src attribute of ImageView ):
[Html]
<LinearLayout 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"
Android: orientation = "vertical"
Tools: context = ". MainActivity">
<ImageView
Android: id = "@ + id/view"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: src = "@ drawable/clip"/>
</LinearLayout>
<LinearLayout 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"
Android: orientation = "vertical"
Tools: context = ". MainActivity">
<ImageView
Android: id = "@ + id/view"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: src = "@ drawable/clip"/>
</LinearLayout>
Set a timer in the main program to change the level value:
[Java]
Public class MainActivity extends Activity
{
Private ImageView view = null;
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
Super. setContentView (R. layout. activity_main );
This. view = (ImageView) super. findViewById (R. id. view );
// Obtain the ClipDrawable object displayed in the image
Final ClipDrawable drawable = (ClipDrawable) view. getDrawable ();
Final Handler handler = new Handler ()
{
@ Override
Public void handleMessage (Message msg)
{
// If the message is sent by the program
If (msg. what = 0x123)
{
// Modify the level value of ClipDrawable
Drawable. setLevel (drawable. getLevel () + 200 );
}
}
};
Final Timer timer = new Timer ();
Timer. schedule (new TimerTask ()
{
@ Override
Public void run ()
{
Message msg = new Message ();
Msg. Why = 0x123;
// Send a message to notify the application to modify the level value of ClipDrawable
Handler. sendMessage (msg );
// Cancel the timer
If (drawable. getLevel ()> = 10000)
{
Timer. cancel ();
}
}
},0,300 );
}
}
Public class MainActivity extends Activity
{
Private ImageView view = null;
@ Override
Protected void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
Super. setContentView (R. layout. activity_main );
This. view = (ImageView) super. findViewById (R. id. view );
// Obtain the ClipDrawable object displayed in the image
Final ClipDrawable drawable = (ClipDrawable) view. getDrawable ();
Final Handler handler = new Handler ()
{
@ Override
Public void handleMessage (Message msg)
{
// If the message is sent by the program
If (msg. what = 0x123)
{
// Modify the level value of ClipDrawable
Drawable. setLevel (drawable. getLevel () + 200 );
}
}
};
Final Timer timer = new Timer ();
Timer. schedule (new TimerTask ()
{
@ Override
Public void run ()
{
Message msg = new Message ();
Msg. Why = 0x123;
// Send a message to notify the application to modify the level value of ClipDrawable
Handler. sendMessage (msg );
// Cancel the timer
If (drawable. getLevel ()> = 10000)
{
Timer. cancel ();
}
}
},0,300 );
}
}
Program running effect: