位图不缩放只是拖动正在发生请帮助我获得zoomin和缩小

时间:2012-05-28 12:52:46

标签: android

你是Android平台的新手 在我的代码中只有拖动发生并且没有发生缩放请帮帮我

这是使用

的代码
@Override
public void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub



    canvas.drawBitmap(image, getBounds().left,getBounds().top, new Paint());



}

/* (non-Javadoc)
 * @see com.simplogics.surfaceview.Views.BaseView#onTouchEvent(android.view.MotionEvent)
 */


static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;


@Override

public boolean onTouchEvent(MotionEvent event) {
    boolean handled = false;

    Log.d("SimpEditText", "(int)event.getX() " + (int)event.getX());
    Log.d("SimpEditText", "(int)event.getY() " + (int)event.getY());

    if(isTouchedInBounds((int)event.getX(), (int)event.getY())){
        Log.d("SimpEditText", "Touched in Bounds");
        handled = true;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            lastPosition.x = getBounds().left;
            lastPosition.y = getBounds().top;
        //  initialRotation = rotation(event);
            Log.d("VerticalLabelView", "mode=DRAG");
            mode = DRAG;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            mode = ZOOM;

            initialScale = spacing(event);
            midPoint(mid, event);
            Log.d("VerticalLabelView", "mode=ZOOM");
            initialRotation = rotation(event);

            break;
        case MotionEvent.ACTION_UP:
            mode = NONE;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            Log.d("VerticalLabelView", "mode=NONE");
            break;
        case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
                int x = (int)(event.getX() - start.x);
                int y = (int)(event.getY() - start.y);
                setPosition((int)lastPosition.x + x, (int)lastPosition.y + y);



            }else if(mode == ZOOM){

                currentScale = spacing(event);

                float scale =currentScale/initialScale;

                currentRotation = rotation(event);
                float angle = currentRotation - initialRotation;
                if(getImage().getWidth()> 100 && image.getWidth() <900){
                    /*Matrix testMatrix = new Matrix();
                    testMatrix.postScale(scale, scale,mid.x,mid.y);*/
                /*  rotatedImage = Bitmap.createBitmap(getImage(), 0, 0, (int)(image.getWidth()*scale),
                            (int)(image.getHeight()*scale), testMatrix, true);
                */
                    rotatedImage = Bitmap.createScaledBitmap(getImage(), (int)(image.getWidth()*scale), 
                        (int)(image.getHeight()*scale), true);
                setScale(getBounds().left,getBounds().top
                        , (int)(getImage().getWidth()*scale), (int)(getImage().getHeight()*scale));

                /*rotatedImage =rotate(angle, image);
                Matrix testMatrix = new Matrix();
                testMatrix.postScale(scale, scale,mid.x,mid.y);
                this.image = rotatedImage;
                rotatedImage = Bitmap.createScaledBitmap(image, (int)(image.getWidth()*scale), 
                        (int)(image.getHeight()*scale), true);
                this.image = rotatedImage;*/
                /*Matrix testMatrix = new Matrix();
                testMatrix.postScale(scale, scale,mid.x,mid.y);*/
            //  this.image = rotatedImage;
                /*rotatedImage = Bitmap.createScaledBitmap(image, (int)(image.getWidth()*scale), 
                        (int)(image.getHeight()*scale), true);
                this.image = rotatedImage;*/
                }

                this.image = rotatedImage;
            }
            break;
        }


    }


    return handled;

}

这是我使用的onDraw和touchevent方法

请帮帮我......

1 个答案:

答案 0 :(得分:0)

Hy try this教程,找出问题所在:

通过本教程,您可以学习许多功能,如:

  1. 拖动图片
  2. 使用捏缩放(双指)放大
  3. 使用捏缩放(两个手指)缩小
  4. 希望您正在寻找