package com.zxhl.cms.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * Created by gaoleichao on 2018/6/7.
 */

public class ImageUtils {


    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float width, float height, float roundPx) {
        Bitmap b = getRoundedCornerBitmap(bitmap, roundPx);
        if (b == null) {
            return null;
        }
        float oldWidth = b.getWidth();
        float oldHeight = b.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(width / oldWidth, height / oldHeight);
        return Bitmap.createBitmap(b, 0, 0, (int) oldWidth, (int) oldHeight, matrix, true);
    }


    /**
     * @param bitmap
     * @param roundPx 一般设成14
     * @return
     * @Description: 获得圆角图片的方法
     * @author
     * @date 2014-4-2
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
        if (bitmap == null) {
            return null;
        }
        try {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final int color = 0xff424242;// 颜色值（0xff---alpha）
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);// Rect是使用int类型作为数值，RectF是使用float类型作为数值
            // --------抗锯齿-------//
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, null, rect, paint);
            return output;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
