我的CircleView

我的CircleView

Drawable转BitMap


    /**
     *Drawable转Bitmap
     * @param drawable
     * @return bitmap
     */
    public static Bitmap getBmpFormDraw(Drawable drawable) {
        Bitmap bitmap;
        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(
                    COLORDRAWABLE_DIMENSION,
                    COLORDRAWABLE_DIMENSION,
                    Bitmap.Config.ARGB_8888);
        } else {
            bitmap = Bitmap.createBitmap(
                    drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(),
                    drawable.getOpacity() != PixelFormat.OPAQUE ?
                            Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        }
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        return bitmap;
    }

重新设置bitmap的大小

 /**
     *重新设置bitmap大小
     * @param bitmap 处理之前的图片
     * @param w bitmap处理之后的宽度
     * @param h bitmap处理之后的高度
     * @return bitmap 处理之后的图片
     */
    public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
        Bitmap BitmapOrg = bitmap;
        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = w;
        int newHeight = h;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);
        return resizedBitmap;
    }

变圆处理

/**
     *弄圆形图片
     * @param borderWidth 边界宽度
     * @param borderColor 边界颜色
     * @return bitmap 处理之后图片
     */
    public static Bitmap getCircleBitmap(Bitmap mBitmap, int borderWidth, int borderColor) {
        Paint mPaint = new Paint();
        BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);
        mPaint.setAntiAlias(true);
        int minLength = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
        int radius = minLength / 2;

        Bitmap newBitmap = Bitmap.createBitmap(
                minLength + borderWidth * 2,
                minLength + borderWidth * 2,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(newBitmap);

        canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, mPaint);

        if (borderWidth != 0) {
            Paint borderPaint = new Paint();
            borderPaint.setColor(borderColor);
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setAntiAlias(true);
            borderPaint.setStrokeWidth(borderWidth);
            canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, borderPaint);
        }
        return newBitmap;
    }

总体代码

public class CircleMake {
    private static int COLORDRAWABLE_DIMENSION = 2;
    private final static int borderWidth = 2;
    private final static int borderColor = Color.WHITE;


    public static Bitmap getBitmap(Context context, int rsId, Resources.Theme theme) {
        Drawable drawable = context.getResources().getDrawable(rsId, theme);
        return getBitmap(drawable, borderWidth, borderColor);
    }

    public static Bitmap getBitmap(Drawable drawable) {
        return getBitmap(drawable, borderWidth, borderColor);
    }

    public static Bitmap getBitmap(Drawable drawable, int borderWidth, int borderColor) {
        Bitmap bitmap = getBmpFormDraw(drawable);
        return getCircleBitmap(bitmap, borderWidth, borderColor);
    }
    /**
     *弄圆形图片
     * @param borderWidth 边界宽度
     * @param borderColor 边界颜色
     * @return bitmap 处理之后图片
     */
    public static Bitmap getCircleBitmap(Bitmap mBitmap, int borderWidth, int borderColor) {
        Paint mPaint = new Paint();
        BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(shader);
        mPaint.setAntiAlias(true);
        int minLength = Math.min(mBitmap.getWidth(), mBitmap.getHeight());
        int radius = minLength / 2;

        Bitmap newBitmap = Bitmap.createBitmap(
                minLength + borderWidth * 2,
                minLength + borderWidth * 2,
                Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(newBitmap);

        canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, mPaint);

        if (borderWidth != 0) {
            Paint borderPaint = new Paint();
            borderPaint.setColor(borderColor);
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setAntiAlias(true);
            borderPaint.setStrokeWidth(borderWidth);
            canvas.drawCircle(borderWidth + radius, borderWidth + radius, radius, borderPaint);
        }
        return newBitmap;
    }

    /**
     *Drawable转Bitmap
     * @param drawable
     * @return bitmap
     */
    public static Bitmap getBmpFormDraw(Drawable drawable) {
        Bitmap bitmap;
        if (drawable instanceof ColorDrawable) {
            bitmap = Bitmap.createBitmap(
                    COLORDRAWABLE_DIMENSION,
                    COLORDRAWABLE_DIMENSION,
                    Bitmap.Config.ARGB_8888);
        } else {
            bitmap = Bitmap.createBitmap(
                    drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(),
                    drawable.getOpacity() != PixelFormat.OPAQUE ?
                            Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        }
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        return bitmap;
    }

    /**
     *重新设置bitmap大小
     * @param bitmap 处理之前的图片
     * @param w bitmap处理之后的宽度
     * @param h bitmap处理之后的高度
     * @return bitmap 处理之后的图片
     */
    public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
        Bitmap BitmapOrg = bitmap;
        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = w;
        int newHeight = h;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);
        return resizedBitmap;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值