如何在android中单击按钮时旋转位图?
我在旋转位图时遇到问题。我的要求如下:如何在android中单击按钮时旋转位图?,android,image,Android,Image,我在旋转位图时遇到问题。我的要求如下: 通过摄像头捕获图像,并在ImageView中显示捕获图像 单击/触摸图像时,在imageview上显示两个按钮。一个按钮用于顺时针(45度)方向旋转图像,另一个按钮用于逆时针方向旋转图像 我在这个过程中使用了下面的代码,不幸的是它只工作了一次,当我第一次点击按钮时,ti会将对应的图像旋转45度,之后按钮点击没有变化。 //onclick 公共void onClick(视图v){ 这是因为每次单击按钮时,您仍在引用原始图像,而不是更改的图像。如果要继续旋转
这是因为每次单击按钮时,您仍在引用原始图像,而不是更改的图像。如果要继续旋转或修改更改的图像,则必须引用它,而不是原始图像
只需创建一个全局变量并将更改后的位图存储在其中。然后,当按下按钮时,检查它是否为null。如果为null,则使用原始值;否则使用更改后的值我们需要更多代码。您是否使用onClickListener?matrix.portRotate(deg)45度也可以。请看这个答案,@BradHekman:我很累,但它只能工作一次。当我开火时,第一次单击它旋转,然后第二次单击表示它不会旋转。@gtumca MAC:关于你的信息,我使用了你提到的链接中的代码。请看第二个链接,我已经发布了完整的代码,它对我来说工作正常
switch (v.getId()) {
case R.id.right_image_rotate:
try {
System.gc();
Runtime.getRuntime().gc();
unbindDrawables(findViewById(R.id.image_viewer));
imageRotate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.left_image_rotate:
try {
System.gc();
Runtime.getRuntime().gc();
unbindDrawables(findViewById(R.id.image_viewer));
imageRotate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.image_viewer:
rotateLayout.setVisibility(View.VISIBLE);
imageLeft_rotate.setVisibility(View.VISIBLE);
imageRight_rotate.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
Bitmap bitmapOrg = BitmapFactory.decodeFile(saved_image_file
.getAbsolutePath());
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(45);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
srcBitmap.setScaleType(ScaleType.CENTER);
srcBitmap.setImageBitmap(resizedBitmap);