gpt4 book ai didi

java - 保存使用 onTouchEvent 绘制到位图上的图像

转载 作者:行者123 更新时间:2023-12-02 09:26:58 26 4
gpt4 key购买 nike

我正在尝试触摸应用程序的区域以在屏幕上放置一个“与”门。因此,当我触摸与门区域,然后再次触摸将其放置在电路上时,门会在我触摸的位置绘制,但一旦我再次触摸,它就会消失。

我使用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";
}

注意:regionTouched() 是在 onTouchEvent() 上方的 draw() 中调用的我希望能够触摸我的单屏应用程序中指示“AND”门的区域,然后再次触摸以将其放置在 Canvas 的空白部分上并让它留在那里,并在 Canvas 上分配其位置它被放置在其中。但一旦我再次触摸屏幕,它所做的一切都会被放置和删除。

最佳答案

好的,所以你的与门是使用whatWasTouched()方法选择的,并放置在 Canvas 的空白部分,重复时你的与门就会消失,你希望它保持在原来的位置吗?要实现此目的,您需要在某处保存与门的位置。

在您的regionHit()方法中,您正在绘制AND门位图。

canvas.drawBitmap(_andTest,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize(),null);

然后,在下一个触摸事件中,您的 WhatWasTouched() 可能不会返回 AND 门,并且您的 Touch 类将具有更新的触摸点值。这就是为什么您在下次触摸时看不到 AND 门。

因此您需要保存选定的门以及它们在 Canvas 上的位置。

创建一个类 Gate

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;
}

}

并在您的 View 中使用上面的类,如下所示

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;
}
}

关于java - 保存使用 onTouchEvent 绘制到位图上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58279000/

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