• Bitmap处理


    Android中处理图像是一件很常见的事情,这里记录备忘一些亲身使用过的处理图片数据的方法。

    转为Bitmap

    RGB值转Bitmap

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    private Bitmap createColorBitmap(String rgb, int width, int height) {
          Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
          int color = Color.parseColor(rgb);
          bmp.eraseColor(color);
          return bmp;
    }
    
    //Usage
    Bitmap bmp = createColorBitmap("#cce8cf", 200, 50);
    

    Color值转Bitmap

    1
    2
    3
    4
    5
    6
    7
    
    private Bitmap createColorBitmap(int color, int width, int height) {
      Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      bmp.eraseColor(color);
      return bmp;
    }
    //Usage
    Bitmap bmp = createColorBitmap(Color.BLUE, 200, 50);
    

    字节数组转Bitmap

    1
    2
    3
    
    private Bitmap getBitmapFromByteArray(byte[] array) {
      return BitmapFactory.decodeByteArray(array, 0, array.length);
    }
    

    读取文件转Bitmap

    1
    2
    3
    
    private Bitmap getBitmapFromFile(String pathName) {
          return BitmapFactory.decodeFile(pathName);
    }
    

    读取资源转Bitmap

    1
    2
    3
    
    private Bitmap getBitmapFromResource(Resources res, int resId) {
          return BitmapFactory.decodeResource(res, resId);
      }
    

    输入流转Bitmap

    1
    2
    3
    
    private Bitmap getBitmapFromStream(InputStream inputStream) {
          return BitmapFactory.decodeStream(inputStream);
    }
    

    Drawable转Bitmap

    1
    
    Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
    

    转为Drawable

    资源转Drawable

    1
    
    Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
    

    Bitmap转Drawable

    1
    
    Drawable d = new BitmapDrawable(getResources(),bitmap);
    

    图片圆角展示

    通过对图片数据bitmap进行处理即可,其中pixels为边角的半径。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                    .getHeight(), 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);
            final float roundPx = pixels;
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
            return output;
        }
    

     

  • 相关阅读:
    Django集成Markdown编辑器【附源码】
    Django+JWT实现Token认证
    Docker环境的持续部署优化实践
    2018-行远自迩,登高自卑
    SVN Hooks的介绍及使用
    Django开发密码管理表实例【附源码】
    Django+Echarts画图实例
    Django使用Signals监测model字段变化发送通知
    运维效率之数据迁移自动化
    Python之路(第三十四篇) 网络编程:验证客户端合法性
  • 原文地址:https://www.cnblogs.com/pbq-dream/p/5373182.html
Copyright © 2020-2023  润新知