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.設(shè)置圖片的圓角负蚊,返回設(shè)置后的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;
// 相當(dāng)于清屏
canvas.drawARGB(0, 0, 0, 0);
// 先畫了一個帶圓角的矩形
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
// 再把原來的bitmap畫到現(xiàn)在的bitmap!S杲取袭异!注意這個理解
canvas.drawBitmap(bitmap, rect, rect, paint);
return roundCornerBitmap;
}