Android資源之映像資源(淡入淡出、嵌入)

來源:互聯網
上載者:User

標籤:android平台   xml   imageview   

今天把映像資源剩餘的幾個知識梳理一下。淡入淡出資源同映像狀態和映像層級資源一樣可以切換兩個映像(目前只支援兩個映像的切換),並且使這兩個映像以淡入淡出效果進行切換。如上一篇博文介紹的開關電燈一樣,如果加上淡入淡出效果會更好。

下面在res/drawable目錄中建立一個cross_fade.xml檔案,該檔案內容如下:

<?xml version="1.0" encoding="UTF-8"?><!-- transition標籤中只能有兩個item標籤 --><transition xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/lamp_off"></item>     <item android:drawable="@drawable/lamp_on"></item></transition>

在main_layout檔案中引用該資源檔,代碼如下:
  <ImageView      android:id="@+id/imageview_lamp"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:src="@drawable/cross_fade"      />
從第一個映像(第一個item‘中指定的映像)切換到第2個映像要使用TransitionDrawable.startTransition方法,從第2個映像切換到第1個映像要使用TransitionDrawable.reverseTransition方法,如下代碼所示:

public class MainActivity extends Activity {private ImageView ivLamp;@SuppressLint("NewApi")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Resources res=getResources();//Drawable drawable=res.getDrawable(R.drawable.bitmap_test);//TextView txt=(TextView)findViewById(R.id.textView);//txt.setBackground(drawable);ivLamp=(ImageView)findViewById(R.id.imageview_lamp);//設定level為8,顯示lamp_off.png//ivLamp.setImageLevel(8);}//"開燈"按鈕的單擊事件方法public void onClick_LampOn(View view){//設定level為15,顯示lamp_on.png//ivLamp.setImageLevel(15);TransitionDrawable drawable=(TransitionDrawable)ivLamp.getDrawable();//從第一個映像切換到第2個映像。其中使用1秒(1000毫秒)時間完成淡入淡出效果drawable.startTransition(1000);}//"關燈"按鈕的單擊事件方法public void onClick_LampOff(View view){//設定level為6,顯示lamp_off.png//ivLamp.setImageLevel(6);TransitionDrawable drawable=(TransitionDrawable)ivLamp.getDrawable();//從第2個映像切換到第1個映像。其中使用1秒(1000毫秒)時間完成淡入淡出效果drawable.reverseTransition(1000);}}


淡入淡出的效果如所示:

 如果顯示的映像要求小於裝載映像的視圖,可以考慮使用內嵌影像資源。內嵌影像資源是XML格式的檔案,只有一個<inset>標籤。使用如下的4個屬性設定映像距離上下左右4個方向的距離。

android:insetTop:映像距離上邊的距離。

android:insetRight:映像距離右側的距離。

android:insetBottom:映像距離底邊的距離。

android:insetLeft:映像距離左側的距離。

這個內嵌影像資源很好理解,故在此就不給出例子了。

轉載請註明出處:http://blog.csdn.net/android_jiangjun/article/details/33360661


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.