ClipDrawable资源
ClipDrawable代表从其他位图上截取的一个“图片片段”。
在xml文件中定义了ClipDrawable对象使用<clip.../>元素,该元素的语法为:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource" //指定截取的源Drawable对象
android:clipOrientation="horizontal | vertical" //指定截取方向,可设置水平截取,垂直截取
android:gravity="top|bottom|left|right|center_vertical" //指定截取试对齐方式
>
</clip>
使用ClipDrawable对象时可调用setLevel(int level)方法来设置截取区域大小。当level为0时,截取的图片片段为空,当level 10000时,截取整张图片。
clip.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/wallpaper_01"
android:clipOrientation="vertical"
android:gravity="center_vertical"
>
</clip>
activity_main.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.clipdrawabletest.MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="21dp"
android:src="@drawable/clip" />
</RelativeLayout>
package com.example.clipdrawabletest;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends ActionBarActivity {
private ImageView img ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView1);
//获取ClipDrawable对象
final ClipDrawable drawable = (ClipDrawable) img.getDrawable();
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
if(msg.what == 0x123){
drawable.setLevel(drawable.getLevel()+500);
}
}
};
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);
}
@Override
public 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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}