Android开发ImageUtils工具演示

该工具提供缩放   drawable转换bitmap   转换倒影图     转换成圆角图

  1 package com.nailsoul.imagedemo.utils;
  2 
  3 import android.graphics.Bitmap;
  4 import android.graphics.Canvas;
  5 import android.graphics.LinearGradient;
  6 import android.graphics.Matrix;
  7 import android.graphics.Paint;
  8 import android.graphics.PixelFormat;
  9 import android.graphics.PorterDuffXfermode;
 10 import android.graphics.Rect;
 11 import android.graphics.RectF;
 12 import android.graphics.Bitmap.Config;
 13 import android.graphics.PorterDuff.Mode;
 14 import android.graphics.Shader.TileMode;
 15 import android.graphics.drawable.Drawable;
 16 
 17 public class ImageUtil {
 18 
 19     /**
 20      *  放大缩小图片
 21      * @param bitmap 要放大的图片
 22      * @param dstWidth 目标宽
 23      * @param dstHeight 目标高
 24      * @return
 25      */
 26     public static Bitmap zoomBitmap(Bitmap bitmap, int dstWidth, int dstHeight) {
 27         int width = bitmap.getWidth();
 28         int height = bitmap.getHeight();
 29         Matrix matrix = new Matrix();
 30         float scaleWidht = ((float) dstWidth / width);
 31         float scaleHeight = ((float) dstHeight / height);
 32         matrix.postScale(scaleWidht, scaleHeight);
 33         Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
 34                 matrix, true);
 35         return newbmp;
 36     }
 37 
 38     /**
 39      * 将Drawable转化为Bitmap
 40      * @param drawable
 41      * @return
 42      */
 43     public static Bitmap drawableToBitmap(Drawable drawable) {
 44         int width = drawable.getIntrinsicWidth();
 45         int height = drawable.getIntrinsicHeight();
 46         Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
 47                 .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
 48                 : Bitmap.Config.RGB_565);
 49         Canvas canvas = new Canvas(bitmap);
 50         drawable.setBounds(0, 0, width, height);
 51         drawable.draw(canvas);
 52         return bitmap;
 53 
 54     }
 55 
 56     /**
 57      * 获得圆角图片的方法
 58      * @param bitmap 
 59      * @param roundPx  4脚幅度
 60      * @return
 61      */
 62     public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
 63 
 64         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
 65                 bitmap.getHeight(), Config.ARGB_8888);
 66         Canvas canvas = new Canvas(output);
 67 
 68         final int color = 0xff424242;
 69         final Paint paint = new Paint();
 70         final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
 71         final RectF rectF = new RectF(rect);
 72 
 73         paint.setAntiAlias(true);
 74         canvas.drawARGB(0, 0, 0, 0);
 75         paint.setColor(color);
 76         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
 77 
 78         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
 79         canvas.drawBitmap(bitmap, rect, rect, paint);
 80 
 81         return output;
 82     }
 83 
 84     /**
 85      * 获得带倒影的图片方法
 86      * @param bitmap
 87      * @return
 88      */
 89     public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
 90         final int reflectionGap = 4;
 91         int width = bitmap.getWidth();
 92         int height = bitmap.getHeight();
 93 
 94         Matrix matrix = new Matrix();
 95         matrix.preScale(1, -1);
 96 
 97         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
 98                 width, height / 2, matrix, false);
 99 
100         Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
101                 (height + height / 2), Config.ARGB_8888);
102 
103         Canvas canvas = new Canvas(bitmapWithReflection);
104         canvas.drawBitmap(bitmap, 0, 0, null);
105         Paint deafalutPaint = new Paint();
106         canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
107 
108         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
109 
110         Paint paint = new Paint();
111         LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
112                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
113                 0x00ffffff, TileMode.CLAMP);
114         paint.setShader(shader);
115         // Set the Transfer mode to be porter duff and destination in
116         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
117         // Draw a rectangle using the paint with our linear gradient
118         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
119                 + reflectionGap, paint);
120 
121         return bitmapWithReflection;
122     }
123 
124 }
 1 package com.nailsoul.imagedemo;
 2 
 3 import java.util.Random;
 4 
 5 import android.app.Activity;
 6 import android.graphics.Bitmap;
 7 import android.graphics.drawable.Drawable;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.view.Window;
