汽车动画旋转轮子
我正在开发一个显示不同汽车的应用程序,用户需要选择一辆。当您按下下一步时,汽车移出屏幕,新的汽车进入。
我的资源是:
- 汽车的车身
- 汽车的车轮
(我需要将它们分开,因为当汽车移动时我需要旋转车轮)
我想避免AbsoluteLayout
所以我的 CarView
扩展了`RelativeLayout。代码如下:
public class CarView extends RelativeLayout {
private ImageView mBody;
private ImageView mLeftWheel;
private ImageView mRightWheel;
private float mScale;
public CarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CarView(Context context) {
super(context);
}
public void initView(Car car) {
mScale = getContext().getResources().getDisplayMetrics().density;
Context ctx = getContext();
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mBody = new ImageView(ctx);
mBody.setLayoutParams(params);
mBody.setImageResource(R.drawable.car_body);
addView(mBody);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mLeftWheel = new ImageView(ctx);
mLeftWheel.setLayoutParams(params);
mLeftWheel.setImageResource(R.drawable.car_wheel);
mLeftWheel.setPadding(0, dpToPx(79), dpToPx(188), 0);
addView(mLeftWheel);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mRightWheel = new ImageView(ctx);
mRightWheel.setLayoutParams(params);
mRightWheel.setImageResource(R.drawable.car_wheel);
mRightWheel.setPadding(dpToPx(203), dpToPx(75), 0, 0);
addView(mRightWheel);
}
public void startMoving() {
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(900L);
rotateAnimation.setRepeatCount(Animation.INFINITE);
mLeftWheel.startAnimation(rotateAnimation);
mRightWheel.startAnimation(rotateAnimation);
}
private int dpToPx(float dp) {
return (int) (dp * mScale + 0.5f);
}
}
基本上我所做的就是放置车身,然后使用填充将车轮放置在应有的位置。
阅读此 Android 开发者的帖子后,我注意到 RotateAnimation
旋转整个视图,包括使轮子做一些奇怪的运动的填充。
我应该如何解决这个问题? 你能想出一种更好的方法来放置轮子ImageView
而不是使用填充吗?
我遇到的另一个问题是,在某个时刻我希望轮子停止移动,但是方法 cancel() in Animation
是从以下版本开始:API 级别 8,我需要它在 1.5 上工作。我该如何停止旋转?
I am working in an app that shows different cars and the user needs to choose one. When you press on next the car moves out of the screen and the new one comes in.
My resources are:
- Car's body
- Car's wheel
(I need them to be separated because when the car moves I need to rotate the wheels)
I wanted to avoid AbsoluteLayout
so my CarView
extends `RelativeLayout. Here's the code:
public class CarView extends RelativeLayout {
private ImageView mBody;
private ImageView mLeftWheel;
private ImageView mRightWheel;
private float mScale;
public CarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CarView(Context context) {
super(context);
}
public void initView(Car car) {
mScale = getContext().getResources().getDisplayMetrics().density;
Context ctx = getContext();
RelativeLayout.LayoutParams params;
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mBody = new ImageView(ctx);
mBody.setLayoutParams(params);
mBody.setImageResource(R.drawable.car_body);
addView(mBody);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mLeftWheel = new ImageView(ctx);
mLeftWheel.setLayoutParams(params);
mLeftWheel.setImageResource(R.drawable.car_wheel);
mLeftWheel.setPadding(0, dpToPx(79), dpToPx(188), 0);
addView(mLeftWheel);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mRightWheel = new ImageView(ctx);
mRightWheel.setLayoutParams(params);
mRightWheel.setImageResource(R.drawable.car_wheel);
mRightWheel.setPadding(dpToPx(203), dpToPx(75), 0, 0);
addView(mRightWheel);
}
public void startMoving() {
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(900L);
rotateAnimation.setRepeatCount(Animation.INFINITE);
mLeftWheel.startAnimation(rotateAnimation);
mRightWheel.startAnimation(rotateAnimation);
}
private int dpToPx(float dp) {
return (int) (dp * mScale + 0.5f);
}
}
Basically what I am doing is placing the body of the car and then using padding to place the wheels where they should be.
After reading this android developer's thread I notice that RotateAnimation
rotates the whole view including the padding making the wheels to do some strange movement.
How should I fix this?
Can you think of a better way to place the wheels ImageView
instead of using padding?
Another issue I have is that in a certain point I want the wheels to stop moving, but the method cancel() in Animation
is Since: API Level 8 and I need this to work on 1.5. How should I stop the rotation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为“正确的答案”是创建您自己的布局管理器。至少,这就是我们被告知的。我尝试过一次但无法让它工作。
不太正确的答案可能是边距而不是填充——我还没有尝试过使用边距对
View
进行动画处理,所以我不知道边距是否会旋转。填充应该旋转,因为它被认为是小部件的“内部”。甚至不太正确的答案是使用透明的
View
作为垫片,而不是边距或填充。将ImageView
相对于垫片对齐,没有边距或填充,并将垫片的大小设置为将ImageView
放置在您想要的位置。如果您知道怎么做的话,答案是完全转储小部件,然后使用 Canvas 和 2D 图形 API 来绘制它们。例如,《愤怒的小鸟》可能不使用
ImageViews
。The "right answer", I think, is to create your own layout manager. Leastways, that's what we've been told. I tried it once and couldn't get it to work.
The not-quite-so-right answer may be margins rather than padding -- I haven't tried animating a
View
with margins and so I do not know if margins rotate. Padding should rotate, since that is considered "inside" the widget.The even-less-right answer is to use a transparent
View
as a shim, rather than margins or padding. Align yourImageView
relative to the shim with no margins or padding, with the shim's size set to position yourImageView
where you want it.The well-if-you-know-how answer is to dump the widgets entirely and just draw it all using the
Canvas
and 2D graphics APIs. Angry Birds, for example, presumably does not useImageViews
.