Beginner Android, using ClipDrawable resources using Drawable resources (16)

ClipDrawable represents cutting a “picture fragment” from other [bitmaps.] The root element in XML is , and the direction of cutting is controlled by clipOrientation

Here is an example of a slowly unfolding picture

First define a ClipDrawable resource file [my_clip.xml]

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/fengjing"
    android:clipOrientation="horizontal"
    android:gravity="center">
</clip>

Next, reference it in the imageview in the main interface main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_clip" />
</LinearLayout>

main interface code

package WangLi.Resouce.ClipDrawableTest;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

public class ClipDrawableTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView imageview = (ImageView)findViewById(R.id.image);
        //Get the ClipDrawble object displayed by the picture 
        final ClipDrawable drawable = (ClipDrawable)imageview.getDrawable();
         final Handler handler = new Handler()
        {
            public void handleMessage(Message msg)
            {
                if(msg.what == 0x1233)
                {
                    //Modify the level value of ClipDrawable 
                    drawable.setLevel(drawable.getLevel() + 200 );
                }
            }
        };
        final Timer timer = new Timer();
        timer.schedule(new TimerTask()
        {
            public void run()
            {
                Message msg = new Message();
                msg.what = 0x1233 ;
                 //Send a message to notify the application to modify the level value of the ClipDrawable object
                handler.sendMessage(msg);
                //Cancel the timer 
                if (drawable.getLevel() >= 10000 )
                {
                    timer.cancel();
                }
            }
        },0,300);
    }
}

The operation effect is as follows

At the beginning, the picture expands from the middle to the two sides

Finally expand the full picture

Using ClipDrawable, you can also make a picture progress bar

Leave a Comment

Your email address will not be published. Required fields are marked *