我在onCreate函数中实例化canvas变量,如果我在onCreate函数中绘制一条线,它就会显示正常。但是,如果我在按钮的onClick函数中绘制线条,则它不起作用。可能是什么原因。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imageView = (ImageView) this.findViewById(R.id.imageView);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
canvas = new Canvas(mutableBitmap);
imageView.setImageBitmap(mutableBitmap);
paint = new Paint();
paint.setColor(Color.rgb(255, 153, 51));
paint.setStrokeWidth(10);
}
public void displayLine(View view) {
canvas.drawLine(10, 20, 400, 500, paint);
}
答案 0 :(得分:0)
不是说我必须这样做......但是你尝试添加一个无效的呼叫吗?即:
public void displayLine(View view) {
canvas.drawLine(10, 20, 400, 500, paint);
view.invalidate();
}