3

次のコードで画像を回転しようとしましたが、生成された画像がどんどん大きくなることがわかりました。

Matrix matrix = new Matrix();
matrix.setRotate(10);
Bitmap newImage = Bitmap.createBitmap(image, 0, 0, 
        image.getWidth(), image.getHeight(), matrix, true);

写真を見ることができます:

オリジナル

ここに画像の説明を入力

10度回転

ここに画像の説明を入力

もう一度10度回転

ここに画像の説明を入力

もう一度10度回転

ここに画像の説明を入力

青い長方形は完全なイメージです。

画像がどんどん大きくなっているのがわかりますが (ソファのサイズは変更されていません)、元の画像の 4 隅が後で新しい画像の境界線上にありません。

角を境界線に保つようにコードを変更するにはどうすればよいですか (2 番目の画像のように)?

github でデモ プロジェクトを作成したことを忘れていました。クローンできます。メインの Java コードは次のとおりです。

https://github.com/freewind/Android-RotateTest/blob/master/src/com/example/MyActivity.java

4

3 に答える 3

4

私はあなたのコードを試しましたが、回転した後、OutOfMemory例外でクラッシュし、新しいビットマップが作成されるたびに、非常にリソースを消費します。あなたは決して決してすべきではありません!反復でcreateBitMap()を使用します。画像の回転コードに変更を加えましたが、期待どおりに実行されています。
コードは次のとおりです。

private void addListeners() {
    this.button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            Matrix matrix = new Matrix();

            //copying the image matrix(source) to this matrix 
            matrix.set(imageView.getImageMatrix());

            matrix.postRotate(10, imageView.getWidth()/2, imageView.getHeight()/2);
            imageView.setImageMatrix(matrix);

            //checking the size of the image
            Drawable d = imageView.getDrawable();
            Bitmap bmp = ((BitmapDrawable)d).getBitmap();
            imageInfo(bmp);
        }
    });
}

また、imageViewのスケールタイプをマトリックスに設定します

<ImageView
    android:id="@+id/Image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#336699"
    android:scaleType="matrix"
    android:padding="2px"
    android:src="@drawable/m" />

また、ImageViewから回転したビットマップを取得する場合は、次のようにします。

private Bitmap getBitmapFromView() {
    // this is the important code :)
    // Without it the view will have a dimension of 0,0 and the bitmap will be null
    imageView.setDrawingCacheEnabled(true);
    imageView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
    imageView.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(imageView.getDrawingCache());
    imageView.setDrawingCacheEnabled(false); // clear drawing cache
    return b;
}

これがお役に立てば幸いです。

于 2012-08-28T18:44:03.977 に答える
1

あなたの画像が見えないようですが、同じ問題がありました。画像を回転させようとするたびに、画像のサイズが変更されたように見えました。

私はなんとか自分のイメージをうまく変えることができるいくつかのコードを書くことができました。私の最初のヒントは、アニメーションの回転する画像を作成しようとするときに、毎回新しいビットマップを作成するべきではないということです。これにより、GCが狂ってしまい、パフォーマンス速度が低下します。

サイズを変更せずに画像を回転させるために機能するコードは次のとおりです。

public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {

        float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
        float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();

        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postScale(scaleWidth, scaleHeight);
        rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
        rotateMatrix.postTranslate(shape.getX(), shape.getY());


        return rotateMatrix;

    }

注:シェイプオブジェクトには、スピンしようとしているオブジェクトの実際の寸法(100x100など)が含まれているだけです。

これがお役に立てば幸いです。

于 2012-08-28T17:10:37.663 に答える
1

最初の画像を回転する必要があります。最初のイメージを変数に保存してから、コピーを作成します。回転するときは、常にオリジナルから新しいコピーを作成し、それを回転させます。

編集

コードを次のように変更します。

 @Override
        public void onClick(View view) {
            Matrix matrix = new Matrix();
            matrix.setRotate(10);
            Bitmap copy = image;
            Bitmap newImage = Bitmap.createBitmap(copy, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
            setNewImage(newImage);
        }
于 2012-08-28T16:55:44.943 に答える