如何以编程方式将 9-patch 图像重新应用于 ImageView? [英] How to programmatically re-apply a 9-patch image to an ImageView?

查看:44
本文介绍了如何以编程方式将 9-patch 图像重新应用于 ImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an activity that has an ImageView defined inside a HorizontalScrollView. The image source is a 9-patch file that is constrained to stretch only the right edge to fill the screen. I have implemented a simple zoom feature which allows the user to double tap to zoom in and out, by resizing the bitmap and assigning the new bitmap to the view. My current problem is that when doubling tapping to zoom back out, the 9-patch is not applied when I assign the new resized bitmap to the view. In other words, instead of stretching just the right edge as defined in the 9-patch file, it stretched the entire image.

这是我的 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>

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

Here is the relevant portion of my code, inside the onDoubleTap() call :

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

编辑:在做了一些研究之后,我想通了.不只是操作位图,我还需要包括 9-patch 块,它不是位图图像的一部分,以重新构建一个新的 9-patch 可绘制对象.请参阅下面的示例代码:

EDIT: After doing some research, I have it figured out. Instead of just manipulating the bitmap, I need to also include the 9-patch chunk, which is not part of the bitmap image, to re-construct a new 9-patch drawable. See sample code below:

    ...
 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);
 }

  ....

推荐答案

EDIT:经过一番研究,我想通了.不只是操作位图,我还需要包括 9-patch 块,它不是位图图像的一部分,以重新构建一个新的 9-patch 可绘制对象.请参阅下面的示例代码:

EDIT: After doing some research, I have it figured out. Instead of just manipulating the bitmap, I need to also include the 9-patch chunk, which is not part of the bitmap image, to re-construct a new 9-patch drawable. See sample code below:

    ...
 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);
 }

  ....

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

EDIT #2: For those who looks at my solution here, also take a look at Kai's suggestions below regarding memory management. Very useful information.

这篇关于如何以编程方式将 9-patch 图像重新应用于 ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