我在视图上有一些带有几个按钮的图像。其中一个按钮可将图像向下移动。它通过向顶部边距添加1 px来向下移动图像。这是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
final ImageView image = (ImageView) findViewById(R.id.image1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ViewGroup.MarginLayoutParams) image.getLayoutParams()).topMargin += 1;
image.requestLayout();
}
});
}
现在,我希望能够旋转图像。就像我现在的代码一样,我想要一个按钮,当按下按钮时,图像将旋转。但我怎么能这样做?
答案 0 :(得分:1)
Bitmap source; //Declare Global
float angle=0; //Declare Global
Button button = (Button) findViewById(R.id.button1);
final ImageView image = (ImageView) findViewById(R.id.image1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
angle+=70;
Bitmap rotatedImage=rotateImage(your_image_source,angle);
img.setImageBitmap(rotatedImage);
}
});
public static Bitmap rotateImage(Bitmap sourceImage, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), matrix, true);
}
您可以查看
答案 1 :(得分:0)
imageView.setScaleType(ImageView.ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.setRotate(degree); //you can also translate, scale
imageView.setImageViewMatrix(matrix);