ImageView 使用詳解

和你一起終身學習,這里是程序員Android
經(jīng)典好文推薦,通過閱讀本文,您將收獲以下知識點:
一、ImageView 的繼承關(guān)系
二、ImageView 常用方法
三、ImageView 背景 間距屬性設(shè)置
四、使用Bitmap 類型動態(tài)設(shè)置ImageView 資源
五、ImageView 圖片倒影實現(xiàn)
六、ImageView 圖片縮放實現(xiàn)
七、ImageView 圓角圖片實現(xiàn)
八、Bitmap 與Drawable 轉(zhuǎn)換工具類
一、ImageView 的繼承關(guān)系
ImageView的繼承關(guān)系 如下:
java.lang.Object
? android.view.View
? android.widget.ImageView
二、ImageView 常用方法
ImageView主要用于顯示圖像資源,Bitmap或Drawable資源,同時也常用于圖片渲染調(diào)色,圖片縮放剪裁等。
以下XML代碼段是使用ImageView顯示圖像資源的常見示例:
1. 在xml使用ImageView?控件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
LinearLayout>
三、 ImageView 背景 間距屬性設(shè)置
1. 在xml使用ImageView控件
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey"
android:padding="5dp"
android:src="@drawable/ic_launcher" />
2. 實現(xiàn)效果如下:

padding background 屬性設(shè)置
四、 使用Bitmap 類型動態(tài)設(shè)置ImageView 資源
1. 在xml?使用ImageView控件
<ImageView
android:id="@+id/img_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
2.java?類實現(xiàn)
// 1.從資源中獲取Bitmap
ImageView mImageView1 = (ImageView) findViewById(R.id.img_1);
DrawableUtils.UseBitmap(this, mImageView1, R.drawable.gril);
3.DrawableUtils類方法實現(xiàn)
// 1.從資源中獲取Bitmap
public static void UseBitmap(Context context, ImageView imageView, int drawableId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
drawableId);
imageView.setImageBitmap(bitmap);
}
4. 實現(xiàn)效果如下:

bitmap 類型的圖片
五、ImageView 圖片倒影實現(xiàn)
1. 在xml使用ImageView?控件
<ImageView
android:id="@+id/img_4"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/grey"
android:padding="5dp" />
2.?java代碼 實現(xiàn)效果
// 4.倒影圖片
ImageView mImageView4 = (ImageView) findViewById(R.id.img_4);
mImageView4.setImageBitmap(DrawableUtils.CreateReflectionImageWithOrigin(
DrawableUtils.DrawableToBitmap(getResources().getDrawable(
R.drawable.img1))));
3.?DrawableUtils?工具類的方法實現(xiàn)
// 5. Drawable----> Bitmap
public static Bitmap DrawableToBitmap(Drawable drawable) {
// 獲取 drawable 長寬
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
// 獲取drawable的顏色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 創(chuàng)建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 創(chuàng)建bitmap畫布
Canvas canvas = new Canvas(bitmap);
// 將drawable 內(nèi)容畫到畫布中
drawable.draw(canvas);
return bitmap;
}
4. 實現(xiàn)效果如下:

ImageView 倒影功能實現(xiàn)
六、ImageView 圖片縮放實現(xiàn)
1. 在xml使用ImageView?控件
<ImageView
android:id="@+id/img_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
2.?java代碼 實現(xiàn)效果
// 2. 圖片縮放
ImageView mImageView2 = (ImageView) findViewById(R.id.img_2);
mImageView2.setImageDrawable(DrawableUtils.ZoomDrawable(getResources().getDrawable(R.drawable.img1),
240, 200));
3.?DrawableUtils?工具類方法實現(xiàn)
// 9. drawable進行縮放 ---> bitmap 然后比對bitmap進行縮放
public static Drawable ZoomDrawable(Drawable drawable, int w, int h) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// 調(diào)用5 中 drawable轉(zhuǎn)換成bitmap
Bitmap oldbmp = DrawableToBitmap(drawable);
// 創(chuàng)建操作圖片用的Matrix對象
Matrix matrix = new Matrix();
// 計算縮放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 設(shè)置縮放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其內(nèi)容是對原bitmap的縮放后的圖
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(newbmp);
}
4. 實現(xiàn)效果如下:

