Tito February 2016

Draw bitmap over already drawn background on SurfaceView

In my SurfaceView when I click on the screen it creates black dots after that I connect them and it has a filled with color. After that I am ttrying to draw other bitmap and I want the current state be something like a background and when I touch the screen I want to draw a bitmap on this possition and when I am trying to draw a bitmap on touch location the canvas goes back to the previous state with the draws.

Here is my code:

package com.inveitix.android.clue.ui.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.inveitix.android.clue.R;
import com.inveitix.android.clue.cmn.Door;
import com.inveitix.android.clue.cmn.MapPoint;

import java.util.ArrayList;
import java.util.List;

public class DrawingView extends SurfaceView {

    private static final String TAG = "DrawingView";
    private static final float DOOR_SIZE = 30;

    private Paint paint;
    private Canvas canvas;
    private int maxHeight;
    private int maxWidth;
    private SurfaceHolder surfaceHolder;
    private List<MapPoint> shape;
    private List<Door> doors;
    private float ratio;
    private boolean isFloorFinished;
    private boolean isDoorSelected;
    Path path;

    public DrawingView(Context context) {
        super(context);
        init();
    }

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setIs        

Answers


Tito February 2016

I found it I have to draw doors in the same time with the floor so I did this changes:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && !isFloorFinished) {
            if (surfaceHolder.getSurface().isValid()) {
                shape.add(new MapPoint(event.getX(), event.getY()));
                canvas = surfaceHolder.lockCanvas();
                canvas.drawColor(Color.WHITE);
                for (MapPoint point : shape) {
                    canvas.drawCircle(point.getX(), point.getY(), 10, paint);
                }
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        } else if (event.getAction() == MotionEvent.ACTION_DOWN && isDoorSelected) {
            if (surfaceHolder.getSurface().isValid()) {
                Door door = new Door();
                door.setConnectedTo("door1"); //this is for test
                door.setX(event.getX());
                door.setY(event.getY());
                doors.add(door);
                drawFloor();
            }
        }
        return false;
    }

    public void drawFloor() {
        Bitmap bmpFloorPattern = BitmapFactory.decodeResource(getResources(), R.drawable.floor_pattern6);
        BitmapShader patternBMPshader = new BitmapShader(bmpFloorPattern,
                Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        canvas = surfaceHolder.lockCanvas();
        path = new Path();
        path.reset();

        if (shape != null) {
            path.moveTo(shape.get(0).getX(), shape.get(0).getY());
            alignPoints(path);
            canvas.drawColor(Color.WHITE);
            for (int i = 0; i < shape.size(); i++) {
                path.lineTo(shape.get(i).getX(), shape.get(i).getY());
            }
        }
        paint.setShader(patternBMPshader);
        path.close();
        canvas.drawPath(path, paint);
        paint.setShader(null);
        if(doors.size() &g 

Post Status

Asked in February 2016
Viewed 3,290 times
Voted 5
Answered 1 times

Search




Leave an answer