使用Seek Bar缩放android Canvas

时间:2014-08-28 12:08:05

标签: java android canvas zoom android-seekbar

这是我在stackoverflow中的第一个问题。任何帮助将非常感谢。 我正在尝试使用搜索栏缩放画布,因此依赖于搜索栏的进度,画布将放大并缩小。

这是主要活动中的代码:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
    CPaintView paint  =  (CPaintView) this.findViewById(R.id.cPaintView1);

    paint.zoomX = (float)progress;
    paint.zoomY = (float)progress;
    paint.invalidate();
}

然后它会将进度值传递给包含画布的CPaintView类:

public class CPaintView extends View implements OnTouchListener {

    final Paint paint = new Paint();
    int color = Color.parseColor("#FF0000");

    Bitmap offScreenBitmap;
    Canvas offScreenCanvas;

    final int CIRCLE = 0;
    final int TRIANGLE = 1;
    final int SQUARE = 2;
    int shape = CIRCLE;
    private int mActivePointerId;
    float zoomX = 0;
    float zoomY = 0;

    public CPaintView(Context context) {
        super(context);

        setup();
    }

    public CPaintView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setup();
    }

    public CPaintView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setup();
    }

    public void setup()
    {
        setOnTouchListener(this); // define event listener and start intercepting events 
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        // draw the off screen bitmap
        if(zoomX > 0)
        {
            canvas.save();
            canvas.scale(zoomX, zoomY);
            canvas.drawBitmap(offScreenBitmap, 0, 0, paint);
            canvas.restore();
        }
        else
            canvas.drawBitmap(offScreenBitmap, 0, 0, paint);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // get the x,y coordinates of the MotionEvent.ACTION_MOVE event

        int pointerIndex = 0;
        for(int i = 0; i < event.getPointerCount(); i++)
        {
            mActivePointerId = event.getPointerId(i);
            pointerIndex = event.findPointerIndex(mActivePointerId);
            float x = event.getX(pointerIndex);
            float y = event.getY(pointerIndex);

            float y1 = y + 20;
            float x2 = x + 20;
            float y2 = y1 - 10;
            paint.setColor(color);

            float verts[] = {x, y, x, y1, x2, y2};
            int[] vertsColors = {color, color, color, color, color, color};


            switch(shape)
            {
                case CIRCLE:
                    offScreenCanvas.drawCircle(x, y, 10, paint); break;// draw a red circle at the x,y coordinates specified by the user
                case TRIANGLE:
                    offScreenCanvas.drawVertices(Canvas.VertexMode.TRIANGLES, verts.length, verts, 0, null, 0, vertsColors, 0, null, 0, 0, paint);break;
                case SQUARE:
                    offScreenCanvas.drawRect(x-10, y-10, x+10, y+10, paint); break; 
            }

            invalidate();
        }
                return true;
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // create / re-create the off screen bitmap to capture the state of our drawing
        // this operation will reset the user's drawing
            offScreenBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            offScreenCanvas = new Canvas(offScreenBitmap);
    }
}

我遇到onDraw方法的问题。问题是,当我增加搜索栏的进度时,整个画布消失了,我试图在它上画画,看起来它并没有画出来。但当我将搜索栏进度减少到0时,所有绘图都会返回,我可以再次看到它,包括看起来不起作用的绘图。

我很抱歉我的英语不好。

非常感谢。

1 个答案:

答案 0 :(得分:0)

您的问题是规模如何运作的概念。在0,0的缩放是未定义的。在1f缩放,1f是默认值。缩放不起作用,

 if(zoomX > 0)
        {
            canvas.save();
            canvas.scale(zoomX, zoomY);
            canvas.drawBitmap(offScreenBitmap, 0, 0, paint);
            canvas.restore();
        }
        else
            canvas.drawBitmap(offScreenBitmap, 0, 0, paint);

如果缩放为0,则它​​将正确绘制缩放的位图。因为它会跳过缩放步骤。否则你有一个像50的缩放,这基本上可能是位图是一个点。尝试让缩放悬停在1左右。缩放= 12 /(1 +进度)然后缩放的范围可以从12x到小于零的值。你的绘图似乎应该在那里,它只是 WAAAAY 。除了在零处,您跳过该步骤并将其设为1。