a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android處理圖像數據轉換的各種方法

2020-04-11 11:41:36
字體:
來源:轉載
供稿:網友

Android中處理圖像是一件很常見的事情,這里記錄備忘一些親身使用過的處理圖片數據的方法。

轉為Bitmap

RGB值轉Bitmap

復制代碼 代碼如下:

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

復制代碼 代碼如下:

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

復制代碼 代碼如下:

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

讀取文件轉Bitmap

復制代碼 代碼如下:

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

讀取資源轉Bitmap

復制代碼 代碼如下:

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

輸入流轉Bitmap

復制代碼 代碼如下:

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

Drawable轉Bitmap

復制代碼 代碼如下:

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

轉為Drawable

資源轉Drawable

復制代碼 代碼如下:

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

Bitmap轉Drawable
復制代碼 代碼如下:

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

圖片圓角展示

通過對圖片數據bitmap進行處理即可,其中pixels為邊角的半徑。

復制代碼 代碼如下:

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;
    }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩中文视频 | 九九久久国产 | 国产精品18hdxxxⅹ在线 | 日本不卡一区二区 | 国产精品视频黄色 | 天天夜夜操操 | 亚洲黄色一级毛片 | 日韩成人免费在线 | 久久久99国产精品免费 | 九九免费在线观看 | 国产精品丰满对白在线观看 | 欧美在线一二三 | 久久成人精品 | 欧美亚洲国产一区 | 在线观看中文 | 欧美视频在线一区 | 午夜成人免费影院 | 国产成人精品一区二区 | 久久久com | 国产视频91在线 | 四虎黄色网 | 国产高清久久久 | 欧美亚洲视频 | 欧美激情自拍偷拍 | 欧美日韩国产成人 | 皇上侵犯双性太子高h虐受视频 | 国产综合精品一区二区三区 | 一区二区免费 | 日韩三级电影在线免费观看 | av中文字幕在线播放 | 国产精品久久久久久久久久妞妞 | 国产精品亚洲欧美日韩一区在线 | 午夜在线视频免费观看 | 色婷婷综合网 | 91精品在线观看入口 | 精品成人国产 | 久久综合狠狠综合久久综合88 | 亚洲成人av | 日韩中文一区二区三区 | 久久久久久久中文 | 在线观看一区二区视频 |