標籤:
ClipDrawable
ClipDrawable代表從其他位元影像上截取一個“圖片片段”
在XML檔案中定義ClipDrawable對象使用<clip.../>元素,該元素的文法為:
以上文法格式中可指定如下三個屬性:
1.android:drawable:指定截取的源Drawable對象
2.android:clipOrientaton:指定截取方向,可設定水平或垂直截取
3.android:gravity:指定截取時的對齊
註:使用ClipDrawable對象是可調用setLevel(int levle)方法設定截取的地區大小,當level為0時,截取的圖片片段為空白;當level為10000時,截取整張圖片。
執行個體如下:徐徐展開的風景
注意:實際開發中,可採用該種方式完成進度顯示控制。
資源檔==》drawable檔案夾下<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="horizontal" android:drawable="@drawable/people" android:gravity="center" ></clip>布局檔案==》<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/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/myclip" /> </LinearLayout>代碼實現==》package com.example.myclipdrawable;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.drawable.ClipDrawable;import android.view.Menu;import android.widget.ImageView;@SuppressLint("HandlerLeak")public class MainActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageView img = (ImageView) this.findViewById(R.id.image);final ClipDrawable drawable = (ClipDrawable) img.getDrawable();final Handler hanler = new Handler(){@Overridepublic void handleMessage(Message msg){if (msg.what == 1){int value=drawable.getLevel() + 200;drawable.setLevel(value);}}};final Timer timer = new Timer();timer.schedule(new TimerTask(){@Overridepublic void run(){Message msg = new Message();msg.what = 1;hanler.sendMessage(msg);// 取消定時器if (drawable.getLevel() >= 10000){timer.cancel();}}}, 0, 300);}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
運行效果:隨著時間的變化,圖片逐漸被截取完成
android學習筆記34——ClipDrawable資源