我使用canvas制作UI。我希望当用户在屏幕上触摸然后在画布上显示一个图像所以请帮助我。当用户触摸屏幕时我想要这个。如果有的话,图像是由drawable拍摄的任何方式在(x,y)坐标上显示这个图像然后告诉我谢谢?
public class caanvas extends View {
public Canvas canvas1=new Canvas();
public float firstcord=0f,secndcord=0f,thirdcord=0f,forthcord=0f,fifthcord=0f,sixcord=0f,sevencord=0f,eightcord=0f;
public float widthborder=0f;
public float h = getContext().getResources().getDisplayMetrics().heightPixels;
public float w = getContext().getResources().getDisplayMetrics().widthPixels;
public caanvas(Context context) {
super(context);
widthborder=w/72f;
firstcord=w/2.88f;
secndcord=w/1.5f;
thirdcord=w/18f;
forthcord=w/1.035f;
fifthcord=h/4.2f;
sixcord=h/2.46f;
sevencord=h/1.63f;
eightcord=h/1.28f;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas1=canvas;
Paint paint = new Paint();
paint.setColor(Color.LTGRAY);
paint.setStrokeWidth(widthborder);
canvas.drawLine(firstcord, fifthcord, firstcord, eightcord, paint);
canvas.drawLine(secndcord, fifthcord, secndcord, eightcord, paint);
canvas.drawLine(thirdcord, sixcord, forthcord, sixcord, paint);
canvas.drawLine(thirdcord, sevencord, forthcord, sevencord, paint);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
switch (ev.getAction())
{
case MotionEvent.ACTION_DOWN:
Showimage();
Log.i("Tag","Start");
break;
}
return true;
}
public void Showimage()
{
Bitmap mFinalbitmap= BitmapFactory.decodeResource(getResources(), R.drawable.cross);
canvas1.drawBitmap(mFinalbitmap, 0, 0, null);
}
}