ClipDrawable代表從其它位圖上截取一個(gè)"圖片片段",XML中的根元素為<clip.../>,截取的方向由clipOrientation控制
下面以一個(gè)慢慢展開(kāi)的圖片為例
先定義一個(gè)ClipDrawable資源文件my_clip.xml
?
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/fengjing" android:clipOrientation="horizontal" android:gravity="center"> </clip>
接下來(lái)在主界面main.xml中的imageview中引用它
?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 這里imageView的src定義為clipdrawable資源 --> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/my_clip" /> </LinearLayout>
主界面代碼
?
package WangLi.Resouce.ClipDrawableTest; import java.util.Timer; import java.util.TimerTask; 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; public class ClipDrawableTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageview = (ImageView)findViewById(R.id.image); //獲取圖片所顯示的ClipDrawble對(duì)象 final ClipDrawable drawable = (ClipDrawable)imageview.getDrawable(); final Handler handler = new Handler() { 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() { public void run() { Message msg = new Message(); msg.what = 0x1233; //發(fā)送消息,通知應(yīng)用修改ClipDrawable對(duì)象的level值 handler.sendMessage(msg); //取消定時(shí)器 if(drawable.getLevel() >= 10000) { timer.cancel(); } } },0,300); } }
運(yùn)行效果如下
剛開(kāi)始圖片從中間往兩邊展開(kāi)
最后展開(kāi)完整的圖片
利用ClipDrawable,還可以做圖片進(jìn)度條
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
