ClipDrawable代表從其它點陣圖上擷取一個"圖片片段",XML中的根元素為<clip.../>,擷取的方向由clipOrientation控制
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:clipOrientation=["horizontal" | "vertical"] android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] />
android:clipOrientation有兩個屬性,預設為horizontal
android:gravity有兩個屬性,預設為left
使用時可以用ClipDrawable的setLevel(int level)方法來設定擷取圖片的區域大小,當level為0,就是空;為10000時,擷取整個圖片。
下面是通過這個物件來實現圖片緩緩展開的效果。
轉自:http://blog.csdn.net/jiaruihua_blog/article/details/12276215
思路:
1.在xml中寫clip標籤,在drawable中放入一個圖片
2.在佈局中放一個ImageView,src選擇這個xml檔案
3.在程式碼中用Imageview的findViewById()找到imageview物件
4.通過 final ClipDrawable drawable = (ClipDrawable)imageview.getDrawable(); 得到ClipDrawable物件
5.通過Timer和Handler結合來改變擷取圖片的大小,這裡要通過setLevel()來做
6.當ClipDrawable.getLevel() >= 10000時結束擷取,因為這時已經擷取了全部圖片
程式碼:
<?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>
java
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); } }
詳細參考:
http://blog.csdn.net/lee576/article/details/7827676
http://blog.csdn.net/jiaruihua_blog/article/details/12276215