RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 813238
Accepted
keltkelt
keltkelt
Asked:2020-04-12 20:09:35 +0000 UTC2020-04-12 20:09:35 +0000 UTC 2020-04-12 20:09:35 +0000 UTC

制作几个硬币减少并飞到一个点的动画的最佳方法是什么?

  • 772

有一堆金子的图像,有一个动画打开的箱子。当您点击一堆时,它应该变成一把硬币并飞入胸部。动画这些硬币的最佳方法是什么?将每个加载为 ImageView 并分别设置动画?

回复评论:

  1. 我还是一个初学者,我只使用 ConstraintLayout,在它发生的课程之后。我应该将所有内容都更改为相对吗?
  2. 我不需要从 AppCompatActivity 继承你的类,只需执行必要的导入?
  3. R.dimen.paddings 中的paddings参数以红色突出显示。
  4. 我对这条线感到困惑。
    ((类型父 startView)startView.getParent).removeView(startView);

改成这样:

((ImageView)startView.getParent()).removeView(startView);

但现在 removeView() 是红色的。

  1. 在实现一个类时,我的主类需要声明抽象或实现一个抽象方法 AnimationEnd(int)
android
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Jarvis_J
    2020-04-12T21:31:12Z2020-04-12T21:31:12Z

    我将下面的课程用于类似的目的(在我的情况下,点飞)。

    用法:在需要时为每个堆创建一个实例并传递上下文RelativeLayout(是的,根必须是View activity. RelativeLayoutYou can FrameLayout)、移动开始的图像(你的堆,无论它在哪里)、胸部的图像(钱会飞到哪里)),将“飞”的金额(金额)。

    public class AnimateScore {
    private static final int ANIMATION_DURATION = 400;
    
    private View targetView;
    private int offset, number;
    private AnimationEnd animationEnd;
    private ImageView img;
    private RelativeLayout container;
    
    interface AnimationEnd {
        void animationEnd(int number);
    }
    
    AnimateScore(Context ctx, RelativeLayout container, View startView, View targetView, int number) {
        animationEnd = (AnimationEnd) ctx;
        this.number = number;
    
        int dimens = (int) ctx.getResources().getDimension(R.dimen.paddings);
        offset = (int) (-24 * ctx.getResources().getDisplayMetrics().density);
    
        this.container = container;
    
        this.targetView = targetView;
    
        img = new ImageView(ctx);
        int[] coords = {getRelativeLeft(startView), getRelativeTop(startView)};
        String str = "+" + String.valueOf(number);
        img.setImageResourse(R.drawable.my_img)
        img.setPadding(dimens, dimens, dimens, dimens);
        img.setX(coords[0] + dimens * 2);
        img.setY(coords[1]);
    
        ((тип parent startView)startView.getParent).removeView (startView);
    
        container.addView(img);
        img.postDelayed(new Runnable() {
            @Override
            public void run() {
                drawAnimation(img);
            }
        }, 10);
    }
    
    private void drawAnimation(View v1) {
        v1.clearAnimation();
        ScaleAnimation animation = new ScaleAnimation (1, 0,1,0);
        animation.setDuration(ANIMATION_DURATION + 100);
        animation.setFillAfter(true);
        v1.setAnimation(animation);
        v1.startAnimation(animation);
        v1.animate().translationX(getRelativeLeft(targetView))
                .translationY(getRelativeTop(targetView) + offset).setDuration(ANIMATION_DURATION)
                .setInterpolator(new DecelerateInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
    
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
                container.removeView(img);
                animationEnd.animationEnd(number);
            }
    
            @Override
            public void onAnimationCancel(Animator animation) {
    
            }
    
            @Override
            public void onAnimationRepeat(Animator animation) {
    
            }
        });
    
    }
    
    private int getRelativeLeft(View myView) {
        if (myView.getParent() == myView.getRootView())
            return myView.getLeft();
        else
            return myView.getLeft() + getRelativeLeft((View) myView.getParent());
    }
    
    private int getRelativeTop(View myView) {
        if (myView.getParent() == myView.getRootView())
            return myView.getTop();
        else
            return myView.getTop() + getRelativeTop((View) myView.getParent());
    }
    }
    

    offset- 为了正确操作(我不知道偏移量来自哪里,但确实如此)

    dimens*2- 占两个父母padding

    在接收器类中不要忘记implements AnimateScore.AnimationEnd

    如果某些东西不起作用或您有疑问,请稍微调整课程以适应答案 - 请写在评论中;)

    一次:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/globalCont"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
         <android.support.constraint.ConstraintLayout...>
    
    
    </android.support.constraint.ConstraintLayout>
    </RelativeLayout>
    

    二:不,只是在适当的时候调用实例:new AnimateScore (YourActivity.this, findViewById(R.id.globalcont),your_coin_pile_img,your_chest_img, 100)

    三:这些是我在dimen. 您可以删除它或将光标移到那里,按 alt+enter 并输入一个值(通常为 5-10-15dp)。或者dimen手动注册。

    四:“type parent”——你的“黄金堆”所在的容器类型(LinearLayout、FrameLayout、ConstrainLayout等)

    五:实现:)(当光标为红色时,alt+enter)。可选部分,可与interface. 需要它来跟踪动画何时“到达”,例如,然后才将硬币添加到胸部。

    • 4

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5