如何以编程方式将9补丁图像重新应用于ImageView?

Ken*_*nny 5 android android-widget nine-patch android-layout android-imageview

我有一个活动,在Horizo​​ntalScrollView中定义了ImageView.图像源是一个9补丁文件,仅限于拉伸右边缘以填充屏幕.我已经实现了一个简单的缩放功能,允许用户通过调整位图大小并将新位图分配给视图来双击放大和缩小.我目前的问题是,当双击点击以缩小时,当我将新的已调整大小的位图分配给视图时,不会应用9补丁.换句话说,它不是像9补丁文件中那样只拉伸右边缘,而是拉伸整个图像.

这是我的XML:

<HorizontalScrollView
            android:id="@+id/hScroll"
            android:fillViewport="true"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:fadingEdge="none" >

            <RelativeLayout
                android:id="@+id/rlayoutScrollMap"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <ImageView
                    android:id="@+id/imgResultMap"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:scaleType="fitXY"
                    android:src="@drawable/map_base"/>

            </RelativeLayout>
        </horizontalScrollView>
Run Code Online (Sandbox Code Playgroud)

这是我的代码的相关部分,在onDoubleTap()调用中:

public boolean onDoubleTap(MotionEvent e)  
                {  
                    if (zoom == 1) {
                        zoom = 2; // zoom out
                    } else {
                        zoom = 1; // zoom in
                    }
                    Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.map_base);            
                    Bitmap bmp = Bitmap.createScaledBitmap(image, image.getWidth() * zoom, image.getHeight() * zoom, false);
                    ImageView imgResultMap = (ImageView)findViewById(R.id.imgResultMap);
                    imgResultMap.setImageBitmap(bmp);

                    return false; 
                } 
Run Code Online (Sandbox Code Playgroud)

编辑:经过一些研究,我发现它.我不仅要操纵位图,还需要包含不是位图图像一部分的9补丁块,以重新构建一个新的9补丁drawable.请参阅以下示例代码:

    ...
 else {
        // Zoom out
        zoom = 1;
        Bitmap mapBitmapScaled = mapBitmap; 
        // Load the 9-patch data chunk and apply to the view
        byte[] chunk = mapBitmap.getNinePatchChunk();
        NinePatchDrawable mapNinePatch = new NinePatchDrawable(getResources(), 
            mapBitmapScaled, chunk, new Rect(), null);
        imgResultMap.setImageDrawable(mapNinePatch);
 }

  ....
Run Code Online (Sandbox Code Playgroud)

Ken*_*nny 10

编辑:经过一些研究,我发现它.我不仅要操纵位图,还需要包含不是位图图像一部分的9补丁块,以重新构建一个新的9补丁drawable.请参阅以下示例代码:

    ...
 else {
        // Zoom out
        zoom = 1;
        Bitmap mapBitmapScaled = mapBitmap; 
        // Load the 9-patch data chunk and apply to the view
        byte[] chunk = mapBitmap.getNinePatchChunk();
        NinePatchDrawable mapNinePatch = new NinePatchDrawable(getResources(), 
            mapBitmapScaled, chunk, new Rect(), null);
        imgResultMap.setImageDrawable(mapNinePatch);
 }

  ....
Run Code Online (Sandbox Code Playgroud)

编辑#2:对于那些在这里查看我的解决方案的人,还要看看Kai关于内存管理的建议.非常有用的信息.