代码是平常使用时收集的,并不是本人所写。这些bitmap处理方法在我的android程序里面使用相对较多,希望对大家也有些帮助。

import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;public class AdjustBitmap {    //将bitmap调整到指定大小    public static Bitmap sizeBitmap(Bitmap origin, int newWidth, int newHeight) {        if (origin == null) {            return null;        }        int height = origin.getHeight();        int width = origin.getWidth();        float scaleWidth = ((float) newWidth) / width;        float scaleHeight = ((float) newHeight) / height;        Matrix matrix = new Matrix();        matrix.postScale(scaleWidth, scaleHeight);// 使用后乘        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);        if (!origin.isRecycled()) {//这时候origin还有吗?            origin.recycle();        }        return newBM;    }    //按比例缩放    public static Bitmap scaleBitmap(Bitmap origin, float scale) {        if (origin == null) {            return null;        }        int width = origin.getWidth();        int height = origin.getHeight();        Matrix matrix = new Matrix();        matrix.preScale(scale, scale);        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);        if (newBM.equals(origin)) {            return newBM;        }        origin.recycle();        return newBM;    }    public static Bitmap cropBitmap(Bitmap bitmap) {//从中间截取一个正方形        int w = bitmap.getWidth(); // 得到图片的宽,高        int h = bitmap.getHeight();        int cropWidth = w >= h ? h : w;// 裁切后所取的正方形区域边长        return Bitmap.createBitmap(bitmap, (bitmap.getWidth() - cropWidth) / 2,                (bitmap.getHeight() - cropWidth) / 2, cropWidth, cropWidth);    }    public static Bitmap getCircleBitmap(Bitmap bitmap) {//把图片裁剪成圆形        if (bitmap == null) {            return null;        }        bitmap = cropBitmap(bitmap);//裁剪成正方形        try {            Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);            Canvas canvas = new Canvas(circleBitmap);            final Paint paint = new Paint();            final Rect rect = new Rect(0, 0, bitmap.getWidth(),                    bitmap.getHeight());            final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),                    bitmap.getHeight()));            float roundPx = 0.0f;            roundPx = bitmap.getWidth();            paint.setAntiAlias(true);            canvas.drawARGB(0, 0, 0, 0);            paint.setColor(Color.WHITE);            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));            final Rect src = new Rect(0, 0, bitmap.getWidth(),                    bitmap.getHeight());            canvas.drawBitmap(bitmap, src, rect, paint);            return circleBitmap;        } catch (Exception e) {            return bitmap;        }    }}

更多相关文章

  1. android中加载图片时出现oom
  2. Android(安卓)图片处理方法大全
  3. 记一次Android(安卓)OOM探险之旅
  4. Android中使用gridview如何让图片在上文字在下
  5. android 让ImageView的图片全屏填充
  6. Android(安卓)调用相册 拍照 实现系统控件缩放 切割图片
  7. Android(安卓)将View 转化为bitmap 图片
  8. Android使用控件ImageView加载图片的方法
  9. Android调用系统图库

随机推荐

  1. Android(安卓)混淆打包
  2. Android中ListView中显示图片和文本
  3. Android(安卓)P SystemUI之StatusBar加载
  4. Android内核源码交叉编译
  5. Android中丰富多彩的onTouch事件
  6. android > 禁止横竖屏切换
  7. android布局ui
  8. 四、 Android之手机屏幕朝向
  9. 那些年收藏的技术文章(一)-CSDN篇
  10. Android:EditText 常用属性