11 import android.view.WindowManager;
12 import android.widget.ImageView;
13 import android.widget.TextView;
14 
15 import com.nailsoul.imagedemo.utils.ImageUtil;
16 
17 public class ImageDemoActivity extends Activity {
18     private ImageView mIv01, mIv02,mIv03;
19     private TextView mTv01, mtv02,mtv03,mtv00;
20 
21     public void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN;
24         getWindow().setFlags(flag,flag);
25         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
26         setContentView(R.layout.main);
27         setupViews();
28     }
29 
30     private void setupViews() {
31         mIv01 = findView(R.id.image01);
32         mIv02 = findView(R.id.image02);
33         mIv03 = findView(R.id.image03);
34         mtv00=findView(R.id.text00);
35         mTv01=findView(R.id.text01);
36         mtv02=findView(R.id.text02);
37         mtv03=findView(R.id.text03);
38 
39         // 获取壁纸返回值是Drawable
40 //        Drawable drawable = getWallpaper();
41         Drawable drawable=getResources().getDrawable(R.drawable.a1+new Random().nextInt(15));
42         // 将Drawable转化为Bitmap
43         Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);
44         // 缩放图片
45         Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 355, 317);
46         // 获取圆角图片
47         Bitmap roundBitmap = ImageUtil
48                 .getRoundedCornerBitmap(bitmap, 10.0f);
49         // 获取倒影图片
50         Bitmap reflectBitmap = ImageUtil
51                 .createReflectionImageWithOrigin(zoomBitmap);
52         // 这里可以让Bitmap再转化为Drawable
53         // Drawable roundDrawable = new BitmapDrawable(roundBitmap);
54         // Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);
55         // mImageView01.setBackgroundDrawable(roundDrawable);
56         // mImageView02.setBackgroundDrawable(reflectDrawable);
57 
58         mIv01.setImageBitmap(roundBitmap);
59         mIv02.setImageBitmap(reflectBitmap);
60         mIv03.setImageBitmap(zoomBitmap);
61         mtv00.setText("ImageUtils演示");
62         mTv01.setText("上图为圆角图   "+roundBitmap.getWidth()+"height:"+roundBitmap.getHeight());
63         mtv02.setText("上图为倒影图   "+reflectBitmap.getWidth()+"height:"+reflectBitmap.getHeight());
64         mtv03.setText("上图为缩放图   "+zoomBitmap.getWidth()+"height:"+zoomBitmap.getHeight());
65     }
66     
67     
68 
69     public <T> T findView(int id){
70         return (T) super.findViewById(id);
71     }
72 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <ScrollView
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent"
10         android:scrollbars="vertical" >
11 
12         <LinearLayout
13             android:layout_width="fill_parent"
14             android:layout_height="fill_parent"
15             android:orientation="vertical" >
16 
17             <TextView
18                 android:id="@+id/text00"
19                 android:layout_width="fill_parent"
20                 android:layout_height="wrap_content"
21                 android:gravity="center" />
22 
23             <ImageView
24                 android:id="@+id/image01"
25                 android:layout_width="wrap_content"
26                 android:layout_height="wrap_content"
27                 android:padding="10px" />
28 
29             <TextView
30                 android:id="@+id/text01"
31                 android:layout_width="fill_parent"
32                 android:layout_height="wrap_content"
33                 android:gravity="center" />
34 
35             <ImageView
36                 android:id="@+id/image02"
37                 android:layout_width="wrap_content"
38                 android:layout_height="wrap_content"
39                 android:padding="10px" />
40 
41             <TextView
42                 android:id="@+id/text02"
43                 android:layout_width="fill_parent"
44                 android:layout_height="wrap_content"
45                 android:gravity="center" />
46 
47             <ImageView
48                 android:id="@+id/image03"
49                 android:layout_width="wrap_content"
50                 android:layout_height="wrap_content"
51                 android:padding="10px" />
52 
53             <TextView
54                 android:id="@+id/text03"
55                 android:layout_width="fill_parent"
56                 android:layout_height="wrap_content"
57                 android:gravity="center" />
58         </LinearLayout>
59     </ScrollView>
60 
61 </LinearLayout>

来源:http://www.open-open.com/lib/view/open1382151866948.html

原文地址:https://www.cnblogs.com/bxfx111/p/3584827.html