Android在实际开发中很多时候都要对图片进行一定的处理,这里总结的BitmapUtils 类包括一下几个功能:

1.Android图片倒影,

2.Android图片模糊处理,

3.Android图片圆角处理,

4.图片沿着y轴旋转一定角度,

5.Android给图片添加边框。

接下来就直接上代码了,代码中有一定的解释。直接哪来用就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * @Title: BitmapUtils.java
 * @Copyright: Corporation. Ltd. Copyright 1998-2018, All rights reserved
 * @Description: TODO<请描述此文件是做什么的>
 * @author: xjp
 * @data: 2014年9月18日 上午10:10:30
 * @version: V1.0
 */
package com.mktech.bitmaputils;
 
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.Log;
 
/**
 * TODO<请描述这个类是干什么的>
 *
 * @author xjp
 * @data: 2014年9月18日 上午10:10:30
 * @version: V1.0
 */
public class BitmapUtils {
 
    private static final String TAG = "BitmapUtils";
     
    /**
     *
     * TODO<创建倒影图片>
     * @throw
     * @return Bitmap
     * @param srcBitmap 源图片的bitmap
     * @param reflectionHeight 图片倒影的高度
     */
    public static Bitmap createReflectedBitmap(Bitmap srcBitmap,int reflectionHeight) {
 
        if (null == srcBitmap) {
            Log.e(TAG, "the srcBitmap is null");
            return null;
        }
 
        // The gap between the reflection bitmap and original bitmap.
        final int REFLECTION_GAP = 0;
 
        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();
 
        if (0 == srcWidth || srcHeight == 0) {
            Log.e(TAG, "the srcBitmap is null");
            return null;
        }
 
        // The matrix
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
 
        try {
             
            // The reflection bitmap, width is same with original's, height is
            // half of original's.
            Bitmap reflectionBitmap = Bitmap.createBitmap(srcBitmap, 0, srcHeight - reflectionHeight,
                    srcWidth, reflectionHeight, matrix, false);
 
            if (null == reflectionBitmap) {
                Log.e(TAG, "Create the reflectionBitmap is failed");
                return null;
            }
 
            // Create the bitmap which contains original and reflection bitmap.
            Bitmap bitmapWithReflection = Bitmap.createBitmap(srcWidth, srcHeight + reflectionHeight,
                    Bitmap.Config.ARGB_8888);
 
            if (null == bitmapWithReflection) {
                return null;
            }
 
            // Prepare the canvas to draw stuff.
            Canvas canvas = new Canvas(bitmapWithReflection);
 
            // Draw the original bitmap.
            canvas.drawBitmap(srcBitmap, 0, 0, null);
 
            // Draw the reflection bitmap.
            canvas.drawBitmap(reflectionBitmap, 0, srcHeight + REFLECTION_GAP,
                    null);
 
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            LinearGradient shader = new LinearGradient(0, srcHeight, 0,
                    bitmapWithReflection.getHeight() + REFLECTION_GAP,
                    0x70FFFFFF, 0x00FFFFFF, TileMode.MIRROR);
            paint.setShader(shader);
            paint.setXfermode(new PorterDuffXfermode(
                    android.graphics.PorterDuff.Mode.DST_IN));
 
            canvas.save();
            // Draw the linear shader.
            canvas.drawRect(0, srcHeight, srcWidth,
                    bitmapWithReflection.getHeight() + REFLECTION_GAP, paint);
            if (reflectionBitmap != null && !reflectionBitmap.isRecycled()){
                reflectionBitmap.recycle();
                reflectionBitmap = null;
            }
             
            canvas.restore();
             
            return bitmapWithReflection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e(TAG, "Create the reflectionBitmap is failed");
        return null;
    }
     
    /**
     *
     * TODO<图片圆角处理>
     * @throw
     * @return Bitmap
     * @param srcBitmap 源图片的bitmap
     * @param ret 圆角的度数
     */
    public static Bitmap getRoundImage(Bitmap srcBitmap, float ret) { 
       
            if(null == srcBitmap){
                Log.e(TAG, "the srcBitmap is null");
                return null;
            }
             
            int bitWidth = srcBitmap.getWidth(); 
            int bitHight = srcBitmap.getHeight(); 
       
            BitmapShader bitmapShader = new BitmapShader(srcBitmap, 
                    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
            Paint paint = new Paint(); 
            paint.setAntiAlias(true); 
            paint.setShader(bitmapShader); 
       
            RectF rectf = new RectF(0, 0, bitWidth, bitHight); 
       
            Bitmap outBitmap = Bitmap.createBitmap(bitWidth, bitHight, 
                    Config.ARGB_8888); 
            Canvas canvas = new Canvas(outBitmap); 
            canvas.drawRoundRect(rectf, ret, ret, paint); 
            canvas.save();
            canvas.restore();
             
            return outBitmap; 
        
 
     
    /**
     *
     * TODO<图片沿着Y轴旋转一定角度>
     * @throw
     * @return Bitmap
     * @param srcBitmap 源图片的bitmap
     * @param reflectionHeight 图片倒影的高度
     * @param rotate 图片旋转的角度
     */
    public static Bitmap skewImage(Bitmap srcBitmap, float rotate, int reflectionHeight) {
 
        if(null == srcBitmap){
            Log.e(TAG, "the srcBitmap is null");
            return null;
        }
         
        Bitmap reflecteBitmap = createReflectedBitmap(srcBitmap, reflectionHeight);
         
        if (null == reflecteBitmap){
            Log.e(TAG, "failed to createReflectedBitmap");
            return null;
        }
         
        int wBitmap = reflecteBitmap.getWidth();
        int hBitmap = reflecteBitmap.getHeight();
        float scaleWidth = ((float) 180) / wBitmap;
        float scaleHeight = ((float) 270) / hBitmap;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        reflecteBitmap = Bitmap.createBitmap(reflecteBitmap, 0, 0, wBitmap, hBitmap, matrix,
                true);
        Camera localCamera = new Camera();
        localCamera.save();
        Matrix localMatrix = new Matrix();
        localCamera.rotateY(rotate);
        localCamera.getMatrix(localMatrix);
        localCamera.restore();
        localMatrix.preTranslate(-reflecteBitmap.getWidth() >> 1,
                -reflecteBitmap.getHeight() >> 1);
        Bitmap localBitmap2 = Bitmap.createBitmap(reflecteBitmap, 0, 0,
                reflecteBitmap.getWidth(), reflecteBitmap.getHeight(), localMatrix,
                true);
        Bitmap localBitmap3 = Bitmap.createBitmap(localBitmap2.getWidth(),
                localBitmap2.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(localBitmap3);
        Paint localPaint = new Paint();
        localPaint.setAntiAlias(true);
        localPaint.setFilterBitmap(true);
        localCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint);
        if (null != reflecteBitmap && !reflecteBitmap.isRecycled()) {
            reflecteBitmap.recycle();
            reflecteBitmap = null;
        }
        if (null != localBitmap2 && !localBitmap2.isRecycled()) {
            localBitmap2.recycle();
            localBitmap2 = null;
        }
        localCanvas.save();
        localCanvas.restore();
        return localBitmap3;
    }
     
     
    /**
     *
     * TODO<图片模糊化处理>
     * @throw
     * @return Bitmap
     * @param bitmap 源图片
     * @param radius The radius of the blur Supported range 0 < radius <= 25
     * @param context 上下文
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @SuppressLint("NewApi")
    public static Bitmap blurBitmap(Bitmap bitmap,float radius,Context context){ 
         
        //Let's create an empty bitmap with the same size of the bitmap we want to blur 
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
           
        //Instantiate a new Renderscript 
        RenderScript rs = RenderScript.create(context); 
           
        //Create an Intrinsic Blur Script using the Renderscript 
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); 
           
        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps 
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap); 
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); 
           
        //Set the radius of the blur 
        if(radius > 25){
            radius = 25.0f;
        }else if (radius <= 0){
            radius = 1.0f;
        }
        blurScript.setRadius(radius); 
           
        //Perform the Renderscript 
        blurScript.setInput(allIn); 
        blurScript.forEach(allOut); 
           
        //Copy the final bitmap created by the out Allocation to the outBitmap 
        allOut.copyTo(outBitmap); 
           
        //recycle the original bitmap 
        bitmap.recycle(); 
        bitmap = null;
        //After finishing everything, we destroy the Renderscript. 
        rs.destroy(); 
           
        return outBitmap; 
           
           
    }
     
     
    /**
     * TODO<给图片添加指定颜色的边框>
     * @param srcBitmap 原图片
     * @param borderWidth 边框宽度
     * @param color 边框的颜色值
     * @return
     */ 
    public static Bitmap addFrameBitmap(Bitmap srcBitmap,int borderWidth,int color) 
    
        if (srcBitmap == null){
            Log.e(TAG, "the srcBitmap or borderBitmap is null");
            return null;
        }
         
        int newWidth = srcBitmap.getWidth() + borderWidth ;
        int newHeight = srcBitmap.getHeight() + borderWidth ;
         
        Bitmap outBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);
         
        Canvas canvas = new Canvas(outBitmap);
         
        Rect rec = canvas.getClipBounds(); 
        rec.bottom--; 
        rec.right--; 
        Paint paint = new Paint(); 
        //设置边框颜色 
        paint.setColor(color); 
        paint.setStyle(Paint.Style.STROKE); 
        //设置边框宽度 
        paint.setStrokeWidth(borderWidth); 
        canvas.drawRect(rec, paint);
         
        canvas.drawBitmap(srcBitmap, borderWidth/2, borderWidth/2, null);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        if (srcBitmap !=null && !srcBitmap.isRecycled()){
            srcBitmap.recycle();
            srcBitmap = null;
        }
         
        return outBitmap;
    
       
}

以上代码经验证可以直接使用。

分享。

完结。

来自:http://www.2cto.com/kf/201409/335681.html

Android 中图可以用到的图片处理类 BitmapUtils的更多相关文章

  1. Android 中图能够用到的图片处理类 BitmapUtils

    Android在实际开发中非常多时候都要对图片进行一定的处理,这里总结的BitmapUtils 类包含一下几个功能: 1.Android图片倒影, 2.Android图片模糊处理, 3.Android ...

  2. Android中使用开源框架android-image-indicator实现图片轮播部署

    之前的博文中有介绍关于图片轮播的实现方式,分别为(含超链接): 1.<Android中使用ViewFlipper实现屏幕切换> 2.<Android中使用ViewPager实现屏幕页 ...

  3. android 中ImageButton按下改变背景图片的效果

    最近在做一个app的登陆界面,才发现原来认为很简单的UI效果,其实背后却蕴含的知识很多,积累一个算一个吧. 实现方法有两种:一种是添加代码,一种是配置xml文件. 方法一:代码添加 ImageButt ...

  4. 浅谈Android中拍照、从相册选择图片并截图相关知识点

    前言 我们在Android开发中经常会需要使用相机或者从相册中选取图片的情况,今天就把这里面相关的知识点总结下,方便以后开发的时候使用. 1.相机拍照并可自定义截图功能 我们先来看如何使用Intent ...

  5. android中图型的阴影效果(shadow-effect-with-custom-shapes)

    思路: 在自己定义shape中添加一层或多层,并错开.就可以显示阴影效果.为添加立体感,button按下的时候,仅仅设置一层.我们能够通过top, bottom, right 和 left 四个參数来 ...

  6. 关于android中调用系统拍照,返回图片是旋转90度

    转载博客:http://blog.csdn.net/walker02/article/details/8211628 项目开发中遇到的一个问题,对于三星手机在做手机照片选择时出现图片显示不正常,研究后 ...

  7. Android中的几种解析XML文件的类

    Ø DOM解析 优点: 1.XML树在内存中完整存储,因此可以直接修改其数据和结构. 2.可以通过该解析器随时访问XML树中的任何一个节点. 3.DOM解析器的API在使用上也相对比较简单. 缺点:如 ...

  8. Android开发之使用Handler封装下载图片工具类(源码分享)

    假设每下载一张图片,就得重写一次Http协议,多线程的启动和handler的信息传递就显得太麻烦了,我们直接来封装一个工具类,便于我们以后在开发时随时能够调用. (1)在清单文件加入权限 <us ...

  9. Android中SQLite数据库操作(2)——SQLiteOpenHelper类

    如果开发者对SQL语法不熟悉,我要告诉你一个好消息,Android提供了一个SQLiteOpenHelper类. 在实际项目中很少使用SQLiteDatabase的方法(请看:http://blog. ...

随机推荐

  1. 控制nginx并发链接数量和客户端请求nginx的速率

    一.控制nginx并发链接数 ngx_http_limit_conn_module这个模块用于限制每个定义的key值的链接数,特别是单IP的链接数. 不是所有的链接数都会被计数,一个符合计数要求的连接 ...

  2. 使用GD库做图片水印

    png图片作为水印加到其他类型图片后,背景变黑色 原因: imagecopy函数拷贝时可以保留png图像的原透明信息,而imagecopymerge却不支持图片的本身的透明拷贝. 然后直接上代码: / ...

  3. Python头脑风暴2

    今天想到了一个致富新途径:假如我在X东上班,我写个X宝爬虫,专门爬在X宝买奢侈品的土豪,然后我自己注册个X宝号,用脚本一个个加他们然后给他们发信息说我X东这还有比你更便宜更好的...不知道行不行啊(狗 ...

  4. Linux中同步与异步、阻塞与非阻塞概念以及五种IO模型

    1.概念剖析 相信很多从事linux后台开发工作的都接触过同步&异步.阻塞&非阻塞这样的概念,也相信都曾经产生过误解,比如认为同步就是阻塞.异步就是非阻塞,下面我们先剖析下这几个概念分 ...

  5. Solution: 最近公共祖先·一 [hiho一下 第十三周]

    题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...

  6. PHP GD库---之微信朋友圈9张图

    $item_pic = "img/item.jpg"; list($width, $height) = getimagesize($item_pic); $item_pic = i ...

  7. LoadRunner 11破解方法

    名称:HP Loadrunner Software 11.00 版本号:11.00.0.0 安装环境:Win 7 软件安装成功后,会弹出提示告知license的有效期为10天. 破解方法: 1.下载破 ...

  8. Selenium WebDriver-通过ActionChains实现页面元素拖拽

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  9. Maven项目下Tomcat插件选择方法

    1. 进入Tomcat官网:http://tomcat.apache.org/ 选择Maven plugin 2. 选择版本 3. 查看版本对应的插件版本: 有两种方式添加:如下图所示:

  10. java EE技术体系——CLF平台API开发注意事项(2)——后端测试

    前言:上篇博客说到了关于开发中的一些情况,这篇博客主要说明一些关于测试的内容. 一.宏观说明 要求:每一个API都必须经过测试.   备注:如果涉及到服务间调用(如权限和基础数据),而对方服务不可用时 ...