使用BitmapFactory的decodeFile()方法获取的Bitmap对象是只读的,无法进行编辑操作
需要进行编辑的话,需要获取到该对象的一个副本
代码如下:
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Bitmap bmSrc = BitmapFactory.decodeFile("sdcard/img15.jpg");
// 生产一份与原图宽高和配置一模一样的白纸
Bitmap bmCopy = Bitmap.createBitmap(bmSrc.getWidth(),
bmSrc.getHeight(), bmSrc.getConfig());
// 设置画笔
Paint paint = new Paint();
// 打开画板,并把白纸铺在画板上
Canvas canvas = new Canvas(bmCopy);
// 把原图绘画到副本上
canvas.drawBitmap(bmSrc, new Matrix(), paint);
// 设置显示原图和副本
ImageView iv_src = (ImageView) findViewById(R.id.iv_src);
ImageView iv_copy = (ImageView) findViewById(R.id.iv_copy);
iv_src.setImageBitmap(bmSrc);
iv_copy.setImageBitmap(bmCopy);
} }