多个贴纸擦除问题

时间:2018-06-16 10:00:42

标签: java android image-processing android-bitmap

我尝试制作图像编辑器应用程序。在这个应用程序中,我尝试在贴纸可以移动,缩放,旋转的图像上放置多个贴纸,并且还可以选择贴纸。谁能帮我 ?这里是我用贴纸移动,旋转,缩放所做的代码,但我不能删除选定的贴纸。

public class MainActivity extends AppCompatActivity {
    Bitmap originalBitmap,originalBitmap1;
    RelativeLayout rlImageViewContainer;
    private ImageView ivRedo;
    private ImageView ivUndo;
    Button btn_erase;
    private ArrayList<Path> paths;
    private ArrayList<Path> redoPaths;
    Paint destPaint = new Paint();
    Path destPath = new Path();
    Bitmap destBitmap;
    Canvas destCanvas = new Canvas();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
        originalBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.b);
        initView();
    }

    private void initView() {
        ivUndo = (ImageView) findViewById(R.id.iv_undo);
        ivRedo = (ImageView) findViewById(R.id.iv_redo);

        rlImageViewContainer = findViewById(R.id.rl_image_view_container);

        final PhotoSortrView photoSortrView = new PhotoSortrView(this);
        rlImageViewContainer.addView(photoSortrView,0);
        photoSortrView.setBackgroundColor(getResources().getColor(R.color.tran));
        photoSortrView.addImages(this, originalBitmap);
        photoSortrView.addImages(this, originalBitmap1);

        ivUndo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                undo();
            }
        });
        ivRedo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//                redo();
            }
        });
        btn_erase = (Button) findViewById(R.id.btn_erase);
        btn_erase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (photoSortrView.isErase) {
                    btn_erase.setBackgroundColor(Color.RED);
                    photoSortrView.isErase = false;
                    photoSortrView.isMove = true;
                } else {
                    btn_erase.setBackgroundColor(Color.GREEN);
                    photoSortrView.isErase = true;
                    photoSortrView.isMove = false;
                }
            }
        });
    }
}

PhotoSortrView.java

public class PhotoSortrView extends View implements MultiTouchController.MultiTouchObjectCanvas<MultiTouchEntity> {

    private static final String TAG = "PhotoSortr ####### ";
    // private static final int[] IMAGES = { R.drawable.m74hubble };
    private Path drawPath = new Path();
    // drawing and canvas paint
    private Paint drawPaint = new Paint(), canvasPaint = new Paint();
    // initial color
    private int paintColor = Color.TRANSPARENT;
    // canvas
    private Canvas drawCanvas = new Canvas();
    // canvas bitmap
    private Bitmap canvasBitmap;

    private ArrayList<MultiTouchEntity> imageIDs = new ArrayList<MultiTouchEntity>();
    // --
    private MultiTouchController<MultiTouchEntity> multiTouchController = new MultiTouchController<MultiTouchEntity>(this);
    // --
    private MultiTouchController.PointInfo currTouchPoint = new MultiTouchController.PointInfo();

    private static final int UI_MODE_ROTATE = 1, UI_MODE_ANISOTROPIC_SCALE = 2;
    private int mUIMode = UI_MODE_ROTATE;
    // --
    private static final float SCREEN_MARGIN = 100;
    private int displayWidth, displayHeight;

    public boolean isErase = false;
    public boolean isMove = true;


    Paint destPaint = new Paint();
    Path destPath = new Path();
    Bitmap destBitmap;
    Canvas destCanvas = new Canvas();


    int height,width;



    // ---------------------------------------------------------------------------------------------------
    public PhotoSortrView(Context context) {
        this(context, null);
        setupPaint();
        init(context);
    }

    private void setupPaint() {
        destPaint.setAlpha(0);
        destPaint.setAntiAlias(true);
        destPaint.setStyle(Paint.Style.STROKE);
        destPaint.setStrokeJoin(Paint.Join.ROUND);
        destPaint.setStrokeCap(Paint.Cap.ROUND);
        destPaint.setStrokeWidth(20);
        destPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

    public PhotoSortrView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        setupDrawing();
    }

