美文网首页
AdjustBitmap

AdjustBitmap

作者: Bennys | 来源:发表于2018-06-22 16:23 被阅读7次

public class AdjustBitmap {

/**

     * 将bitmap调整到指定大小

     */

    public static BitmapsizeBitmap(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;

    }

/**

*

     * @param origin

     * @param scale

     * @return 按比例缩放

     */

    public static BitmapscaleBitmap(Bitmap origin, float scale) {

if (origin ==null) {

return null;

        }

int width = origin.getWidth();

        int height = origin.getWidth();

        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;

    }

/**

*

     * @param bitmap

     * @return 从中间截取一个正方形

     */

    public static BitmapcropBitmap(Bitmap bitmap) {

// 得到图片的宽,高

        int w = bitmap.getWidth();

        int h = bitmap.getHeight();

        // 裁切后所取的正方形区域边长

        int cropWidth = w >= h ? h : w;

        return Bitmap.createBitmap(bitmap, 0,

                0, cropWidth, cropWidth);

    }

/**

*

     * @param bitmap

     * @param sx 2

     * @param sy 3

     * @return 返回一个截取的图形

     */

    public static BitmapcropBitmap(Bitmap bitmap,int sx,int sy) {

// 得到图片的宽,高

        int w = bitmap.getWidth();//600 900

        int h = bitmap.getHeight();//900 600

        int startWidth=0;

        int startHeight=0;

        int x=0;

        int y=0;

        int z=0;

        // 裁切后所取的正方形区域边长

        if (sx==sy) {

x = w >= h ? h : w;

            y=x;

        }else if (sx>sy){

//横 长方型

            if (w>h){//331:240 3:2

               z=h/sx;

            }else {//240:331  3:2

                z=x/sx;

            }

x=sx*z;

            y=sy*z;

        }else {

//竖型 长方型

            if (w>h){//331:240 2:3

                z=h/sy;

            }else {//240:331  2:3

                z=x/sy;

            }

x=sx*z;

            y=sy*z;

        }

return Bitmap.createBitmap(bitmap, startWidth,

                startHeight, x, y);

    }

/**

*

     * @param bitmap

     * @return 把图片裁剪成圆形

     */

    public static BitmapgetCircleBitmap(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;

        }

}

public static BitmapdrawableToBitmap(Drawable drawable) {

// 取 drawable 的长宽

        int w = drawable.getIntrinsicWidth();

        int h = drawable.getIntrinsicHeight();

        // 取 drawable 的颜色格式

        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

                : Bitmap.Config.RGB_565;

        // 建立对应bitmap

        Bitmap bitmap = Bitmap.createBitmap(w, h, config);

        // 建立对应 bitmap 的画布

        Canvas canvas =new Canvas(bitmap);

        drawable.setBounds(0, 0, w, h);

        // 把 drawable 内容画到画布中

        drawable.draw(canvas);

        return bitmap;

    }

}

相关文章

网友评论

      本文标题:AdjustBitmap

      本文链接:https://www.haomeiwen.com/subject/qmnkyftx.html