Android ImageView圖片自適應

jopen 10年前發布 | 83K 次閱讀 Android Android開發 移動開發

網絡上下載下來的圖片自適應:android:adjustViewBounds="true"(其詳細解釋在下面)
<ImageView

   android:id="@+id/dynamic_item_image"

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   android:layout_gravity="top"

   android:layout_marginTop="5dip"

   android:adjustViewBounds="true"

   android:background="@drawable/imageview_background" />
另外,android:background="@drawable/imageview_background"是給圖片加了一個邊框,其中

imageview_background.xml:

<?xml version="1.0" encoding="utf-8"?> <shape  xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/white"/> <stroke android:width="2.0dip" android:color="#99D9D9D9" /> <corners android:radius="2.0dip" /> <padding android:left="5.0dip" android:top="5.0dip" android:right="5.0dip" android:bottom="5.0dip" /> </shape>

ImageView屬性說明:

1、類概述

                顯示任意圖像,例如圖標。ImageView類可以加載各種來源的圖片(如資源或圖片庫),需要計算圖像的尺寸,比便它可以在其他布局中使用,并提供例如縮放和著色(渲染)各種顯示選項。

2、XML屬性

  • 屬性名稱

    </td>

    描述

    </td> </tr>

    android:adjustViewBounds

    </td>

    是否保持寬高比。需要與maxWidthMaxHeight一起使用,否則單獨使用沒有效果。

    </td> </tr>

    android:cropToPadding

    </td>

    是否截取指定區域用空白代替。單獨設置無效果,需要與scrollY一起使用,效果如下,實現代碼見代碼部分:

    Android ImageView圖片自適應 

     

    </td> </tr>

    android:maxHeight

    </td>

    設置View的最大高度,單獨使用無效,需要與setAdjustViewBounds一起使用。如果想設置圖片固定大小,又想保持圖片寬高比,需要如下設置:

    1) 設置setAdjustViewBoundstrue

    2) 設置maxWidthMaxHeight

    3) 設置設置layout_widthlayout_heightwrap_content

    </td> </tr>

    android:maxWidth

    </td>

    設置View的最大寬度。同上。

    </td> </tr>

    android:scaleType

    </td>

    設置圖片的填充方式。

    </tr>

    matrix

    </td>

    0

    </td>

    用矩陣來繪圖

    </td>


    fitXY

    </td>

    1

    </td>

    拉伸圖片(不按比例)以填充View的寬高

    Android ImageView圖片自適應

     

    </td>

    layout_

    height

    :30px


    layout_

    width

    :120px

    </td> </tr>

    fitStart

    </td>

    2

    </td>

    按比例拉伸圖片,拉伸后圖片的高度為View的高度,且顯示在View的左邊

    Android ImageView圖片自適應

     

    </td> </tr>

    fitCenter

    </td>

    3

    </td>

    按比例拉伸圖片,拉伸后圖片的高度為View的高度,且顯示在View的中間

    Android ImageView圖片自適應

     

    </td> </tr>

    fitEnd

    </td>

    4

    </td>

    按比例拉伸圖片,拉伸后圖片的高度為View的高度,且顯示在View的右邊

    Android ImageView圖片自適應

     

    </td> </tr>

    center

    </td>

    5

    </td>

    按原圖大小顯示圖片,但圖片寬高大于View的寬高時,截圖圖片中間部分顯示Android ImageView圖片自適應

     

    </td>

     

    layout_

    height

    :60px


    layout_

    width

    :80px


    padding

    :10px

     

    </td> </tr>

    centerCrop

    </td>

    6

    </td>

    按比例放大原圖直至等于某邊View的寬高顯示。Android ImageView圖片自適應

     

    </td> </tr>

    centerInside

    </td>

    7

    </td>

    當原圖寬高或等于View的寬高時,按原圖大小居中顯示;反之將原圖縮放至View的寬高居中顯示。Android ImageView圖片自適應

     

    </td> </tr> </tbody> </table> </td> </tr>

    android:src

    </td>

    設置Viewdrawable(如圖片,也可以是顏色,但是需要指定View的大小)

    </td> </tr>

    android:tint

    </td>

    將圖片渲染成指定的顏色。見下圖:

    Android ImageView圖片自適應 左邊為原圖,右邊為設置后的效果,見后面代碼。

    </td> </tr> </tbody> </table>
    原文地址:http://blog.sina.com.cn/s/blog_618199e60100y537.html

     本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
  • sesese色