Zooming and Panning of view in android

Sunil Pandey picture Sunil Pandey · Apr 7, 2011 · Viewed 13.8k times · Source

Hi friends I am creating one app where i put two views. first is imageview and the other one is my customeview.

i am drawing image on my custom view. my custom view code is as follow

public class DrawView extends ImageView{
    @SuppressWarnings("unused")
    private static final String TAG = "DrawView";
    Painter painter = new Painter();
    PathStack pathStack;
    private DrawApplication drawApplication;

    public DrawView(Context context, AttributeSet attr) {
        super(context,attr);
        setFocusable(true);
        setFocusableInTouchMode(true);
        drawApplication=(DrawApplication)context.getApplicationContext();
        pathStack=drawApplication.getPainter().getPathStack();
        painter=drawApplication.getPainter();
        setScaleType(ScaleType.MATRIX);

    }

    @Override
    public void onDraw(Canvas canvas) {
        float scaleFactor=drawApplication.getScaleFactor();
        canvas.scale(scaleFactor, scaleFactor,(float)getWidth()/2,(float)getHeight()/2);
        List<DrawingPath> currentStack=pathStack.getCurrentStack();

        for (DrawingPath drawPath : currentStack) {
            drawPath.draw(canvas, painter);

        }

    }
}

many things clear from code like storing path on drawapplication etc.

now we have decided to provide zooming and panning to this view. and my drawing should also be zoomed accordingly along with imageview which in back.

also i should be able to draw correctly.

I tried canvas scaling but that is worst, because after scaling i am not able to draw to the touched point and also less drawing to the screen resulting in more drawing to the view.

drawing on widget is also very lengthy task results in performance slow down. plz provide any way.

and also i am using api level 10.

Edit1

Well friends. i got one idea. i have to draw path on different zoom level. so if we can calculate path ratio in different zoom level than it will solve my problem. plz provide help accordingly

Edit2

if we will be able to scale path then it can solve my problem

Edit3

i think if we draw it on bitmap than i can achieve my goal, but my problem is resizing bitmap and providing scrolling to bitmap on different zoom level

Answer

Vincent Mimoun-Prat picture Vincent Mimoun-Prat · Apr 7, 2011

Similar questions on SO:

Can be done on Honeycomb (API lvl 11) but not properly before.