0

我在 Draw 方法上有以下内容:

 @Override
protected void onDraw(Canvas canvas) {
    //        super.onDraw(canvas);
    canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint); //Deseneaza Bitmapul mutabil
    if (PaintActivity.isPic == 1) {
        rect = new Rect((int)mX, (int)mY, ((int)mX+bitmaps.get(lastBitmap).bitmap.getWidth()), ((int)mY+bitmaps.get(lastBitmap).bitmap.getHeight()));
        rectscale = new Rect((int)mX-20, (int)mY-20, (int)mX+20, (int)mY+20);
        rectrotate = new Rect((int)((mX+bitmaps.get(lastBitmap).bitmap.getWidth())-20), (int)mY-20, (int)(mX+bitmaps.get(lastBitmap).bitmap.getHeight()+20), (int)mY+20);
        for(int i=0;i<bitmaps.size();i++){
            if((lastBitmap != i)&& (bitmaps.get(i)!= null) ){
                if(bitmaps.get(i).isRotate){
                    matrixRotate.setRotate(bitmaps.get(i).rectrotateVal, bitmaps.get(i).pX+(bitmaps.get(i).bitmap.getWidth() / 2), bitmaps.get(i).pY+(bitmaps.get(i).bitmap.getHeight() / 2)); //rotate it
                }else{
                    matrixRotate.setRotate(0, bitmaps.get(i).pX+(bitmaps.get(i).bitmap.getWidth() / 2), bitmaps.get(i).pY+(bitmaps.get(i).bitmap.getHeight() / 2)); //rotate it
                }
                canvas.setMatrix(matrixRotate);
                canvas.drawBitmap(bitmaps.get(i).bitmap, bitmaps.get(i).pX,  bitmaps.get(i).pY, mBitmapPaint);
                canvas.setMatrix(null);
                canvas.drawCircle(bitmaps.get(i).pX, bitmaps.get(i).pY, 10, cPaint);
                canvas.drawCircle(bitmaps.get(i).pX+bitmaps.get(i).bitmap.getWidth(), bitmaps.get(i).pY, 10, cPaint);
            }
        }
        if (bitmaps.get(lastBitmap).bitmap != null) {
            bitmaps.get(lastBitmap).rectscale = rectscale;
            bitmaps.get(lastBitmap).rectrotate = rectrotate;
            if(isScalling){
                rect = new Rect((int)mX,(int) mY, (int)tempx, (int)tempy);
                int w = Math.abs(rect.width());
                int h = Math.abs(rect.height());
                canvas.drawRect(rect, rPaint);
                if((w!=0)&&(h!=0)){
                    bitmaps.get(lastBitmap).bitmap = getResizedBitmap(bitmaps.get(lastBitmap).bitmap2, h, w, bitmaps.get(lastBitmap).bitmap.getConfig());
                }
            }
            if(isRotating){
                rotateVal = (float) 45;
                bitmaps.get(lastBitmap).rectrotateVal = rotateVal;
                bitmaps.get(lastBitmap).isRotate = isRotating;
                matrixRotate.setRotate(rotateVal, mX+(bitmaps.get(lastBitmap).bitmap.getWidth() / 2), mY+(bitmaps.get(lastBitmap).bitmap.getHeight() / 2)); //rotate it
                canvas.setMatrix(matrixRotate);
                canvas.drawBitmap(bitmaps.get(lastBitmap).bitmap,  mX, mY, mBitmapPaint);
                canvas.setMatrix(null);
                canvas.drawCircle(bitmaps.get(lastBitmap).pX+bitmaps.get(lastBitmap).bitmap.getWidth(), bitmaps.get(lastBitmap).pY, 10, cPaint);
                canvas.drawRect(bitmaps.get(lastBitmap).rect, rPaint);
            }else{
                canvas.drawBitmap(bitmaps.get(lastBitmap).bitmap, mX, mY, mBitmapPaint);
                canvas.drawCircle(mX, mY, 10, cPaint);
                canvas.drawCircle(mX+bitmaps.get(lastBitmap).bitmap.getWidth(), mY, 10, cPaint);
            }
            bitmaps.get(lastBitmap).pX = mX;
            bitmaps.get(lastBitmap).pY = mY;
            bitmaps.get(lastBitmap).rect = rect;
            bitmaps.get(lastBitmap).rectscale = rectscale;
            bitmaps.get(lastBitmap).rectrotate = rectrotate;
        }
        for(int j=0;j<textArray.size();j++){
            if (textArray.get(j) != null) {
                canvas.drawText(textArray.get(j), tXArray.get(j), tYArray.get(j), textPaintArray.get(j));
            }
        }
    } 