Imageview 縮放
七、ImageView 圓角圖片 實現(xiàn)
1. 在xml使用ImageView?控件
<ImageView
android:id="@+id/img_3"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/grey"
android:padding="5dp" />
2.java代碼 實現(xiàn)效果
// 3. 圓角圖片
ImageView mImageView3 = (ImageView) findViewById(R.id.img_3);
mImageView3.setImageBitmap(DrawableUtils.SetRoundCornerBitmap(
DrawableUtils.DrawableToBitmap(getResources().getDrawable(
R.drawable.img1)), 60));
3.?DrawableUtils工具類方法實現(xiàn)
// 6.圓角圖片
public static Bitmap SetRoundCornerBitmap(Bitmap bitmap, float roundPx) {
int width = bitmap.getWidth();
int heigh = bitmap.getHeight();
// 創(chuàng)建輸出bitmap對象
Bitmap outmap = Bitmap.createBitmap(width, heigh,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outmap);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, heigh);
final RectF rectf = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectf, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return outmap;
}
4. 實現(xiàn)效果如下:

圓角圖片實現(xiàn)
八、Bitmap與Drawable?轉(zhuǎn)換工具類
Bitmap與Drawable?轉(zhuǎn)換常用工具類源代碼如下:
public class DrawableUtils {
// 1.從資源中獲取Bitmap
public static void UseBitmap(Context context, ImageView imageView, int drawableId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
drawableId);
imageView.setImageBitmap(bitmap);
}
// 2.Bitmap ---> byte[]
public byte[] BitmapToBytes(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
// 3.byte[] ---->bitmap
public Bitmap BytesToBitmap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
// 4.Bitmap 縮放方法
public static Bitmap ZoomBitmap(Bitmap bitmap, int width, int heigh) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scalewidth = (float) width / w;
float scaleheigh = (float) heigh / h;
matrix.postScale(scalewidth, scaleheigh);
Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newBmp;
}
// 5. Drawable----> Bitmap
public static Bitmap DrawableToBitmap(Drawable drawable) {
// 獲取 drawable 長寬
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
// 獲取drawable的顏色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 創(chuàng)建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 創(chuàng)建bitmap畫布
Canvas canvas = new Canvas(bitmap);
// 將drawable 內(nèi)容畫到畫布中
drawable.draw(canvas);
return bitmap;
}
// 6.圓角圖片
public static Bitmap SetRoundCornerBitmap(Bitmap bitmap, float roundPx) {
int width = bitmap.getWidth();
int heigh = bitmap.getHeight();
// 創(chuàng)建輸出bitmap對象
Bitmap outmap = Bitmap.createBitmap(width, heigh,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outmap);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, heigh);
final RectF rectf = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectf, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return outmap;
}
// 7.獲取帶倒影的圖片
public static Bitmap CreateReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGapLine = 4;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
h / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, h, w, h + reflectionGapLine, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, h + reflectionGapLine, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGapLine, 0x70ffffff,
0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
+ reflectionGapLine, paint);
return bitmapWithReflection;
}
// 8. bitmap ---Drawable
public static Drawable BitmapToDrawable(Bitmap bitmap, Context context) {
BitmapDrawable drawbale = new BitmapDrawable(context.getResources(),
bitmap);
return drawbale;
}
// 9. drawable進行縮放 ---> bitmap 然后比對bitmap進行縮放
public static Drawable ZoomDrawable(Drawable drawable, int w, int h) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// 調(diào)用5 中 drawable轉(zhuǎn)換成bitmap
Bitmap oldbmp = DrawableToBitmap(drawable);
// 創(chuàng)建操作圖片用的Matrix對象
Matrix matrix = new Matrix();
// 計算縮放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 設(shè)置縮放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其內(nèi)容是對原bitmap的縮放后的圖
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(newbmp);
}
}至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺得很優(yōu)秀,歡迎點擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請聯(lián)系小編刪除。同時感謝您的閱讀,期待您的關(guān)注。
點個在看,方便您使用時快速查找!
評論
圖片
表情
