Android how do I zoom and scroll a bitmap which is

2020-08-01 06:01发布

问题:

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.