1. 程式人生 > >對Bitmap放大縮小處理與裁剪

對Bitmap放大縮小處理與裁剪

對Bitmap放大縮小處理與裁剪

示例效果圖

如下圖所示,把拿到的招商銀行的圖示做成右側半透明的不規則形狀的背景

你好! 這是你第一次使用 **Markdown編輯器** 所展示的歡迎頁。如果你想學習如何使用Markdown編輯器, 可以仔細閱讀這篇文章,瞭解一下Markdown的基本語法知識。

實現方法

1、帶弧度的圓角矩形和圖片透明度

由於這張圖的白色背景部分是一個圓角矩形帶陰影的背景圖片,所以要注意放上去的圖示不要遮住圓角和陰影,需要一個圓角矩形的ImageView的控制元件,圓角的弧度和白色背景的圓角弧度一致。圓角矩形ImageView,弧度可以自己調節


public class XCRoundRectImageView extends ImageView {

    private Paint paint;
    
    public XCRoundRectImageView(Context context) {
        this(context,null);  
    }  
  
    public XCRoundRectImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);  
    }  
  
    public
XCRoundRectImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); paint = new Paint(); } /** * 繪製圓角矩形圖片 * @author caizhiming */ @Override protected void onDraw(Canvas canvas) { Drawable drawable =
getDrawable(); if (null != drawable) { Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Bitmap b = getRoundBitmap(bitmap, 25); final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight()); final Rect rectDest = new Rect(0,0,getWidth(),getHeight()); paint.reset(); canvas.drawBitmap(b, rectSrc, rectDest, paint); } else { super.onDraw(canvas); } } /** * 獲取圓角矩形圖片方法 * @param bitmap * @param roundPx,一般設定成14 * @return Bitmap * @author caizhiming */ private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); int x = bitmap.getWidth(); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } }

使用,背景圖示需要一定的透明度,設定 android:alpha=“0.06”

     <com.basestonedata.instalment.ui.scanpay.wiget.XCRoundRectImageView
            android:id="@+id/iv_bg_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:scaleType="centerInside"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:alpha="0.06" />

2、把圖片資源或者圖片連結變成bitmap

把資原始檔中的圖片轉化為bitmap

Resources res = MainActivity.this.getResources();
Bitmap bmp= BitmapFactory.decodeResource(res, R.mipmap.flower);

把Url圖片連結轉化為bitmap

//ImageLoader
Bitmap bitmap = ImageLoader.getInstance().loadImageSync((url),options);

//Glide框架
 Glide.with(getActivity()).load(mUserEntity.getData().getCover()).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                bitmap = resource;
            }
        });

3、圖片的放大與裁剪處理

public class BitmapCropUtils {

//放大圖片 新的寬高dp,放大縮小
    public static Bitmap zoomImg(Context context, Bitmap bm, int newWidth , int newHeight){
        // 獲得圖片的寬高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 計算縮放比例
        float scaleWidth = ((float) dip2px(context,newWidth)) / width;
        float scaleHeight = ((float) dip2px(context,newHeight)) / height;
        // 取得想要縮放的matrix引數
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的圖片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        Bitmap returnBm=imageCrop(context,newbm);
        return returnBm;
    }

//裁剪圖片
    public  static Bitmap imageCrop(Context context, Bitmap bitmap) {
        // 得到圖片的寬,高
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

       //width最大不能超過長方形的短邊
        if (w < width || h < width) {
           width = w > h ? h : w;
        }

//可以從圖片的中心裁剪
//        int retX = (w - width) / 2;
//        int retY = (h - width) / 2;

       //public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
       // surce:用來剪裁的圖片源;
       // x:剪裁x方向的起始位置;
       //y:剪裁y方向的起始位置;
       //width:剪裁的寬度;
       //height:剪裁的高度;
       //filer:不是很清楚它的作用 - -!
       //需要注意的是:必須滿足條件:x+width<=bitmap.width()(圖片源的原始寬度)否則會丟擲IllegalArgumentException異常。
        return Bitmap.createBitmap(bitmap, 0, 0,dip2px(context,145), dip2px(context,125), null, false);
    }

//把圖片裁剪成圓形(補充)
  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;
        }
    }

    /**
     * 根據手機的解析度從 dp 的單位 轉成為 px(畫素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}