Drawable围绕其中心Android旋转

Sam*_*gal 26 android rotation drawable

我用以下代码得到了奇怪的结果:

iv = (ImageView) findViewById(R.id.iv);
        iv.setImageResource(R.drawable.spinner_white_76);

        Animation a = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, iv.getDrawable()
                        .getIntrinsicWidth() / 2, Animation.RELATIVE_TO_SELF,
                iv.getDrawable().getIntrinsicHeight() / 2);
        a.setRepeatCount(-1);
        a.setDuration(1000);

        iv.startAnimation(a);
Run Code Online (Sandbox Code Playgroud)

什么是正确的方式来指定轴点(可绘制的中心)?

Sam*_*gal 61

感觉很蠢!花了一些时间仔细阅读文档后才能使用它:

Animation a = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        a.setRepeatCount(-1);
        a.setDuration(1000);
Run Code Online (Sandbox Code Playgroud)

  • 添加"a.setInterpolator(new LinearInterpolator());" 使旋转更平滑. (16认同)
  • @Md Omar Faroque Anik:添加"a.setFillAfter(true);" 使转换持久化并且"a.setRepeatCount(0);" 不要重复. (2认同)

st4*_*r4m 6

请注意,如果您的对象具有非对称填充(例如,左边填充为5px,右边填充为0px),则此操作无效,因为填充被视为对象的一部分.这意味着计算中心将被抵消.

解决此问题的一种方法是,如果出于布局原因使用填充,则使用边距而不是填充.正如API所说的关于边距:"这个空间超出了这个观点的范围." (ViewGroup.MarginLayoutParams)

这意味着边距不会像填充那样旋转.