Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用onTouchEvent保存绘制到位图上的图像_Java_Android_Android Layout_Android Canvas_Android Bitmap - Fatal编程技术网

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

Java 使用onTouchEvent保存绘制到位图上的图像,java,android,android-layout,android-canvas,android-bitmap,Java,Android,Android Layout,Android Canvas,Android Bitmap,我正在尝试触摸我的应用程序区域,以便在屏幕上放置AND门。所以当我触摸与门区域,然后我再次触摸将其放置在电路上,门在我触摸的位置被画出,但一旦我再次触摸,它就会消失 我用canvas.drawBitmap静态创建了一个电路,它们出现在那里并保持不变。也就是说,我在屏幕上创建了大量canvas.draw位图图像 @覆盖 公共布尔onTouchEvent(MotionEvent MotionEvent){ Log.d(“调试”,“在onTouchEvent中”); if((motionEvent.g

我正在尝试触摸我的应用程序区域,以便在屏幕上放置AND门。所以当我触摸与门区域,然后我再次触摸将其放置在电路上,门在我触摸的位置被画出,但一旦我再次触摸,它就会消失

我用canvas.drawBitmap静态创建了一个电路,它们出现在那里并保持不变。也就是说,我在屏幕上创建了大量canvas.draw位图图像

@覆盖
公共布尔onTouchEvent(MotionEvent MotionEvent){
Log.d(“调试”,“在onTouchEvent中”);
if((motionEvent.getAction()&motionEvent.ACTION\u掩码)==motionEvent.ACTION\u UP){
placeComponent();
Touch.horizontalpothed=(int)motionEvent.getX()/grid.getBlockSize();
Touch.verticaltouch=(int)motionEvent.getY()/grid.getBlockSize();
}
draw();
返回true;
}
void placeComponent(){
Log.d(“调试”、“就地组件”);
//转换浮动屏幕坐标
//转换为整数网格坐标
touchTemp=触摸的对象(触摸。水平触摸,触摸。垂直触摸);
}
私有void regionHit(){
位图_andTest=BitmapFactory.decodeResource(getResources(),R.drawable.andgatetrans);
如果(触摸温度等于(“和”)){
canvas.drawBitmap(_和test,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize(),null);
//drawIcons.drawANDGatev2(画布,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize());
}
如果(触摸温度等于(“或”)){
}
如果(触摸温度等于(“非”)){
}
如果(触摸温度等于(“开关”)){
}
}
//用于告诉regionHit()要做什么
私有字符串whatWasTouched(浮动水平触摸、浮动垂直触摸){

如果(horizontalTouched>=5.0&&horizontalTouched=0.0&&verticalTouched=5.0&&verticalTouched=5.0&&horizontalTouched=10.0&&verticalTouched=5.0&&horizontalTouched=0.0&&horizontalTouched=0.0&&horizontalTouched=5.0&&verticalTouched=0.0&&horizo)ntalTouched=10.0&&verticalTouched=0.0&&horizontalTouched=15.0&&verticalTouched确定,因此使用whatwastTouched()方法选择与门,并将其放置在画布的空白部分,重复此操作时,与门将消失,并希望它保持在其位置?要完成此操作,需要将与门的位置保存在某个位置

在regionHit()方法中,您正在绘制和选通位图

canvas.drawBitmap(_andTest,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize(),null);
然后在下一次触摸事件中,whatWasTouched()可能不会返回AND gate,并且touch类将更新触摸点值。这就是为什么在下一次触摸中看不到AND 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;
    }

}
在您的视图中使用上面的类,如下所示

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;
    }
}
公共类网关视图扩展视图{
私人ArrayList大门;
私有位图和网关位图;
公共网关视图(上下文){
超级(上下文);
//位图应该在这里解码
andGateBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.andgatetrans);
最终初始容量=5;
门=新阵列列表(初始容量);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//在列表中绘制所有保存的门。
final int listSize=gates.size();
for(int i=0;i