Bitmap 是什么

https://juejin.cn/post/7439621582617722931

Bitmap API

http://www.cnblogs.com/liyan-blogs/p/5535071.html

// 将ByteArray转换为Bitmap
public Bitmap byteArrayToBitmap(byte[] byteArray) {
    return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}

// 将Bitmap保存为本地图片文件
public void saveBitmapToFile(Bitmap bitmap, String filePath) {
    File file = new File(filePath);
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// 示例用法
byte[] byteArray = // 从服务器获取的字节数组
Bitmap bitmap = byteArrayToBitmap(byteArray);
saveBitmapToFile(bitmap, "/sdcard/image.jpg");
  • 指定一个色值生成bitmap
public Bitmap getBackGroundBitmap(int color) {

                     Paint p = new Paint();

                     p.setColor(Color.RED);

                     Bitmap bitmap = Bitmap.createBitmap(190, 110, Bitmap.Config.ARGB_8888);

                     Canvas canvas = new Canvas(bitmap);

                     canvas.drawColor(android.R.color.transparent);

                     canvas.drawColor(color);

                     return bitmap;

       }
  • 设置bitmap的透明度
 public static Bitmap getAlplaBitmap(Bitmap sourceImg, int number) {

          int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];

          sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg.getWidth(), sourceImg.getHeight());

          number = number * 255 / 100;

          for (int i = 0; i < argb.length; i++) {

                argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);

          }

           sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888);

        return sourceImg;

   }
  • drawable转换成bitmap
   public static Bitmap drawableToBitmap(Drawable drawable) {

          Bitmap bitmap = Bitmap.createBitmap(

                  drawable.getIntrinsicWidth(),

                  drawable.getIntrinsicHeight(),

                  drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);

          Canvas canvas = new Canvas(bitmap);

          drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

          drawable.draw(canvas);

          return bitmap;

   }
  • bitmap圆角
   public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {

           Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),

               bitmap.getHeight(), Config.ARGB_8888);

           Canvas canvas = new Canvas(output);

           final int color = Color.RED; 

           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 = 6;

           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;

   }
分类: Bitmap

0 条评论

发表回复

您的电子邮箱地址不会被公开。