The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

          ImageView中XML屬性src和background的區(qū)別:

          background會根據(jù)ImageView組件給定的長寬進行拉伸,而src就存放的是原圖的大小,不會進行拉伸。src是圖片內(nèi)容(前景),bg是背景,可以同時使用。

          此外:scaleType只對src起作用;bg可設(shè)置透明度,比如在ImageButton中就可以用android:scaleType控制圖片的縮放方式


          如上所述,background設(shè)置的圖片會跟View組件給定的長寬比例進行拉伸。舉個例子, 36x36 px的圖標放在 xhdpi 文件夾中,在854x480(FWVGA,對應(yīng)hdpi)環(huán)境下,按照

          xhdpi : hdpi : mdpi: ldip = 2 : 1.5 : 1 : 0.75

          的比例計算,在FWVGA下,圖標的實際大小應(yīng)該是 27x27。

          但是當我把它放到一個 layout_width = 96px, layout_height = 75px 的 LinearLayout,布局代碼如下:

          1. <LinearLayout android:gravity="center" android:layout_width="96px" android:layout_height="75px"  >  
          2.     <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toolbar_bg"/>      
          3. </LinearLayout>  

          實際情況是,我們得到的ImageButton的大小是 33x27,很明顯width被拉伸了,這是我們不想看到的情況。

          解決方案一:

          代碼中動態(tài)顯式設(shè)置ImageButton的layout_width和layout_width,如下 

          1. LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(2727);  
          2. layout.addView(imageButton, layoutParam);  
          不過,事實上我們并不希望在代碼存在“硬編碼”的情況。

          解決方案二:

          在你通過setBackgroundResource()或者在xml設(shè)置android:background屬性時,將你的background以XML Bitmap的形式定義,如下:

          1. <?xml version="1.0" encoding="utf-8"?>  
          2. <bitmap xmlns:android="http://schemas.android.com/apk/res/android"  
          3.     android:id="@id/toolbar_bg_bmp"  
          4.     android:src="@drawable/toolbar_bg"  
          5.     android:tileMode="disabled" android:gravity="top" >  
          6. </bitmap>  
          調(diào)用如下:

              imageButton.setBackgroundResource(R.drawable.toolbar_bg_bmp)

          或者

              <ImageButton ...  android:background="@drawable/toolbar_bg_bmp" ... />

          若背景圖片有多種狀態(tài),還可參照toolbar_bg_selector.xml:

          1. <?xml version="1.0" encoding="utf-8"?>  
          2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
          3.     <item android:state_pressed="true" >  
          4.         <bitmap android:src="@drawable/toolbar_bg_sel" android:tileMode="disabled" android:gravity="top" />  
          5.     </item>  
          6.     <item >  
          7.         <bitmap android:src="@drawable/toolbar_bg" android:tileMode="disabled" android:gravity="top" />  
          8.     </item>  
          9. </selector>  


          如此,不管是通過代碼方式setBackgroundResource()或XML android:background方式設(shè)置背景,均不會產(chǎn)生被拉伸的情況。

          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
          posted on 2015-03-01 00:31 Eric_jiang 閱讀(333) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 漯河市| 阳江市| 镇宁| 宁陕县| 青海省| 无锡市| 永善县| 黄骅市| 潢川县| 鹤庆县| 汽车| 桃江县| 平远县| 龙里县| 宾川县| 忻州市| 砀山县| 林甸县| 北辰区| 澜沧| 荔波县| 积石山| 贡嘎县| 耒阳市| 大同市| 潞西市| 当涂县| 岢岚县| 南投市| 宝兴县| 龙岩市| 华坪县| 常山县| 武定县| 涡阳县| 烟台市| 井陉县| 法库县| 界首市| 仙游县| 吉安县|