画布放大自定义视图

时间:2015-07-02 11:18:01

标签: android canvas

我想在我的应用程序中添加缩放画布的功能。我解释一下我的代码:

当我绘制时,我得到画布的位图并将其保存到ArrayList(这是用于撤消绘制,我尝试绘制路径并保存到路径arraylist但我无法绘制文本,所以我使用了该方法) 。 我有一个布尔变量来检查是否启用了缩放。这是因为当你按下屏幕时它会绘制一些东西(一条线,一个圆柱......),我在onDraw和onTouchEvent上使用这个变量来禁用绘图并启用缩放。

当我画画时没有问题但是当我尝试使用变焦时它什么也没做。我正在搜索很多关于缩放但我无法将其实现到我的应用程序中。以下代码是我尝试实现缩放的最后一个代码。我是从here得到的。

private boolean zoomenable=false;
private ArrayList<Bitmap> bmps=new ArrayList<Bitmap>();
private static float MIN_ZOOM=1f;
private static float MAX_ZOOM=5f;
private float scaleFactor=1.f;
private ScaleGestureDetector detector;
private static int NONE=0;
private static int DRAG=1;
private static int ZOOM=2;
private int mode;
private float startX=0f;
private float startY=0f;
private float translateX=0f;
private float translateY=0f;
private float previusTranslateX=0f;
private float previusTranslateY=0f;
private boolean dragged=false;
private float displayWidth;
private float displayHeight;

public DrawingView(Context context, AttributeSet attrs){
    super(context, attrs);
    detector=new ScaleGestureDetector(getContext(), new ScaleListener());
    WindowManager wm=(WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display=wm.getDefaultDisplay();
    displayHeight=display.getHeight();
    displayWidth=display.getWidth();

    //Draw setup (create path, paint, set color, stroke...)
}

protected void onDraw(Canvas canvas){

    if(zoomenable==true) {

        canvas.save();

        if(bmps.size()==1){
            bmps.add(canvasBitmap);
        }
        canvas.drawBitmap(bmps.get(bmps.size() - 1), 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);

        canvas.scale(scaleFactor, scaleFactor);
        if (translateX * -1 < 0) {
            translateX = 0;
        } else if ((translateX * -1) > (scaleFactor - 1) * displayWidth) {
            translateX = (1 - scaleFactor) * displayWidth;
        }

        if (translateY * -1 < 0) {
            translateY = 0;
        } else if ((translateY * -1) > (scaleFactor - 1) * displayHeight) {
            translateY = (1 - scaleFactor) * displayHeight;
        }
        canvas.translate(translateX / scaleFactor, translateY / scaleFactor);
        canvas.restore();
    }else {

    //Draw shapes preview    

    }
}

public boolean onTouchEvent(MotionEvent event){

    if(zoomenable==false) {

        //touch events for drawing

    }
    if(zoomenable==true){
        switch (event.getAction()&MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_DOWN:
                mode=DRAG;
                startX=event.getX()-previusTranslateX;
                startY=event.getY()-previusTranslateY;
                break;

            case MotionEvent.ACTION_MOVE:
                translateX=event.getX()-startX;
                translateY=event.getY()-startY;
                double distance=Math.sqrt(Math.pow(event.getX()-(startX+previusTranslateX),2)+Math.pow(event.getY()-(startY+previusTranslateY),2));
                if(distance>0){
                    dragged=true;
                }
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                mode=ZOOM;
                break;

            case MotionEvent.ACTION_UP:
                mode=NONE;
                dragged=false;
                previusTranslateY=translateY;
                previusTranslateX=translateX;
                break;

            case MotionEvent.ACTION_POINTER_UP:
                mode=DRAG;
                previusTranslateX=translateX;
                previusTranslateY=translateY;
                break;
        }
        detector.onTouchEvent(event);
        if((mode==DRAG&&scaleFactor!=1f&&dragged)||mode==ZOOM){
            invalidate();
        }
    }
    return true;
}

0 个答案:

没有答案