gpt4 book ai didi

android在触摸事件上绘图

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:08:03 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,使用户能够触摸屏幕并根据用户的手指坐标绘制图像。这是我的代码:

public class DrawingBoard extends View {

Drawable editIcon = getResources().getDrawable(R.drawable.icon);
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);

float xPos = 0;
float yPos = 0;

public DrawingBoard (Context context) {
// TODO Auto-generated constructor stub
super (context);
}
@Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);

canvas.save();
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.translate(xPos, yPos);
editIcon.draw(canvas);
canvas.restore();

invalidate();
}
@Override
public boolean onTouchEvent (MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
xPos = event.getX();
yPos = event.getY();
break;
}

return true;

}
}
}

但是,每当我尝试在模拟器中点击屏幕时,都没有显示图像....

请指出我的错误...谢谢

最佳答案

onTouchEvent() 中没有 invalidate()

        @Override
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);

canvas.save();
canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.translate(xPos, yPos);
editIcon.draw(canvas);
canvas.restore();

// invalidate();
}
@Override
public boolean onTouchEvent (MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
xPos = event.getX();
yPos = event.getY();
invalidate(); // add it here
break;
}

return true;

}

关于android在触摸事件上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6756806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com