水平スクロールビュー内に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>
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);
}
....