標籤:android imageview src blackground 不規則icon
Android實習劄記(10)---ImageView的src屬性 VS blackground屬性
轉載請註明出處:coder-pig
問題分析
相信大家對於ImageView圖片組件並不陌生吧,見名知意,就是用來顯示圖片的咯!
而顯示圖片的話可以通過src屬性,又或者blackground屬性來進行設定!
這些大家都知道,但是有沒有去糾結下什麼情況下用這個,什麼情況下用那個呢?
估計很多朋友的回答是"沒有",哈哈,那麼在本節中就來對這兩個屬性進行一個講解吧!
由淺入深,先說下大部分人都知道的:
一.常識
①background通常指的都是背景,而src指的是內容!!
②當使用src填入圖片時,是按照圖片大小直接填充,並不會進行展開
而使用background填入圖片,則是會根據ImageView給定的寬度來進行展開
多說無益,寫個簡單布局測試下咯:
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jay.example.imageviewdemo.MainActivity" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/pen" /> <ImageView android:layout_width="200dp" android:layout_height="wrap_content" android:background="@drawable/pen" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pen" /> <ImageView android:layout_width="200dp" android:layout_height="wrap_content" android:src="@drawable/pen" /></LinearLayout>
代碼很簡單,前面兩個用blackground,後面兩個用src,如下:
分析:
寬高都是wrap_content那就一樣,是原圖大小,但是,當我們固定了寬或者高的話,
差別就顯而易見了,blackground完全填充了整個ImageView,而src依舊是那麼大,
而且他置中了哦,這就涉及到了ImageView的另一個屬性scaleType了!
另外還有一點,這裡我們說了只設定width或者height哦!加入我們同時設定了
width和height的話,blackground依舊填充,但是,src的大小可能發生改變哦!
比如,我們測試下下面這段代碼:
<ImageView android:layout_width="100dp" android:layout_height="50dp" android:src="@drawable/pen" />
:
ps:至於ImageView的scaleType這裡就不詳細進行講解了,詳情可參閱筆者的另一篇博文:
android-UI組件執行個體大全之ImageView映像視圖
另外,這個屬性只對src屬性生效哦!!切記!!
二.解決blackground展開導致圖片變形的方法
在前面的中的第二個Imageview中我們可以看到圖片已經被展開變形了,
正方形變成了長方形,對於和我一樣有輕微強迫症的人來說,顯然是不可接受的,
有沒有辦法去設定呢?答案肯定是有的,筆者暫時知道的有以下兩種方式:
1)這個適用於動態載入ImageView的,代碼也漸漸,只要在添加View的時候,把大小寫死就可以了
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(48, 48); layout.addView(ibtnPen, layoutParam);
2)除了動態載入view,更多的時候,我們還是會通過xml布局的方式引入ImageView的
解決方案也不難,就是通過drawable的Bitmap資源檔來完成,然後blackground屬性設定為該檔案即可!
這個xml檔案在drawable檔案夾下建立,這個檔案夾是要自己建立的哦!!
pen_bg.xml:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/pen_bg" android:gravity="top" android:src="@drawable/pen" android:tileMode="disabled" > </bitmap>
上述代碼並不難理解,估計大家最迷惑的是titleMode屬性吧,這個屬性是平鋪,就是我們windows設定
背景時候的平鋪,多個小表徵圖鋪滿整個螢幕捏!記得了吧!不記得自己可以試試!disabled就是把他給禁止了!
就是上面這串簡單的代碼,至於調用方法如下:
動態: ibtnPen.setBacklgroundResource(R.drawable.penbg);
靜態: android:background = "@drawable/penbg"
三.設定透明度的問題
說完前面兩個區別,接著再說下setAlpha屬性咯!
這個很簡單,這個屬性,只有src時才是有效果的!!
!!!! src設定setAlpha( )才有效果!!!!!
四.兩者結合妙用:
這是網上的一張圖:
一看去是一個簡單的GridView,每個item都是一個ImageView,但是細心的你可能發現了,
上面的ICON都不是規則的,而是圓形,圓角矩形等等,於是乎這裡用到了src + background了!
要實現上述的效果,你只需要兩個操作:
找一張透明的png圖片 + 設定一個黑色的背景
(當然你也可以設定png的透明度來實現,不過結果可能和預想的有出入哦!)
哈哈,呆萌呆萌的小豬就這樣顯示到ImageView上了,哈哈,blackground設定了藍色背景!
代碼如下:
<ImageView android:layout_width="150dp" android:layout_height="wrap_content" android:src="@drawable/pig" android:background="#6699FF" />
另外,你也可以用selctor實現點擊效果,設定不同的情況設定不同的圖片,以實現點擊或者觸摸效果!
好了,本節就到這裡,如果對本文有什麼疑問或者發現什麼紕漏,歡迎告知,萬分感激!!
Android實習劄記(10)---ImageView的src屬性 VS blackground屬性