汽车动画旋转的车轮 [英] Car animation rotating wheels

查看:200
本文介绍了汽车动画旋转的车轮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个应用程序,显示不同的汽车工作,用户需要选择一个。当美元下一个P $ PSS车子移出屏幕和新的用武之地。

我的资源是:


  • 该车的车身

  • 汽车的轮

(我需要它们,因为当车子移动我需要旋转车轮分开)

我想避免 AbsoluteLayout 所以我的 Carview会扩展`RelativeLayout的。这里的code:

 公共类Carview会扩展RelativeLayout的{    私人ImageView的mBody;
    私人ImageView的mLeftWheel;
    私人ImageView的mRightWheel;
    私人浮动mScale;    公共Carview会(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }    公共Carview会(上下文的背景下){
        超级(上下文);
    }    公共无效initView(车车){
        mScale =的getContext()getResources()getDisplayMetrics()密度。;
        上下文CTX =的getContext();
        RelativeLayout.LayoutParams PARAMS;        PARAMS =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mBody =新ImageView的(CTX);
        mBody.setLayoutParams(PARAMS);
        mBody.setImageResource(R.drawable.car_body);
        addView(mBody);        PARAMS =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mLeftWheel =新ImageView的(CTX);
        mLeftWheel.setLayoutParams(PARAMS);
        mLeftWheel.setImageResource(R.drawable.car_wheel);
        mLeftWheel.setPadding(0,dpToPx(79),dpToPx(188),0);
        addView(mLeftWheel);
        PARAMS =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mRightWheel =新ImageView的(CTX);
        mRightWheel.setLayoutParams(PARAMS);
        mRightWheel.setImageResource(R.drawable.car_wheel);
        mRightWheel.setPadding(dpToPx(203),dpToPx(75),0,0);
        addView(mRightWheel);
    }    公共无效startMoving(){        RotateAnimation rotateAnimation =新RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5F,Animation.RELATIVE_TO_SELF,0.5F);
        rotateAnimation.setInterpolator(新LinearInterpolator());
        rotateAnimation.setDuration(900L);
        rotateAnimation.setRepeatCount(Animation.INFINITE);        mLeftWheel.startAnimation(rotateAnimation);
        mRightWheel.startAnimation(rotateAnimation);
    }    私人诠释dpToPx(浮点DP){
        回报(INT)(DP * mScale + 0.5F);
    }
}

基本上,我做的是放置车身上,然后用填充放置轮子,他们应该的。

这的Andr​​oid开发者的线程我通知后该 RotateAnimation 旋转包括使车轮做一些奇怪的动作填充整个视图。

我应该如何解决这个问题?
你能想出更好的办法来代替的ImageView 而不是使用填充轮子?

另一个问题我是在某一点我想轮子停止移动,但该方法的取消()动画是自:API等级8,我需要这1.5工作。我应该如何停止转动?


解决方案

  

你能想出更好的办法来放置,而不是使用填充车轮ImageView的?


正确答案,我认为,就是要创建自己的布局管理器。至少 - 这就是我们一直在说。我试了一次,并不能得到它的工作。

在未十分那么正确的答案可能是利润而不是填充 - 我没有尝试过一个动画查看与利润,所以我不知道是否利润率旋转。填充应该旋转,因为这被认为是内部的小工具。

在偶数不太正确的答案是使用透明查看作为垫片,而不是利润的的填充。你的的ImageView 相对对齐到没有利润或填充垫片,用垫片的大小设置为定位您的ImageView ,你想要它。

该井-IF-你知道是如何的答案是完全转储部件和刚刚绘制全部使用画布和2D图形API。愤怒的小鸟,例如,presumably不使用 ImageViews

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?

解决方案

Can you think of a better way to place the wheels ImageView instead of using padding?

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 your ImageView relative to the shim with no margins or padding, with the shim's size set to position your ImageView 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 use ImageViews.

这篇关于汽车动画旋转的车轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