編輯:Android 技術基礎
這一節介紹 UI 基礎控件 ImageView(圖像視圖),顧名思義,就是用來顯示圖像的 View。官方 API:ImageView。本節內容包括:
API 文檔裡 ImageView 有兩個能設置圖片的屬性:src 和 background。常識:
① background 通常指背景,src 指內容。
② 用 src 填入圖片時,按圖片原始大小直接填充,不會拉伸;而用 background 填圖,則會根據 ImageView 給定的寬度進行拉伸。
寫個簡單布局測一下:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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>
效果圖:

結果分析: 寬高都是 wrap_content 時兩者一樣,都是原圖大小。但一旦固定了寬或高,差別就明顯了:background 完全填滿整個 ImageView,而 src 依舊保持原尺寸,並且居中——這就涉及 ImageView 的另一個屬性 scaleType 了。另外要注意,這裡我們只設了 width 或 height 其中一個。如果同時設了 width 和 height,background 依然填滿,但 src 的大小可能發生改變。比如測一下:
<ImageView android:layout_width="100dp" android:layout_height="50dp" android:src="@drawable/pen" />
運行效果圖:

PS:scaleType 下面會講。
前面效果圖裡的第二個 ImageView,圖片被拉伸變形了,正方形變成了長方形,對有輕微強迫症的人來說顯然不可接受。有沒有辦法解決?當然有,筆者目前知道兩種:
適用於動態加載 ImageView 的場景,代碼很簡單,添加 View 時把尺寸寫死即可:
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(48, 48); layout.addView(ibtnPen, layoutParam);
更多時候我們通過 XML 布局引入 ImageView,這時可以用 drawable 的 Bitmap 資源文件來解決,再把 background 指向該文件即可。這個 xml 文件要在 drawable 文件夾下創建(文件夾需自己建)。
pen_bg.xml:
<bitmap xmlns:android="https://schemas.android.com/apk/res/android" android:id="@id/pen_bg" android:gravity="top" android:src="@drawable/pen" android:tileMode="disabled" > </bitmap>
這段代碼不難理解,最容易犯迷糊的是 tileMode 屬性——它是「平鋪」,就像 Windows 設置桌面背景時的平鋪,用多個小圖鋪滿整個區域;disabled 就是把它禁用。調用方式:
動態:ibtnPen.setBackgroundResource(R.drawable.penbg);
靜態:android:background = "@drawable/penbg"
再說下 setAlpha 屬性,很簡單——只有 src 時才有效果!!
網上的一張圖:

乍看是個簡單的 GridView,每個 item 都是 ImageView,但細看會發現上面的圖標並不規則,而是圓形、圓角矩形等形狀。這裡就用到了 src + background 的組合。要實現這種效果,只需兩步:找一張透明的 png 圖片 + 設置一個背景色(也可以調整 png 的透明度來實現,但結果可能與預想有出入)。寫個簡單例子:

如圖,呆萌的小豬就這樣顯示到 ImageView 上了,background 設了藍色背景!
實現代碼:
<ImageView android:layout_width="150dp" android:layout_height="wrap_content" android:src="@drawable/pig" android:background="#6699FF" />
PS:當然你也可以用 selector 實現點擊效果,為不同狀態設置不同圖片,做出點擊或觸摸反饋。
前景(對應 src 屬性):setImageDrawable( );
背景(對應 background 屬性):setBackgroundDrawable( );
ImageView 提供了 adjustViewBounds 屬性,用於設置縮放時是否保持原圖長寬比。單獨設置不起作用,需要配合 maxWidth 和 maxHeight 一起使用;而後兩者也要 adjustViewBounds 為 true 才會生效。
- android:maxHeight:設置 ImageView 的最大高度
- android:maxWidth:設置 ImageView 的最大寬度
代碼示例:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- 正常的圖片 --> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" android:src="@mipmap/meinv" /> <!-- 限制了最大寬度與高度,並且設置了調整邊界來保持所顯示圖像的長寬比--> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px" android:adjustViewBounds="true" android:maxHeight="200px" android:maxWidth="200px" android:src="@mipmap/meinv" /> </LinearLayout>
運行效果圖:

結果分析: 上面那張是未經處理的原圖,尺寸 541×374;下面那張我們用 maxWidth 和 maxHeight 把 ImageView 限制在最大 200px,即最多顯示 200×200,同時設了 adjustViewBounds = "true" 來保持長寬比,此時 ImageView 實際寬高為 128×200。
android:scaleType 用於設置圖片如何縮放或移動以適應 ImageView 大小,Java 代碼中可通過 imageView.setScaleType(ImageView.ScaleType.CENTER); 設置。可選值如下:
- fitXY:對圖像橫向與縱向獨立縮放,使圖片完全適應 ImageView,但橫縱比可能改變
- fitStart:保持縱橫比縮放,直到較長的邊與 ImageView 的邊相等,縮放後把圖片放在左上角
- fitCenter:同上,縮放後放於中間
- fitEnd:同上,縮放後放於右下角
- center:保持原圖大小,顯示在中心。原圖大於 ImageView 時,超出部分裁剪
- centerCrop:保持橫縱比縮放,直到完全覆蓋 ImageView,可能導致圖片顯示不全
- centerInside:保持橫縱比縮放,直到 ImageView 能完整顯示圖片
- matrix:默認值,不改變原圖大小,從左上角開始繪製,超出部分裁剪
下面分組對比:
以 fitEnd 為例,其他兩個類似:
<!-- 保持圖片的橫縱比縮放,知道該圖片能夠顯示在ImageView組件上,並將縮放好的圖片顯示在imageView的右下角 --> <ImageView android:id="@+id/imageView3" android:layout_width="300px" android:layout_height="300px" android:layout_margin="5px" android:scaleType="fitEnd" android:src="@mipmap/meinv" />
運行效果圖:
示例代碼:
<ImageView android:layout_width="300px" android:layout_height="300px" android:layout_margin="5px" android:scaleType="centerCrop" android:src="@mipmap/meinv" /> <ImageView android:layout_width="300px" android:layout_height="300px" android:layout_margin="5px" android:scaleType="centerInside" android:src="@mipmap/meinv" />
運行效果圖:

