ClipDrawable 資源

ClipDrawable 表明從其餘位圖上截取的一個「圖片片斷」。
示例:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView  
    android:id="@+id/image"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scaleType="fitStart"
    android:src="@drawable/my_clip"
    />
</LinearLayout>
my_clip.xml
<?xml version="1.0" encoding="UTF-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/shuangta" 
    android:clipOrientation="horizontal" 
    android:gravity="center"> 
</clip>
由於 ClipDrawble 對象可調用 setLevel(int level)控制截取圖片的部分,所以本示例只要設 置一個定時器,讓程序不斷調用 ClipDrawble 的 setLevel(int level)方法便可實現圖片徐徐展開 的效果。
 
ClipDrawableTest.java
package org.crazyit.res;

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

import org.crazyit.res.R;

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;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class ClipDrawableTest extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView imageview = (ImageView) findViewById(R.id.image);
        // 獲取圖片所顯示的ClipDrawable對象
        final ClipDrawable drawable = (ClipDrawable)
            imageview.getDrawable();
        final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                // 若是該消息是本程序所發送的
                if (msg.what == 0x1233)
                {
                    // 修改ClipDrawable的level值
                    drawable.setLevel(drawable.getLevel() + 200);
                }
            }
        };
        final Timer timer = new Timer();
        timer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                Message msg = new Message();
                msg.what = 0x1233;
                // 發送消息,通知應用修改ClipDrawable對象的level值。
                handler.sendMessage(msg);
                // 取消定時器
                if (drawable.getLevel() >= 10000)
                {
                    timer.cancel();
                }
            }
        }, 0, 300);
    }
}
ScreenShot:雙塔圖片逐漸從中間向兩邊展開。
 
相關文章
相關標籤/搜索