Reputation: 1710
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?
Upvotes: 3
Views: 4677
Reputation: 1710
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);
Upvotes: 5
Reputation: 24233
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;
}
}
Upvotes: 0
Reputation: 11177
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.
Upvotes: 0
Reputation: 6270
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
Upvotes: 0