    public PhotoSortrView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);

    }

    private void init(Context context) {
        Log.e(TAG, "init: " );
        Resources res = context.getResources();
        setBackgroundColor(Color.TRANSPARENT);

        DisplayMetrics metrics = res.getDisplayMetrics();
        this.displayWidth = res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? Math
                .max(metrics.widthPixels, metrics.heightPixels) : Math.min(
                metrics.widthPixels, metrics.heightPixels);
        this.displayHeight = res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? Math
                .min(metrics.widthPixels, metrics.heightPixels) : Math.max(
                metrics.widthPixels, metrics.heightPixels);
    }

    public void addImages(Context context, Bitmap resourceId) {
        Log.e(TAG, "addImages: " );


//        setupDrawing();
        Resources res = context.getResources();
        imageIDs.add(new ImageEntity(resourceId, res));

        float cx = SCREEN_MARGIN + (float)
                (Math.random() * (displayWidth - 2 * SCREEN_MARGIN));
        float cy = SCREEN_MARGIN + (float)
                (Math.random() * (displayHeight - 2 * SCREEN_MARGIN));
        imageIDs.get(imageIDs.size() - 1).load(context, cx, cy);
        invalidate();

    }

    public void removeAllImages() {
        imageIDs.removeAll(imageIDs);
        invalidate();
    }

    public void removeImage() {
        if (imageIDs.size() > 0) {
            imageIDs.remove(imageIDs.size() - 1);
        }
        invalidate();
    }

    public int getCountImage() {
        return imageIDs.size();
    }

    // ---------------------------------------------------------------------------------------------------
    @Override
    protected void onDraw(Canvas canvas) {
        Log.e(TAG, "onDraw: " );
        if (isErase){
            Log.e(TAG, "onDraw: isErase" );
            if (destBitmap != null){
                destCanvas.drawPath(destPath,destPaint);
                canvas.drawBitmap(destBitmap,0,0,null);
            }
        }
        else if (isMove){
            height = imageIDs.get(0).getHeight();
            width = imageIDs.get(0).getWidth();
            Log.e(TAG, "onDraw: isMove");
            canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
            canvas.drawPath(drawPath,drawPaint);
            int n = imageIDs.size();
            for (int i = 0; i < n; i++)
                imageIDs.get(i).draw(canvas);
        }
        super.onDraw(canvas);
    }

    // ---------------------------------------------------------------------------------------------------
    public void trackballClicked() {
        mUIMode = (mUIMode + 1) % 3;
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //collegeActivity.set
        int action = event.getAction();
        Log.e(TAG, "onTouchEvent: " );
        float touchX = event.getX();
        float touchY = event.getY();
        // respond to down, move and up events
        if (isErase){
            Log.e(TAG, "onTouchEvent: isErase" );
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    destPath.moveTo(touchX,touchY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    destPath.lineTo(touchX,touchY);
                    break;
            }

        } else if (isMove) {
            Log.e(TAG, "onTouchEvent: isMove" );
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    drawPath.moveTo(touchX, touchY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    drawPath.lineTo(touchX, touchY);
                    break;
                case MotionEvent.ACTION_UP:
                    drawPath.lineTo(touchX, touchY);
                    drawCanvas.drawPath(drawPath, drawPaint);
                    drawPath.reset();
                    break;
                default:
                    return false;
            }
            return multiTouchController.onTouchEvent(event);
        }
        invalidate();
        return true;
    }



    private void setupDrawing() {
        Log.e(TAG, "setupDrawing: " );
        // prepare for drawing and setup paint stroke properties
        drawPath = new Path();
        drawPaint.setAlpha(0);
        drawPaint.setColor(0);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(10);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);

    }

    // size assigned to view
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (w > 0 && h > 0) {
            Log.e(TAG, "onSizeChanged: " );
            super.onSizeChanged(w, h, oldw, oldh);
            width = w;
            height = h;
            canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            drawCanvas = new Canvas(canvasBitmap);
        }
    }
    // update color
    public void setColor(String newColor) {
        Log.e(TAG, "setColor: " );
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);
    }

    public MultiTouchEntity getDraggableObjectAtPoint(MultiTouchController.PointInfo pt) {
        float x = pt.getX(), y = pt.getY();
        int n = imageIDs.size();
        for (int i = n - 1; i >= 0; i--) {
            ImageEntity im = (ImageEntity) imageIDs.get(i);
            if (im.containsPoint(x, y))
                return im;
        }
        return null;
    }

    public void selectObject(MultiTouchEntity img, MultiTouchController.PointInfo touchPoint) {
        currTouchPoint.set(touchPoint);
        if (img != null) {
            // Move image to the top of the stack when selected
            drawPaint.setColor(Color.TRANSPARENT);
            imageIDs.remove(img);
            imageIDs.add(img);

            destCanvas = new Canvas();
            destBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
            destCanvas.setBitmap(destBitmap);
            destCanvas.drawBitmap(img.getBitmap(),0,0,null);

        } else {
            // Called with img == null when drag stops.
        }
        invalidate();
    }


    public void getPositionAndScale(MultiTouchEntity img, MultiTouchController.PositionAndScale objPosAndScaleOut) {
        // requires averaging the two scale factors)
        objPosAndScaleOut.set(img.getCenterX(), img.getCenterY(),
                (mUIMode & UI_MODE_ANISOTROPIC_SCALE) == 0,
                (img.getScaleX() + img.getScaleY()) / 2,
                (mUIMode & UI_MODE_ANISOTROPIC_SCALE) != 0, img.getScaleX(),
                img.getScaleY(), (mUIMode & UI_MODE_ROTATE) != 0,
                img.getAngle());
    }

    /**
     * Set the position and scale of the dragged/stretched image.
     */
    public boolean setPositionAndScale(MultiTouchEntity img,
                                       MultiTouchController.PositionAndScale newImgPosAndScale, MultiTouchController.PointInfo touchPoint) {
        currTouchPoint.set(touchPoint);
        boolean ok = img.setPos(newImgPosAndScale);
        if (ok)
            invalidate();
        return ok;
    }

    public boolean pointInObjectGrabArea(MultiTouchController.PointInfo pt, MultiTouchEntity img) {
        return false;
    }
}

0 个答案:

没有答案