1

我正在实现代码以通过触摸侦听器旋转图像。当用户触摸和旋转图像时,它正在旋转,同时它正在“缩小”。我需要没有缩小的图像旋转。这是我实现的代码。

public class SeekBarCustomized extends Activity implements OnTouchListener{

    //VerticalSeekBar mVerSeekbar;
    ImageView mIvRotateImage;
    private float y=0;
    int r =0;

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

        mIvRotateImage = (ImageView) findViewById(R.id.ivImgView);
        mIvRotateImage.setOnTouchListener(this);

}
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        //double r=Math.atan2(event.getX()-mIvRotateImage.getWidth()/2, mIvRotateImage.getHeight()/2-event.getY());
        //double r=Math.atan2(event.getX(), event.getY());
        int rotation=(int)Math.toDegrees(r);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:

                r = r + 2;

                rotate(v, event);
                break;
            case MotionEvent.ACTION_UP:
                break;
        }//switch       

        return true;
    }

private void rotate(View v, MotionEvent event) {
        LayoutParams mParams = (LayoutParams) mIvRotateImage.getLayoutParams();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.increcircle);
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.preRotate(r);
        Bitmap rotaBitmap = Bitmap.createBitmap(bitmap, 0, 0, 200, 200, matrix,
                true);
        BitmapDrawable bdr = new BitmapDrawable(rotaBitmap);

        int x = mParams.leftMargin;
        int y = mParams.topMargin;
        mParams.leftMargin = 50;
        mParams.topMargin = 50;
        mIvRotateImage.setImageDrawable(bdr);
        mIvRotateImage.setLayoutParams(mParams);
    }
}

我的 XML 文件是

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

<ImageView android:id="@+id/ivImgView"
    android:layout_width="200px" android:layout_height="200px"
    android:layout_marginTop="50px" android:layout_marginLeft="50px"
     android:src="@drawable/increcircle"/>
</RelativeLayout>
4

1 回答 1

1
I used this code snippet.

//android:scaleType="center" 
in <ImageView /> tag at xml file/
By using this Image is rotating but not scaling.


<ImageView android:id="@+id/ivImgView"
    android:layout_width="200px" android:layout_height="200px"
    android:layout_marginTop="50px" android:layout_marginLeft="50px"
     android:src="@drawable/increcircle" android:scaleType="center"/>
于 2012-04-24T08:49:21.157 回答