代码直接复制使用,修改为自己需要的参数就可以了
五种方法获取Bitmap对象
1.构建BitmapDrawable对象生成Bitmap
BitmapDrawable mBitmap = new BitmapDrawable(getResources(), "sdcard/xxx.png");
Bitmap bitmap = mBitmap.getBitmap();
2.通过资源ID创建,需要将对应的资源先放在项目中的drawable文件夹中
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);
3.通过文件创建,需要文件绝对路径
Bitmap bitmap = BitmapFactory.decodeFile("sdcard/xxx.png");
4.通过字节数组创建
byte[] data;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
5.通过输入流创建
InputStream inputStream = new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
};
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
以下代码将图片调整到与控件大小一样刚好占满控件,前面两个参数为位图的宽度和高度,第三个参数为格式
onWindowFocusChanged这个方法里面才可以获取控件的宽度和高度,oncreate方法里面是无法获取的
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
int[] a = new int[2];
img = (ImageView) findViewById(R.id.img);
img.getLocationOnScreen(a);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);
//缩放
Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap,img.getWidth(), img.getHeight(),true);
img.setImageBitmap(bitmap2);
}
}
第一个参数需要剪切的位图对象,然后是剪切的起始位置为剪切图左上角的坐标,最后两个参数是剪切图的宽度和高度
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap,100,100,200,200);
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap2);;
下面的代码将图片旋转90°
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);
Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
以下代码将Bitmap保存为图片文件,安卓10以下保存在sdcard根目录,安卓10以上保存在Android/data/包名/files目录下
new Thread(new Runnable(){
@Override
public void run(){
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.abc1);
try{
String filePath=null;
//如果手机已插入sd卡,且app具有读写sd卡的权限
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
if(Build.VERSION.SDK_INT< 29){
//安卓10以下保存在SD卡根目录
filePath=Environment.getExternalStorageDirectory().getCanonicalPath()+"/"+System.currentTimeMillis()+".jpg";
}else{
//安卓10以上保存在Android/data/com.example.filetorw/files目录下
filePath=MainActivity.this.getExternalFilesDir(null).getAbsolutePath()+"/"+System.currentTimeMillis()+".jpg";
}
}
FileOutputStream outStream=new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outStream);
outStream.flush();
outStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
}).start();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paint mPaint = new Paint();;
mPaint.setStyle(Paint.Style.FILL);
mPaint.setTextSize(36);
mPaint.setStrokeWidth(5);
Canvas mCanvas;
ImageView img = (ImageView) findViewById(R.id.img);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1, options).copy(Bitmap.Config.ARGB_8888, true);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.abc2);
bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
mCanvas = new Canvas(bitmap);
//添加图片
mCanvas.drawBitmap(bitmap2, 100, 100, null);
//添加文字
mCanvas.drawText("测试", 200, 250, mPaint);
mCanvas.save();
mCanvas.restore();
img.setImageBitmap(bitmap);
}
代码直接复制使用,修改为自己需要的参数就可以了