使用按钮在Android中旋转图像 [英] Rotating image in Android with a button

查看:59
本文介绍了使用按钮在Android中旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图上有一个带有几个按钮的图像.其中一个按钮将图像向下移动.它通过向顶部边距添加 1 像素来向下移动图像.代码如下:

I have a image with a few buttons on the view. One of the buttons moves the image down. It moves the image down by adding 1 px to the top margin. Here is the code:

@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();
            }
        });
    }

现在,我希望能够旋转图像.就像我现在拥有的代码一样,我想要一个按钮,按下按钮时图像会旋转.但是我该怎么做呢?

Now, i want to be able to rotate the image. Just like the code i have now, I want a button, when the button is pressed the image will rotate. But how can i do this?

推荐答案

 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. ImageView 中的图像旋转(Android)
  2. 当点击按钮时,在 android 中顺时针旋转图像

试试这个,希望对你有帮助.

Try this ,I hope it will helps you .

这篇关于使用按钮在Android中旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