Android image scaling

I want to add an image to my application that looks like a roadmap image. I want to add zoom features to this image, as well as horizontal scrolling when the image is zoomed.

I wrote some code, but it only allows you to scale and not scroll.

the 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 solve this problem. Is my code enough or do you have better code?

+3
source share
4 answers

what worked for me: just add the built-in zoom controls to the 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); 
+5
source

Take a look at this post: How do I scale images? You must use webview.loadUrl ("file: // ...") to load the image and activate the zoom function

0
source

You are changing the boundaries of the image, not changing the boundaries of the view. Try changing the height and width of the view. Then you can scroll.

You must override the onMeasure() method and set setMeasuredDimension to the current height and width. Maintain height and width as elements.

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; } } 
0
source

You will get very bad results when trying to handle multi-touch with loadUrl. Try this tutorial , it provides the best source code I found for this. If you change the code a bit, you can make it work with 2.1 as well.

0
source

Source: https://habr.com/ru/post/1494095/


All Articles