android - Making buttons automatically move around the screen -
i'm working on android app there around 6 buttons on screen (which play corresponding video when pressed). here's mockup of application looks like:

i want buttons automatically (and randomly) move around screen. should independently, meaning can go in front of (or behind) other buttons -- don't need bump off of each other or that. ideally, if buttons move off canvas (as seen in image above button button going behind action bar), not essential.
how go making buttons move around this?
recently, i've been playing animations , attempted similar. here class. bounces around parent view (which can alter math go further)
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); } }; } usage:
<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>
Comments
Post a Comment