zoukankan      html  css  js  c++  java
  • Bitmap压缩到指定尺寸大小,获取圆角、圆形图片

      /**
         * 使用Matrix将Bitmap压缩到指定大小
         * @param bitmap
         * @param w
         * @param h
         * @return
         */
        public static Bitmap resizeBitmap(Bitmap bitmap, int w, int h)
        {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
    
            float scaleWidth = ((float) w) / width;
            float scaleHeight = ((float) h) / height;
    
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
    
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
                    height, matrix, true);
            return resizedBitmap;
        }
    
        /**
         * 给Bitmap设置圆角
         * @param bitmap
         * @param roundPx  圆角值
         * @return
         */
        public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
    
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                    .getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            final int color = 0xff424242;
            final Paint paint = new Paint();
            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);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
            return output;
        }
    
        /**
         * 得到圆形图片
         * @param bitmap
         * @return
         */
        public static Bitmap getOvalBitmap(Bitmap bitmap){
    
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                    .getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            final int color = 0xff424242;
            final Paint paint = new Paint();
            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);
    
            canvas.drawOval(rectF, paint);
    
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            return output;
        }
    

      

  • 相关阅读:
    计算机漏洞安全相关的概念POC 、EXP 、VUL 、CVE 、0DAY
    开始使用kali的一些小问题:菜鸟瞎折腾
    nmap参数详解(罗列一下)
    安装kali之后必做的几件小事
    Debian下virtualBox增强功能出错
    ArcGIS Engine 基础功能(一)
    sublime 配置简单的python环境
    解决 ‘Could not fetch URL https://pypi.python.org’的问题
    golang基础语法学习
    大象盒子技术栈
  • 原文地址:https://www.cnblogs.com/wangjiaghe/p/7063282.html
Copyright © 2011-2022 走看看