首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使按钮在屏幕上自动移动

使按钮在屏幕上自动移动
EN

Stack Overflow用户
提问于 2013-05-20 11:57:06
回答 4查看 4K关注 0票数 8

我正在开发一个Android应用程序,它的屏幕上大约有6个按钮(当按下这些按钮时,它们会播放相应的视频)。下面是该应用程序的样机:

我希望按钮自动(和随机)在屏幕上移动。他们应该独立地做这件事,这意味着他们可以在其他按钮的前面(或后面) --他们不需要互相碰撞或类似的东西。理想情况下,如果按钮可以从画布上稍微移动一下就好了(如上图所示,按钮位于操作栏后面),但这不是必需的。

我该如何让按钮像这样移动呢?

EN

回答 4

Stack Overflow用户

发布于 2013-05-20 12:29:17

最近,我一直在玩动画,并尝试类似的东西。这是类。它基本上是围绕着父视图(你可以修改它的数学模型,使之更进一步)

代码语言:javascript
运行
复制
package com.example.animationtests.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class BouncingImageView extends ImageView {

    private View mParent;

    public BouncingImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BouncingImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BouncingImageView(Context context) {
        super(context);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mParent = (View) getParent();
        getHandler().post(mRunnable);
    }

    @Override
    protected void onDetachedFromWindow() {
        getHandler().removeCallbacks(mRunnable);
        super.onDetachedFromWindow();
    }

    private final Runnable mRunnable = new Runnable() {
        private static final int DIRECTION_POSITIVE = 1;
        private static final int DIRECTION_NEGATIVE = -1;
        private static final int ANIMATION_STEPS = 1;
        private int mHorizontalDirection = DIRECTION_POSITIVE;
        private int mVerticalDirection = DIRECTION_NEGATIVE;

        public boolean mStarted = false;

        @Override
        public void run() {
            if (mParent == null) {
                return;
            }

            final float width = getMeasuredWidth();
            final float height = getMeasuredHeight();
            final float parentWidth = mParent.getMeasuredWidth();
            final float parentHeight = mParent.getMeasuredHeight();
            float x = getX();
            float y = getY();

            if (!mStarted) {
                /***
                 * Randomize initial position
                 */
                x = (float) Math.random() * (parentWidth - width);
                y = (float) Math.random() * (parentHeight - height);
                mHorizontalDirection = ((int) x % 2 == 0) ? DIRECTION_NEGATIVE : DIRECTION_POSITIVE;
                mVerticalDirection = ((int) y % 2 == 0) ? DIRECTION_NEGATIVE : DIRECTION_POSITIVE;
                mStarted = true;
            } else {
                if (mHorizontalDirection == DIRECTION_NEGATIVE) {
                    x -= ANIMATION_STEPS;
                } else {
                    x += ANIMATION_STEPS;
                }

                if (mVerticalDirection == DIRECTION_NEGATIVE) {
                    y -= ANIMATION_STEPS;
                } else {
                    y += ANIMATION_STEPS;
                }

                if (x - (width / 3) < 0) {
                    mHorizontalDirection = DIRECTION_POSITIVE;
                } else if (x + (width / 3) > (parentWidth - width)) {
                    mHorizontalDirection = DIRECTION_NEGATIVE;
                }

                if (y - (height / 3) < 0) {
                    mVerticalDirection = DIRECTION_POSITIVE;
                } else if (y + (width / 3) > (parentHeight - height)) {
                    mVerticalDirection = DIRECTION_NEGATIVE;
                }
            }

            setX(x);
            setY(y);

            getHandler().post(this);
        }
    };
}

用法:

代码语言:javascript
运行
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".BouncingCircles" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true" >

        <com.example.animationtests.view.BouncingImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/light_dino" />

        <com.example.animationtests.view.BouncingImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/light_dino" />

        <com.example.animationtests.view.BouncingImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/light_dino" />

        <com.example.animationtests.view.BouncingImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@drawable/light_dino" />
    </RelativeLayout>

</RelativeLayout>
票数 6
EN

Stack Overflow用户

发布于 2013-05-20 12:14:35

我相信你需要使用ViewPropertyAnimator。有关如何进一步操作的详细信息,请参阅this。你应该提供给视图的路径每次都应该是随机的。清楚地检查一下this中你需要注意的东西。

从api指南中摘录

例如,如果您为button to move across the screen设置了动画,该按钮将正确绘制,但您可以单击该按钮的实际位置不会更改,因此您必须实现自己的逻辑来处理此问题。

票数 5
EN

Stack Overflow用户

发布于 2013-05-20 12:18:01

您可以在RelativeLayout中添加所有按钮,并更改边距以定位按钮移动。

使用以下代码初始化位置:

代码语言:javascript
运行
复制
    Button button = new Button();
    params = new RelativeLayout.LayoutParams(btnHeight,
                            btnWidth);
    params.leftMargin = leftMargin;
    params.topMargin = topMargin;
    rootview.addView(button, params);

要移动按钮,可以使用TranslateAnimation

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16642369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档