Android之Bitmap专题

博客地址

1.从资源文件中获取

 Bitmap rawBitmap =BitmapFactory.decodeResource(getResources(),R.drawable.img1);

2.从SD卡中获取

(方法1)

String SDCarePath=Environment.getExternalStorageDirectory().toString();
String filePath=SDCarePath+"/"+"haha.jpg";
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);  

(方法2)

InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg"); 
 Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream);

3.设置图片的圆角,返回设置后的BitMap

public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(roundCornerBitmap);
int color = 0xff424242;
Paint paint = new Paint();
paint.setColor(color);
paint.setAntiAlias(true);// 防止锯齿
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectF = new RectF(rect);
float roundPx = pixels;
// 相当于清屏
canvas.drawARGB(0, 0, 0, 0);
// 先画了一个带圆角的矩形
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
// 再把原来的bitmap画到现在的bitmap!!!注意这个理解
canvas.drawBitmap(bitmap, rect, rect, paint);
return roundCornerBitmap;
}

编译器不好用,算了,看博客。。。

你可能感兴趣的:(Android之Bitmap专题)