简体   繁体   English

使用按钮旋转图像 (Android)

[英]Rotating A image with a button (Android)

I have a image on my view I need a button, that when the button is clicked the image will start rotating on its center point by 1px at a time.我的视图中有一个图像,我需要一个按钮,当单击该按钮时,图像将开始围绕其中心点旋转 1 像素。

Right now I have another button that moves the image down, by adding 1 px to the top margin.现在我有另一个按钮可以将图像向下移动,方法是将 1 px 添加到顶部边距。 Here is that code:这是代码:

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

I just need to basically do the same thing, but rather then moving the image down, i need to rotate it.我只需要基本上做同样的事情,而不是向下移动图像,我需要旋转它。

put this in your code: image.setRotation(image.getRotation() + 1);把它放在你的代码中: image.setRotation(image.getRotation() + 1);

finally your code:最后你的代码:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ViewGroup.MarginLayoutParams) image.getLayoutParams()).topMargin += 1;
                image.setRotation(image.getRotation() + 1);
                image.requestLayout();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM