Android 绕其中心旋转的 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);
Whats the right way to指定轴点(可绘制的中心)?
I am getting strange results with the following code:
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);
Whats the right way to specify the axis point (center of the drawable)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感觉自己很蠢!花了一些时间仔细阅读文档后让它开始工作:
Feel stupid! Got it to work after spending some time closely reading the documentation:
请注意,如果您的对象具有不对称内边距(例如,左内边距为 5 像素,右内边距为 0 像素),则此方法将不起作用,因为内边距被视为对象的一部分。这意味着计算的中心将发生偏移。
解决此问题的一种方法是,如果出于布局原因而使用填充,则使用边距而不是填充。正如 API 关于边距的说法:“该空间超出了该视图的边界。” (ViewGroup.MarginLayoutParams)
这意味着边距不会像填充那样旋转。
Note that this will not work if your object has asymmetric padding (for example a left padding of 5px and a right padding of 0px), because padding is considered part of the object. Which means the computed center will be offset.
One solution to this is to use margins instead of padding if you are using padding for layout reasons. As the API says concerning margins: "This space is outside this view's bounds." (ViewGroup.MarginLayoutParams)
This means that margins will not rotate as padding does.
由于这个问题的标题是Drawable围绕其中心旋转(我正在精确地搜索它,没有找到它并且必须自己实现它)想发布我的解决方案/答案。
我最终编写了自定义可绘制对象,它可以包装任何可绘制对象并允许其旋转。这是代码:
As title for this questions is Drawable Rotating around its center (and I was searching exactly for it, had not found it and had to implement it myself) would like to post my solution/answer.
I ended up writing custom drawable which can wrap any drawable and allow its rotation. Here is the code: