Android

来源:互联网 发布:蓝月传奇轮回系统数据 编辑:程序博客网 时间:2024/06/05 17:55
  • Bitmap缩放
    /**     * 缩放Bitmap     *     * @param src    原bitmap     * @param width  缩放后的宽度     * @param height 缩放后的高度     * @return 缩放后的bitmap     */    public static Bitmap scaleBitmap(Bitmap src, int width, int height) {        int srcW = src.getWidth();        int srcH = src.getHeight();        Matrix matrix = new Matrix();        matrix.setScale(((float) width) / srcW, ((float) height) / srcH);        return Bitmap.createBitmap(src, 0, 0, srcW, srcH, matrix, false);    }
  • Bitmap裁剪
    /**     * 将原bitmap裁剪成正方形     *     * @param src 原bitmap     * @return 正方形的bitmap     */    public static Bitmap centerCropSquare(Bitmap src) {        int w = src.getWidth();        int h = src.getHeight();        System.out.println(w + "," + h);        if (w == h) {            return src;        } else if (w > h) {            return Bitmap.createBitmap(src, (w - h) / 2, 0, h, h);        } else {            return Bitmap.createBitmap(src, 0, (h - w) / 2, w, w);        }    }
    /**     * 创建圆形的Bitmap     *     * @param src 原Bitmap     * @return 创建的圆形Bitmap     */    public static Bitmap createRoundBitmap(Bitmap src) {        if (src == null) return null;        Bitmap bmp = centerCropSquare(src);//首先裁剪成正方形        int size = bmp.getWidth();        RectF rectF = new RectF(0, 0, size, size);        Bitmap bg = Bitmap.createBitmap(size, size, bmp.getConfig());        Canvas canvas = new Canvas(bg);        Paint paint = new Paint();        paint.setAntiAlias(true);        canvas.drawRoundRect(rectF, size / 2, size / 2, paint);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        canvas.drawBitmap(bmp, 0, 0, paint);        return bg;    }
  • Bitmap旋转
    /**     * 旋转Bitmap     * 注:该方法会导致Bitmap大小变大     *     * @param src    原bitmap     * @param degree 旋转角度     * @return 旋转之后的bitmap     */    public static Bitmap rotateBitmap(Bitmap src, int degree) {        Matrix matrix = new Matrix();        matrix.postRotate(degree);        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, false);    }
原创粉丝点击