【问题标题】:Android using animation to make an object fall towards the ground using gravityAndroid使用动画使物体使用重力落向地面
【发布时间】:2016-01-25 04:49:30
【问题描述】:

我是 Android 开发的新手,我正在尝试在屏幕顶部创建一个图像并使其落到底部。我一直在环顾四周,无法得到正确的答案或指向正确方向的指针。我发现的许多示例都已过时,并且许多方法不再起作用。

我的可绘制文件夹中有一个图像,我想在 10 秒后创建它并让它像重力一样落向地面。

我看过很多关于 ObjectAnimator、画布、速度、动画的知识。我不知道从哪里开始。

【问题讨论】:

    标签: java android animation gravity


    【解决方案1】:

    请务必查看ViewPropertyAnimator 类及其方法。 对于加速和弹跳等,有所谓的插值器,使动画更逼真/逼真。我能想到的最简单的方法是这样的:

    假设您在这样的 xml 文件中有一个 RelativeLayout 作为父级,一个 ImageView 作为子级:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/your_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/ic_launcher" />
    </RelativeLayout>
    

    然后在您的 Java 代码中像这样启动动画:

    ImageView imageView = (ImageView) findViewById(R.id.your_image);
    
    float bottomOfScreen = getResources().getDisplayMetrics()
        .heightPixels - (imageView.getHeight() * 4);
            //bottomOfScreen is where you want to animate to
    
    imageView.animate()
        .translationY(bottomOfScreen)
        .setInterpolator(new AccelerateInterpolator())
        .setInterpolator(new BounceInterpolator())
        .setDuration(2000);
    

    这应该让你开始。

    【讨论】:

    • 您是否有一些资源可供我参考? :) 非常感谢你
    • 什么意思?教程?
    • 哦,对不起,别介意你说什么是“ViewPropertyAnimator”。再次感谢!
    • 谢谢,顺便说一句,您知道如何进行碰撞吗?当我的图像掉下来时,我希望它反弹一个线性布局。我见过一些使用 rect.intersect 但是它似乎对我不起作用
    • 今天晚些时候会做,再次感谢这是您作为答案输入的非常好的信息!
    【解决方案2】:

    如果要模拟真实重力,难度很大,需要安装其他外部库。你还需要做很多数学才能让重力发挥作用。

    真正的重力:物体会加速和反弹等等

    但如果你只是想让某物以恒定的速度从上到下落下(没有加速度),那就更容易了。

    您只需在项目的anim 文件夹中创建一个新的xml 文件并将其加载到您的代码中。有很多教程可以做到这一点:http://developer.android.com/guide/topics/graphics/view-animation.html

    真的很简单! (XML 的东西总是很容易,不是吗?)

    【讨论】:

    • 我看到了一些优势,但是随着级别的增加,我希望能够提高速度并使其随机出现在屏幕顶部的 x 上
    • 还有模拟“弹跳效果”的方法吗?
    【解决方案3】:

    摘录:

    Handler h=new Handler();
    ImageView img;
    
    Runnable r= new Runnable()
            {
            @Override public void run() {img.invalidate();}
            };
    
    @Override 
    protected void onCreate(Bundle b)
    {
        super.onCreate(b);
        p.setColor(Color.RED);
        p.setStyle(Paint.Style.FILL_AND_STROKE);
        p.setDither(false);
        p.setStrokeWidth(3);
        p.setAntiAlias(true);       
        p.setColor(Color.parseColor("#CD5C5C"));
        p.setStrokeWidth(15);
        p.setStrokeJoin(Paint.Join.ROUND);    
        p.setStrokeCap(Paint.Cap.ROUND);
    
        img=new ImageView(this)
            {   
                @Override protected void onDraw(Canvas cnv)
                {
                    x+=i;
                    y+=j;
                    if(x>300)i=-1;
                    if(y>300)j=-1;
                    if(x<0)i=1;
                    if(y<0)j=1;
    
                    cnv.drawPoint(x,y,p);
                    h.postDelayed(r,fps);   
    

    关键是我们需要从外部句柄调用用户界面主线程的更新,以固定 fps 处理正在运行的线程。

    查看完整代码: Thread Runnable Handle to Graphic Animation Bouncing Ball like Pixels

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多