Android 安卓与绘制星图
我在画布中单击的位置绘制图片。这是代码:Android 安卓与绘制星图,android,Android,我在画布中单击的位置绘制图片。这是代码: public class CanvasdrawActivity extends Activity implements OnTouchListener { ImageView imageView; Bitmap bitmap; Bitmap bitmap2; Canvas canvas; Paint paint; float downx = 0,downy = 0,upx = 0,upy = 0; @Override
public class CanvasdrawActivity extends Activity implements OnTouchListener {
ImageView imageView;
Bitmap bitmap;
Bitmap bitmap2;
Canvas canvas;
Paint paint;
float downx = 0,downy = 0,upx = 0,upy = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.imageView1);
Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
bitmap = Bitmap.createBitmap((int) dw, (int) dh,
Bitmap.Config.ARGB_8888);
bitmap2=BitmapFactory.decodeResource(getResources(),
R.drawable.star_bez_nog);
canvas = new Canvas(bitmap);
imageView.setImageBitmap(bitmap);
imageView.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
canvas.drawBitmap(bitmap2, downx, downy, null);
imageView.invalidate();
break;
}
return true;
}
}
我怎么能只画一次这幅画。当我第一次点击时,星星被画出来了,当我在其他地方点击时,什么也没发生。只能在第一次单击时绘制。设置布尔值
Canvas canvas;
Paint paint;
Boolean drawOnlyOnce = true;
public boolean onTouch(View v, MotionEvent event) {
...
if (drawOnlyOnce) {
canvas.drawBitmap(bitmap2, downx, downy, null);
imageView.invalidate();
drawOnlyOnce = false;
}
...