使用View自带方法 invalidate();
点击屏幕哪里就绘制到哪里
代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//自定义View
this.setContentView(new CustomizeView(this));
}
class CustomizeView extends View {
Bitmap bitmap;
int i = 0, j = 0;
public CustomizeView(Context context) {
super(context);
//获取图片
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.heixiang);
}
// 绘制
@Override
public void onDraw(Canvas canvas){
// 绘制bitmap
canvas.drawBitmap(bitmap,i,j,null);
}
// 监听屏幕
@Override
public boolean onTouchEvent(MotionEvent event){
//点击屏幕
if(event.getAction()==MotionEvent.ACTION_DOWN) {
//点击位置
i = (int) event.getX();
j = (int) event.getY();
//刷新(自带方法)
invalidate();
}
return true;
}
}
}