我有一个在ImageView中定义了HorizontalScrollView的活动。图像源是一个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>下面是我的代码的相关部分,在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;
} 编辑:在做了一些调查之后,我想出了答案。不只是操作位图,我还需要包括9补丁块,这不是位图图像的一部分,以重新构建一个新的9补丁绘图。见下面的示例代码:
...
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);
}
....发布于 2012-05-17 16:45:54
编辑:在做了一些调查之后,我想出了答案。不只是操作位图,我还需要包括9补丁块,这不是位图图像的一部分,以重新构建一个新的9补丁绘图。见下面的示例代码:
...
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);
}
....编辑#2:对于那些在这里查看我的解决方案的人,也请看一下凯在下面关于内存管理的建议。非常有用的信息。
发布于 2012-05-17 02:01:32
为什么你不只是包含两个不同的缩放图像,并在它们之间切换使用imgResultMap.setImageResource(resId)时缩放?还要注意,在UIThread中加载和创建位图并不是提供平滑用户体验的好方法,至少在onCreate()和缓存期间只预加载一次位图。
发布于 2012-05-16 17:53:21
将以下内容:ImageView imgResultMap = (ImageView)findViewById(R.id.imgResultMap);移到onCreate方法中的全局属性中。否则,设备必须搜索视图,并在每次点击时找到它。
并在从imgResultMap.invalidate();方法(http://developer.android.com/reference/android/view/View.html#invalidate())返回之前尝试调用http://developer.android.com/reference/android/view/View.html#invalidate()
https://stackoverflow.com/questions/10623570
复制相似问题