不按比例縮放,目標是把圖片塞滿整個 View。
示例代碼:
<ImageView android:layout_width="300px" android:layout_height="300px" android:layout_margin="5px" android:scaleType="fitXY" android:src="@mipmap/meinv" />
運行效果圖:
好吧,明顯被壓扁了。
從 ImageView 左上角開始繪製原圖,超出部分裁剪。
示例代碼:
<ImageView android:layout_width="300px" android:layout_height="300px" android:layout_margin="5px" android:scaleType="matrix" android:src="@mipmap/meinv" />
運行效果圖:

保持原圖大小,顯示在中心。原圖大於 ImageView 時,超出部分裁剪。
示例代碼:
<ImageView android:layout_width="300px" android:layout_height="300px" android:layout_margin="5px" android:scaleType="center" android:src="@mipmap/meinv" />
運行效果圖:
現在很多 App 都愛用圓形頭像,這裡就簡單寫一個圓形 ImageView——注意,這只是拋磚引玉,並未考慮性能與抗鋸齒。實際項目建議直接用 GitHub 上牛人寫好的控件,比如 RoundedImageView、CircleImageView(git 怎麼用前面講過,clone 下來把相關文件複製進項目即可)。
運行效果圖:
實現代碼——自定義 ImageView:RoundImageView.java
package com.jay.demo.imageviewdemo; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PaintFlagsDrawFilter; import android.graphics.Path; import android.graphics.Rect; import android.graphics.Region; import android.util.AttributeSet; import android.widget.ImageView; /** * Created by coder-pig on 2015/7/18 0018. */ public class RoundImageView extends ImageView { private Bitmap mBitmap; private Rect mRect = new Rect(); private PaintFlagsDrawFilter pdf = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG); private Paint mPaint = new Paint(); private Path mPath=new Path(); public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } //傳入一個Bitmap對象 public void setBitmap(Bitmap bitmap) { this.mBitmap = bitmap; } private void init() { mPaint.setStyle(Paint.Style.STROKE); mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); mPaint.setAntiAlias(true);// 抗鋸尺 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(mBitmap == null) { return; } mRect.set(0,0,getWidth(),getHeight()); canvas.save(); canvas.setDrawFilter(pdf); mPath.addCircle(getWidth() / 2, getWidth() / 2, getHeight() / 2, Path.Direction.CCW); canvas.clipPath(mPath, Region.Op.REPLACE); canvas.drawBitmap(mBitmap, null, mRect, mPaint); canvas.restore(); } }
布局代碼 activity_main.xml:
<com.jay.demo.imageviewdemo.RoundImageView android:id="@+id/img_round" android:layout_width="200dp" android:layout_height="200dp" android:layout_margin="5px"/>
MainActivity.java:
package com.jay.demo.imageviewdemo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private RoundImageView img_round; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img_round = (RoundImageView) findViewById(R.id.img_round); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.meinv); img_round.setBitmap(bitmap); } }
本節講解了 ImageView,內容看著多,其實多為細節性的東西,知道即可。最後那個自定義圓形 ImageView 也只是寫來玩,實際項目還是建議用成熟的三方控件。
Android 佈局入門:LinearLayout 線性佈局與 XML/Java 雙寫法
從 Hello world! 開始,我們示範的畫面其實都離不開同一種佈局。對基礎練習來說這沒問題,但真正做產品時—— 在 Android 開發中,UI 設計的重要性不容
Fragment 基本觀念完整整理:生命週期、靜態動態載入與資料溝通
Fragment 是什麼?Fragment 是 Android 3.0 後引入的介面元件,可理解為 Activity 中的一個片段或模組。它能讓開發者把複雜畫面拆成多個區
WebView 緩存機制全解析:開啟、模式選擇與清理
本節引言 如今不少門戶資訊類應用,比如虎嗅、ifanr、鈦媒體等的 App,本質上都是內嵌一個 WebView 來展示相關內容,這就自然牽扯到 WebView 的緩存問題
第89章:系統服務之 SMS 服務——寫一封有溫度的群發短信(從零開始學 Android)
每逢佳節,電信三雄大概都在偷偷笑——又不知道有多少短信費進了口袋。儘管微信、QQ、飛信滿天飛,節日祝福短信的人氣卻始終擋不住。但千篇一律的群發實在沒什麼意義,你是不是也想