代碼:
clip 屬性的使用:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/gril"
android:clipOrientation="horizontal"
android:gravity="center">
</clip>
layout 中的設定:
<RelativeLayout 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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:src="@drawable/clip_style"
/>
</RelativeLayout>
代碼:
package com.example.clipdrawable;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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) {
// TODO Auto-generated method stub
if (msg.what==0x123) {
/**setlevel()設定圖片截取的大小
* 修改ClipDrawable的level值,level值為0--10000;
* 0:截取圖片大小為空白,10000:截取圖片為整張圖片;
*/
drawable.setLevel(drawable.getLevel()+200);
}
}
};
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message msg = new Message();
msg.what = 0x123;
handler.sendMessage(msg);
if (drawable.getLevel()>=10000) {
timer.cancel();
}
}
}, 0,300);
}
}