如何以编程方式重新申请一个9补丁图像的ImageView的? [英] How to programmatically re-apply a 9-patch image to an ImageView?

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

问题描述

我有,有一个Horizo​​ntalScrollView中定义一个ImageView的活动。图象源是被限制成仅伸展的右边缘以填充屏幕9补丁文件。我实现了一个简单的缩放功能,它允许用户双击放大和缩小,通过调整位图和分配新的位图到视图。我现在的问题是,加倍攻来放大出来的时候,9补丁不是我分配新调整大小的位图到视图应用。代替拉伸就像在9-补丁文件中定义的右边缘换句话说,它伸展整个图像。

下面是我的XML:

 < Horizo​​ntalScrollView
            机器人:ID =@ + ID / HSCROLL
            机器人:fillViewport =真
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:fadingEdge =无>

            < RelativeLayout的
                机器人:ID =@ + ID / rlayoutScrollMap
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT>

                < ImageView的
                    机器人:ID =@ + ID / imgResultMap
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =FILL_PARENT
                    机器人:scaleType =fitXY
                    机器人:SRC =@可绘制/ map_base/>

            < / RelativeLayout的>
        < / horizo​​ntalScrollView>
 

下面是我的code中的相关部分,该onDoubleTap()调用内部的:

 公共布尔onDoubleTap(MotionEvent E)
                {
                    如果(变焦== 1){
                        变焦= 2; // 缩小
                    } 其他 {
                        变焦= 1; // 放大
                    }
                    位图图像= BitmapFactory.de codeResource(getResources(),R.drawable.map_base);
                    BMP位= Bitmap.createScaledBitmap(图像,image.getWidth()*变焦,image.getHeight()*变焦,假);
                    ImageView的imgResultMap =(ImageView的)findViewById(R.id.imgResultMap);
                    imgResultMap.setImageBitmap(BMP);

                    返回false;
                }
 

修改:做一些研究之后,我把它想通了。而不是只操纵位图的,我需要还包括9的贴剂组块,这是不位图图像的一部分,以重新构造新的9-补丁抽拉。请参见下面的示例code:

  ...
 其他 {
        // 缩小
        变焦= 1;
        位图mapBitmapScaled = mapBitmap;
        //将9补丁数据块,并应用到视图
        byte []的块= mapBitmap.getNin​​ePatchChunk();
        NinePatchDrawable mapNinePatch =新NinePatchDrawable(getResources()
            mapBitmapScaled,块,新的矩形(),NULL);
        imgResultMap.setImageDrawable(mapNinePatch);
 }

  ....
 

解决方案

修改:做一些研究之后,我把它想通了。而不是只操纵位图的,我需要还包括9的贴剂组块,这是不位图图像的一部分,以重新构造新的9-补丁抽拉。请参见下面的示例code:

  ...
 其他 {
        // 缩小
        变焦= 1;
        位图mapBitmapScaled = mapBitmap;
        //将9补丁数据块,并应用到视图
        byte []的块= mapBitmap.getNin​​ePatchChunk();
        NinePatchDrawable mapNinePatch =新NinePatchDrawable(getResources()
            mapBitmapScaled,块,新的矩形(),NULL);
        imgResultMap.setImageDrawable(mapNinePatch);
 }

  ....
 

编辑#2 :对于那些谁看我的解决方案在这里,也来看看关于内存管理下面凯的建议。非常有用的信息。

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.

Here is my 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>

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

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: 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: For those who looks at my solution here, also take a look at Kai's suggestions below regarding memory management. Very useful information.

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

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