[英]Saving an image that was drawn onto a bitmap using onTouchEvent
我正在尝试触摸我的应用程序区域以在屏幕上放置一个与门。 因此,当我触摸与门区域,然后再次触摸以将其放置在电路上时,门会在我触摸的位置被绘制,但一旦我再次触摸,它就会消失。
我使用 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() 我希望能够触摸我的单屏应用程序中指示“与”门的区域,然后再次触摸以将其放置在canvas 并让它留在那里,并在放置它的 canvas 上分配其 position。 但是一旦我再次触摸屏幕,它的所有功能都会被放置和移除。
好的,因此使用 whatWasTouched() 方法选择了您的与门并放置在 canvas 的空白部分,重复您的与门消失并且您希望它停留在其 position? 为此,您需要将与门的 position 保存在某处。
在您的 regionHit() 方法中,您正在绘制 AND 门 bitmap。
canvas.drawBitmap(_andTest,Touch.horizontalTouched*grid.getBlockSize(),Touch.verticalTouched*grid.getBlockSize(),null);
然后在下一个触摸事件中,您的 whatWasTouched() 可能不会返回 AND 门,并且您的 Touch class 将具有更新的触摸点值。 这就是为什么您在下一次触摸中看不到 AND 门的原因。
因此,您需要将选定的门及其 position 保存在 canvas 上。
创建 class 门
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;
}
}
并像这样在您的视图中使用上面的 class
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.