gpt4 book ai didi

android - 如何通过触摸监听器旋转图像?

转载 作者:行者123 更新时间:2023-11-30 04:14:24 25 4
gpt4 key购买 nike

我正在实现通过触摸监听器旋转图像的代码。当用户触摸并旋转图像时,它会旋转,同时会“缩小”。我需要不缩小的图像旋转。这是我实现的代码。

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>

最佳答案

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"/>

关于android - 如何通过触摸监听器旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com