如何在其中心点旋转 android 图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3137490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:52:14  来源:igfitidea点击:

How to spin an android icon on its center point?

androidanimationrotation

提问by mobibob

I have written the following to spin my icon on the center of the screen and instead it rotates around the upper-left corner (i.e., origin x=0, y=0 of the ImageView). It should be simple to set some attributes of the ImageView or the RotateAnimation, but I can't figure it out.

我编写了以下内容来在屏幕中央旋转我的图标,而是围绕左上角旋转(即 ImageView 的原点 x=0,y=0)。设置ImageView或者RotateAnimation的一些属性应该很简单,但是我想不通。

public class IconPromoActivity extends Activity {
    private static final float ROTATE_FROM = 0.0f;
    private static final float ROTATE_TO = -10.0f * 360.0f;// 3.141592654f * 32.0f;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView favicon = (ImageView) findViewById(R.id.favicon);

        RotateAnimation r; // = new RotateAnimation(ROTATE_FROM, ROTATE_TO);
        r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, 0, 0, 40, 0);
        r.setDuration((long) 2*1500);
        r.setRepeatCount(0);
        favicon.startAnimation(r);
    }
}

回答by Rich Schuler

Try: r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

尝试: r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

回答by Rodrigo Ferreira

This works for me:

这对我有用:

img = (ImageView)findViewById(R.id.ImageView01);
RotateAnimation rotateAnimation = new RotateAnimation(30, 90,
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

回答by Omar

Here is the full example

这是完整的例子

public class MainActivity extends AppCompatActivity implements MainActivityMvpModel {
    ImageView imageViewThumb;

    private static final float ROTATE_FROM = 30.0f;
    private static final float ROTATE_TO = 360.0f;
    RotateAnimation r; // = new RotateAnimation(ROTATE_FROM, ROTATE_TO);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonTest= (Button) findViewById(R.id.button_test);

        imageViewThumb= (ImageView) findViewById(R.id.icon_thumb);
        imageViewThumb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                r.setDuration((long) 2*500);
                r.setRepeatCount(0);
                imageViewThumb.startAnimation(r);

                imageViewThumb.setColorFilter(R.color.colorThumbPressed);
            }
        });
    }
}