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

标签 java android android-layout android-canvas android-bitmap

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

我使用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/

相关文章:

java - 如何比较 SortedSet 的元素以查看两个集合中有多少元素匹配

java - Hibernate 无法访问 phpMyAdmin 插入的数据

android - Qt 安卓 : where is my file if I write to "./myfile.txt"?

Android - 自定义评分栏看起来像是在流血

java - 如何从 Java 发送一个 4 字节的 header 并在 Python 中读取它?

java - 安卓/Java : Split Data in an Array and display

android - Firebase 函数响应返回 null

android - 带有捕捉全屏卡片的垂直 RecyclerView

android - xml文件中的MultiAutoCompleteTextView setDropDownBackgroundResource

Android:ScrollView 不滚动