在使用ImageButton時,圖片被縮放的原因及解決方案

來源:互聯網
上載者:User

在使用ImageButton時,發現圖片被縮小,總是不能鋪滿按鈕,一時找不到原因,實在是讓人無奈,只能採用笨辦法,把圖片做的比按鈕大,即使縮小了也能鋪滿按鈕。

        後來注意到ndroid工程中有三個目錄(drawable-ldpi、drawable-mdpi、drawable-hdpi)都是可以放置圖片的,於是嘗試了下,把圖片在每個路徑下都放一份,結果問題解決了。

        原來這與螢幕的精細度相關的,不同裝置的LCD精細度(即每英寸的像素點數目)不一樣,同樣的圖片在不同精細度的LCD上顯示的效果就可能不一樣,應用模組可能需準備不同的圖片來適應不同精細度。

        在android工程中,是用圖片放置的目錄來區分的,三個目錄對應的精細度如下:

120: drawable-ldpi

160: drawable-mdpi

240: drawable-hdpi

使用者還可以將圖片放置drawable目錄下,這個預設是對應160精細度的。

三個目錄片,在不同精細度的LCD上,具體是按怎樣的縮放比例顯示的,本人也說不清,下面做了測試:

把同一張圖片複製幾次,在drawable、drawable-ldpi、drawable-mdpi、drawable-hdpi都放置一份,將圖片名字分別命名為dpi.png、ldpi.png、mdpi.png、hdpi.png,然後在程式中分別建立幾組圖片按鈕(ImageButton),查看顯示效果:

中依次是120、160、240三種精細度的模擬器的顯示效果,可以看出只有放在與LCD精細度相對應的目錄下的圖片才能正常顯示,不被縮放:

120的模擬器上第二行的圖片顯示正常,其他的均被縮放

160的模擬器上第一行(drawable目錄是對應160的精細度)和第三行的圖片顯示正常,其他均被縮放

240的模擬器上第四行的圖片顯示正常,其他均被縮放

       如果應用模組要運行在這幾種LCD精細度的裝置上,就應該這三個目錄下分別放置一份圖片,這樣才能保證圖片顯示正常。

另外一種方法,在xml中定義ImageButton時,加上屬性android:scaleType="fitXY",這樣也可以保證圖片與按鈕指定的大小一致,但圖片因為被縮放,可能會模糊。

代碼很簡單,主要是布局:

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <LinearLayout     android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="120px"    >    <ImageButton android:id="@+id/button0"android:layout_width="130px"android:layout_height="73px"android:src="@drawable/dpi"/><ImageButton android:id="@+id/button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/dpi"/><TextView android:id="@+id/text0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@drawable/dpi"/>    </LinearLayout>    <LinearLayout     android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="120px"    >    <ImageButton android:id="@+id/button1"android:layout_width="130px"android:layout_height="73px"android:src="@drawable/ldpi"/><ImageButton android:id="@+id/button11"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ldpi"/><TextView android:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@drawable/ldpi"/>    </LinearLayout>    <LinearLayout     android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="120px"    >    <ImageButton android:id="@+id/button2"android:layout_width="130px"android:layout_height="73px"android:src="@drawable/mdpi"/><ImageButton android:id="@+id/button21"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/mdpi"/><TextView android:id="@+id/text2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@drawable/mdpi"/>    </LinearLayout>    <LinearLayout     android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="120px"    >    <ImageButton android:id="@+id/button3"android:layout_width="130px"android:layout_height="73px"android:src="@drawable/hdpi"/><ImageButton android:id="@+id/button31"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/hdpi"/><TextView android:id="@+id/text3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@drawable/hdpi"/>    </LinearLayout></LinearLayout>

聯繫我們

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