我有许多位图,“矩形”矩形与图片的宽度和高度相同,因此,如果我按下图片,它会将我设置在上面,以移动它。rectscale = 是一个位于图片左上角的矩形,我画一个圆圈,如果我按下圆圈,那么我可以缩放我的图片。rectrotate = 相同的东西,但在图片的右上角。我按下它,它会将我的图片旋转 45 度。我有以下触摸事件:

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    canvas.drawPoint(x, y, paint);
    mX = x;
    mY = y;
    if(PaintActivity.isPic == 1){
        if(bitmaps.size() > 0){
            for(int i = 0;i<(bitmaps.size()-1);i++){
                if(bitmaps.get(i).rect.contains((int)mX,(int) mY)){
                    lastBitmap = i;
                }
            }
        }
        if(bitmaps.size() > 0){
            for(int i = 0;i<(bitmaps.size());i++){
                if(bitmaps.get(i).rectscale.contains((int)mX,(int) mY)){
                    lastBitmap = i;
                    isScalling  = true;
                    tempx = mX+bitmaps.get(lastBitmap).bitmap.getWidth();
                    tempy = mY+bitmaps.get(lastBitmap).bitmap.getHeight();
                }
            }
        }
        if(bitmaps.size() > 0){
            for(int i = 0;i<(bitmaps.size());i++){
                if(bitmaps.get(i).rectrotate.contains((int)mX,(int) mY)){
                    lastBitmap = i;
                    isRotating = true;
                }
            }
        }
    }
}

private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
        Log.d("move", "mx, my=" + mX + " " + mY);
        // mPath.moveTo(mX, mY);
    }
    // invalidate();
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    mPath.moveTo(mX, mY);
    // commit the path to our offscreen
    canvas.drawPath(mPath, paint);
    //        canvas.save();
    // kill this so we don't double draw
    mPath.reset();
    if(PaintActivity.isPic == 1){
        if(isScalling){
            isScalling = false;
        }
        if(isRotating){
            isRotating = false;
        }
        lastBitmap = bitmaps.size()-1;
        mX= bitmaps.get(lastBitmap).pX;
        mY= bitmaps.get(lastBitmap).pY;
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);

        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }
    return true;
}

如果我评论以下几行:

canvas.setMatrix(matrixRotate);   

和:

canvas.setMatrix(null);

这一切都有效,除了旋转。但是如果我不评论它们,旋转就可以了,第一张图片还可以,但是对于其余的图片,矩形有点乱。它们不是从位图的左上角(我的刻度圈所在的位置)开始,而是从较低的某个位置开始,几乎在位图的一半处。所以我可以使用我的缩放和旋转功能,但我必须猜测把手指放在哪里,因为画圆的点不包含在矩形中。有什么想法我可以解决这个问题吗?

编辑:我试过:在将矩阵设置为矩阵旋转之前:

origMatrix = canvas.getMatrix();

而不是:

canvas.setMatrix(null);

我试过了:

canvas.setMatrix(origMatrix);

它仍然弄乱了我的矩形,但至少现在它绘制了用于旋转和缩放矩形所在位置的圆圈(通常位于位图中间的某个位置)

4

1 回答 1

0

我改用这个:

canvas.save();
matrixRotate.setRotate(bitmaps.get(i).rectrotateVal, bitmaps.get(i).pX+(bitmaps.get(i).bitmap.getWidth() / 2), bitmaps.get(i).pY+(bitmaps.get(i).bitmap.getHeight() / 2));
canvas.drawBitmap(bitmaps.get(i).bitmap, matrixRotate , mBitmapPaint);
canvas.restore();
于 2012-11-12T15:36:32.227 回答