1

画像は大きいのですが、その一部だけを画面に表示する必要があります。私はこれをしました。私の問題は、画像を指で回転させる必要があることです。以下のコードは、小さな画像で正常に機能します。しかし、私の画像では、画像を上下に再配置し、奇妙に回転させるという間違った動作をしています。画像を正常に回転させるにはどうすればよいですか?, 小さな画像として. それから私は色に触れることができなければなりません。ここで答えが得られますが、まだ取り組まなければなりません。しかし、最初に、回転の問題をどのように解決できるか教えてください.2番目の問題についてアイデアがあれば、色に触れたいと思います.

これは画像です:

これがイメージです

画面には次のように表示されます。

これが画面に表示される方法です

XML レイアウト:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView android:id="@+id/imag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/roata"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="-200dp"
    android:scaleType="center"/>
 </RelativeLayout>

Java コード:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    picture = (ImageView)findViewById(R.id.imag);
    picture.setOnTouchListener(onTableTouched);
}

public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener()
{
    public boolean onTouch(View v, MotionEvent evt)
    {
        double r = Math.atan2(evt.getX() - picture.getWidth() / 2, picture.getHeight() / 2 - evt.getY());
        int rotation = (int) Math.toDegrees(r);


        if (evt.getAction() == MotionEvent.ACTION_DOWN)
        {
            //
        }

        if (evt.getAction() == MotionEvent.ACTION_MOVE)
        {
           updateRotation(rotation);
        }

        if (evt.getAction() == MotionEvent.ACTION_UP)
        {
            //
        }

        return true;
    }
};

private void updateRotation(double rot)
{
    float newRot = new Float(rot);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);

    Matrix matrix = new Matrix();
    matrix.postRotate(newRot - 50);

    Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    picture.setImageBitmap(redrawnBitmap);
}

更新: 私はどこにもBitmap行けなかったので、できるだけ似たものを得るMatrixために調整しようとしました. RotateAnimtaion()2 番目のパラメーターとアニメーション ダイナミクスの期間を変更すると、Y 位置を変更せずに同様の結果を得ることができると思います。さて、問題は画像がトリミングされていることですscaleType="center". コードとスクリーンショットを投稿します。これは改善できますか?

Animation a = new RotateAnimation(0.0f, param,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    a.setFillEnabled(true);
    a.setFillAfter(true);
    a.setDuration(param);

  picture.startAnimation(a);

ここに画像の説明を入力

4

2 に答える 2

1

Bitmapを次のようにグローバルに宣言するだけです。

private WeakReference<Bitmap> bitmap;

と使用

bitmap =new WeakReference(BitmapFactory.decodeResource(getResources(), R.drawable.roata));

それ以外の

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);
于 2012-07-02T09:28:05.953 に答える
1

問題は、すべての updateRotation() 呼び出しで同じビットマップを再描画しようとしていることだと思います。

この行を削除するのはどうですか、

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);

fromupdateRotation(double rot)をグローバルに宣言しているため、同じビットマップを何度も作成する必要がありません。これが、OutOfMemory を取得している理由です。

これを試して、

Bitmap bitmap=null;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    picture = (ImageView)findViewById(R.id.imag);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roata);
    picture.setOnTouchListener(onTableTouched);
}


public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent evt) {
        double r = Math.atan2(evt.getX() - picture.getWidth() / 2, picture.getHeight() / 2 - evt.getY());
        int rotation = (int) Math.toDegrees(r);

        if (evt.getAction() == MotionEvent.ACTION_DOWN) {
            //
        }

        if (evt.getAction() == MotionEvent.ACTION_MOVE) {
           updateRotation(rotation);
        }

        if (evt.getAction() == MotionEvent.ACTION_UP) {
            //
        }

        return true;
    }
};

private void updateRotation(double rot) {
    float newRot = new Float(rot);

    Matrix matrix = new Matrix();
    matrix.postRotate(newRot - 50);

    Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    picture.setImageBitmap(redrawnBitmap);
}
于 2012-07-02T09:25:20.640 に答える