3

I want to add an image in my application which is like a road map image. I want to add zooming features to this image as well as horizontal scrolling when the image is zoomed.

I have written some code but it allows only zooming and not scrolling.

Code:

public class Zoom extends View {
    private Drawable image;
    private int zoomControler=20;
    public Zoom(Context context)
    {
            super(context);
            image=context.getResources().getDrawable(R.drawable.boothmap);
            setFocusable(true);

    }
    protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
            image.draw(canvas);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

            if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
                    zoomControler+=10;
            if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
                    zoomControler-=10;
            if(zoomControler<10)
                    zoomControler=10;

            invalidate();
            return true;
    }
}

Can you help me solving this problem. Is my code proper so far or do you have any better code?

Gaurav
  • 1,700
  • 4
  • 22
  • 38

4 Answers4

5

what worked for me is : just add built in zoom controls to webview and then call its loadDataWithBaseURL method.

webview.getSettings().setBuiltInZoomControls(true);
webview.loadDataWithBaseURL("file:///android_asset/", "<img src='file:///android_res/drawable/example.png' />", "text/html", "utf-8", null);
Gaurav
  • 1,700
  • 4
  • 22
  • 38
0

Look at this post : How can I get zoom functionality for images? You have to use webview.loadUrl("file://...") to load your image and activate zoom option

Community
  • 1
  • 1
Anass
  • 6,132
  • 6
  • 27
  • 35
  • I have this image in drawable folder and not as URL – Gaurav Aug 17 '11 at 12:38
  • Use webView.loadDataWithBaseURL("file:///android_asset/", "", "text/html", "utf-8", null); – Anass Aug 17 '11 at 12:46
  • I removed my code and instead just used loadUrl() method. Now it scrolls but doesnot zoom in or zoom out plus the image looks blurred now – Gaurav Aug 17 '11 at 17:20
0

You are modifying the image bounds instead of modofying the view bounds. Try modifying the view height and width. You should be able to scroll then.

Will need to override onMeasure() method and set setMeasuredDimension as current height and width. Maintain the height and width as members.

EDIT:

class YourView extends View {
static Bitmap mOrignalBitmap = null; // check n load in constructor.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(mWidth,mHeight); 
}

@Override
protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect src = new Rect(0,0, originalWidth, originalHeight);
        Rect dst = new Rect(viewLeft, viewTop, viewRight, viewBottom);
        canvas.drawBitmap(mOrignalBitmap , src, dst, null);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
                zoomControler+=10;
        if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
                zoomControler-=10;
        if(zoomControler<10)
                zoomControler=10;
        mWidth = //set view width with zoom
        mHeight = // set view height with zoom.
        requestLayout();

        return true;
}

}
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Sorry I am fairly new to android so I dont know how to use this method. If you can guide me, it will be great – Gaurav Aug 17 '11 at 17:15
  • I have just added this in my code: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec,heightMeasureSpec); } I dont know what to do now – Gaurav Aug 17 '11 at 17:41
0

You'll get very bad results by trying to handle multi-touch with loadUrl. Try this tutorial, It provides the best source code I found to do that. If you tweak a bit the code you can make it work on 2.1 as well.

Romain Piel
  • 11,017
  • 15
  • 71
  • 106