2

画像を指定して、その画像の一部のみを拡大縮小できるようにしたいと思います。たとえば、画像の半分を拡大して、その半分がスペース全体を占めるようにします。

これはどのように可能ですか?

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()である必要があります

助けてください

4

2 に答える 2

2

最初に、スケーリングする部分を使用して新しいビットマップを作成する必要があります

createBitmap() //pass the source bitmap, req height and width

結果のビットマップから、次を使用してスケーリングされたビットマップを作成する必要があります

createScaledbitmap() //pass the result bitmap , req width, height

例:

Bitmap originalBitmap = BitmapFactory.decodeResource(res, id);
Bitmap partImage = originalBitmap.createBitmap(width, height, config);
Bitmap scaledImage = partImage.createScaledBitmap(partImage, dstWidth, dstHeight, filter);
于 2012-08-12T06:54:38.853 に答える
1

そのため、いくつかのタイプミスを修正する必要がありましたが、この例はうまく機能しました。http://www.anddev.org/resize_and_rotate_image_-_example-t621.html 入力ミス:

int width = bitmapOrg.width();
int height = bitmapOrg.height();

なる:

int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();

それ以外の場合は、SDK 7 で試してみるとうまくいきました

于 2011-04-20T22:12:21.747 に答える