我试图触摸应用程序的区域以在屏幕上放置一个AND门。因此,当我触摸“与”门区域,然后再次触摸以将其放置在电路上时,门会在我触摸的位置绘制,但是一旦我再次触摸,它就会消失。
ive使用canvas.drawBitmap静态创建了一个电路,它们在那里出现并停留。意思是,我在屏幕上留下了大量的canvas.drawBitmap图像。
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
Log.d("Debugging", "In onTouchEvent");
if((motionEvent.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
placeComponent();
Touch.horizontalTouched = (int)motionEvent.getX()/ grid.getBlockSize();
Touch.verticalTouched = (int)motionEvent.getY()/ grid.getBlockSize();
}
draw();
return true;
}
void placeComponent(){
Log.d("Debugging", "In placeComponent");
// Convert the float screen coordinates
// into int grid coordinates
touchTemp = whatWasTouched(Touch.horizontalTouched, Touch.verticalTouched);
}
private void regionHit() {
Bitmap _andTest = BitmapFactory.decodeResource(getResources(), R.drawable.andgatetrans);
if(touchTemp.equals("AND")){
canvas.drawBitmap(_andTest,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize(),null);
//drawIcons.drawANDGatev2(canvas,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize());
}
if(touchTemp.equals("OR")){
}
if(touchTemp.equals("NOT")){
}
if(touchTemp.equals("SWITCH")){
}
}
// used to tell regionHit() what to do
private String whatWasTouched(float horizontalTouched, float verticalTouched) {
if(horizontalTouched >= 5.0 && horizontalTouched <= 9.0){
if(verticalTouched >= 0.0 && verticalTouched <=4.0){
return "AND";
}
}
if(horizontalTouched >= 5.0 && horizontalTouched <= 9.0){
if(verticalTouched >= 5.0 && verticalTouched <=9.0){
return "OR";
}
}
if(horizontalTouched >= 5.0 && horizontalTouched <= 9.0){
if(verticalTouched >= 10.0 && verticalTouched <=14.0){
return "NOT";
}
}
if(horizontalTouched >= 5.0 && horizontalTouched <= 9.0){
if(verticalTouched >= 15.0 && verticalTouched <=19.0){
return "SWITCH";
}
}
if(horizontalTouched >= 0.0 && horizontalTouched <= 4.0){
if(verticalTouched >= 0.0 && verticalTouched <=4.0){
return "Play/Pause";
}
}
if(horizontalTouched >= 0.0 && horizontalTouched <= 4.0){
if(verticalTouched >= 5.0 && verticalTouched <=9.0){
return "EDIT";
}
}
if(horizontalTouched >= 0.0 && horizontalTouched <= 4.0){
if(verticalTouched >= 10.0 && verticalTouched <=14.0){
return "WIRE";
}
}
if(horizontalTouched >= 0.0 && horizontalTouched <= 4.0){
if(verticalTouched >= 15.0 && verticalTouched <=19.0){
return "LED";
}
}
return "-1";
}
注意:在onTouchEvent()上方的draw()中调用regionTouched() 我希望能够触摸我的单屏应用程序中指示“与”门的区域,然后再次触摸以将其放置在画布的空白部分并留在画布上,并在其中分配其位置。它被放置。但是一旦我再次触摸屏幕,所有的工作都将被放置和移除。
答案 0 :(得分:1)
好吧,因此使用whatWasTouched()方法选择“与”门并将其放置在画布的空白部分,重复执行后,“与”门消失并且您希望它保持在其位置?为此,您需要将AND门的位置保存在某个地方。
在您的regionHit()方法中,您正在绘制AND门位图。
canvas.drawBitmap(_andTest,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize(),null);
然后在下一次触摸事件中,您的whatWasTouched()可能不会返回与门,并且您的Touch类还将具有更新的触摸点值。这就是为什么您在下次触摸时看不到“与”门的原因。
因此,您需要保存选定的门以及它们在画布上的位置。
创建门类
public class Gate {
private Bitmap bitmap;
private int drawX;
private int drawY;
public Gate(Bitmap bitmap, int drawX, int drawY) {
this.bitmap = bitmap;
this.drawX = drawX;
this.drawY = drawY;
}
public void draw(Canvas c) {
c.drawBitmap(bitmap, drawX, drawY, null);
}
public void updateDrawPosition(int drawX, int drawY) {
this.drawX = drawX;
this.drawY = drawY;
}
}
在这样的视图中使用上述类
public class GatesView extends View {
private ArrayList<Gate> gates;
private Bitmap andGateBitmap;
public GatesView(Context context) {
super(context);
// bitmap should be decoded here
andGateBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.andgatetrans);
final int initialCapacity = 5;
gates = new ArrayList<>(initialCapacity);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw all saved gates in the list.
final int listSize = gates.size();
for (int i = 0; i < listSize; i++)
gates.get(i).draw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// when a new gate is selected add it to gatesList
gates.add(new Gate(andGateBitmap, initial Xpos to draw, initial Ypos to draw)); // bitmap should not be decoded from Resource in onDraw() or onTouchEvent().
invalidate(); // tell android that our view has updated and needs to be redrawn.
return true;
}
}