画像を指定して、その画像の一部のみを拡大縮小できるようにしたいと思います。たとえば、画像の半分を拡大して、その半分がスペース全体を占めるようにします。
これはどのように可能ですか?
ImageView fitXYは、元の画像全体に対してのみ機能すると思ったので、機能しますか。
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = 640;
int newHeight = 480;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// create the new Bitmap object
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 50, 50, width,
height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(bmd);
imageView.setScaleType(ScaleType.CENTER);
linearLayout.addView(imageView, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(linearLayout);
}
}
これは、createBitmapで、ソースの最初のピクセルのX座標とY座標が0の場合にのみ機能します。つまり、画像のサブセットを取得できません。画像全体を拡大縮小することしかできません。ただし、createBitmapは画像をサブセット化することを目的としています。
ログで、パラメーターが0でない場合、次の例外が発生します。java.lang.IllegalArgumentException:x + widthは<=bitmap.width()である必要があります
助けてください