android學習筆記34——ClipDrawable資源

來源:互聯網
上載者:User

標籤:

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資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.