1

我将在画布上绘制许多矩形来创建地图。我希望能够放大和缩小地图的不同部分。我查看了其他示例,但没有一个被证明是成功的。有任何想法吗?

public class Int_Map extends View {

private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;

public Int_Map(Context context) {
    super(context);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

public boolean onTouchEvent(MotionEvent event){
    mScaleDetector.onTouchEvent(event);

    Region region = new Region(0, 0, getWidth(), getHeight()/2);

    float x = event.getX();
    float y = event.getY();

    if(region.contains((int)x, (int)y))
    {
        Intent intent = new Intent(getContext(), Exhibit1.class);
        ((Activity)getContext()).startActivity(intent);
    }

    return true;

}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.scale(mScaleFactor, mScaleFactor);

    Rect rect = new Rect();
    rect.set(0,0, canvas.getWidth(), canvas.getHeight()/2);

    Drawable drawable = getResources().getDrawable(R.drawable.exhibit1);
    drawable.setBounds(rect);
    drawable.draw(canvas);

    canvas.restore();

}       

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}

}

4

0 回答 0