Android how do I zoom and scroll a bitmap which is created on the fly

烈酒焚心 提交于 2019-12-21 22:41:43

问题


In my application, i'm using the following code for holding a map which is downloaded from a server.

<?xml version="1.0" encoding="utf-8"?>

    <ScrollView android:id="@+id/scrollView1" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1.0">

        <ImageView android:id="@+id/mapImageView" 
            android:layout_height="wrap_content"  
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"                 
            android:scaleType="center"/>

    </ScrollView>

currently, i can scroll the image up and down only. I need to have the ability to scroll the image to all directions and also to zoom it in and out. How it can be done? Thanks, Eyal.


回答1:


There's a very simple ImageViewTouch that Sephiroto made based on the Gallery application code : http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/

I use it in a project and it works perfectly, with pinch-to-zoom and everything you might want. By default, when you set an image to it, the image will be scaled down to fit the screen.

edit : if you want to prevent the automatic image resize, you can change the ImageViewTouchBase.getProperBaseMatrix method to the following :

protected void getProperBaseMatrix(RotateBitmap bitmap, Matrix matrix) {
    float viewWidth = getWidth();
    float viewHeight = getHeight();
    float w = bitmap.getWidth();
    float h = bitmap.getHeight();
    matrix.reset();
    matrix.postConcat(bitmap.getRotateMatrix());
    matrix.postTranslate((viewWidth - w) / 2, (viewHeight - h) / 2);
}

This should center the image without resizing it.



来源:https://stackoverflow.com/questions/6563614/android-how-do-i-zoom-and-scroll-a-bitmap-which-is-created-on-the-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!