2013-05-20 11 views
8

私は、画面に約6つのボタンがある(これは対応するビデオが押されたときに再生される)Androidアプリで作業しています。ここでは、アプリケーションがどのように見えるかのモックアップだ:ボタンを自動的に画面の周りに動かす

Android App

私は、画面の周りのボタンに自動的に(かつランダムに)移動したいです。彼らはこれを独立して行う必要があります。つまり、他のボタンの前(または後ろ)に行くことができます。お互いにぶつかる必要はありません。理想的には、ボタンがキャンバスから実際にわずかに外れることができれば(ボタンのボタンがアクションバーの後ろにある上の画像に見られるように)、これは必須ではありませんが、これは必須ではありません。

このようにボタンを移動させるにはどうすればいいですか?

+0

それを取得しましたか?どのようにしましたか? –

答えて

4

私はあなたがViewPropertyAnimatorを使用する必要があると信じています。詳細については、thisを参照してください。ビューに与えるべきパスは毎回ランダムでなければなりません。気をつけなければならないことについては、確かにthisにチェックしてください。

APIガイドからの抜粋:例えば

、あなたがbutton to move across the screenをアニメーション場合、ボタンが正しく描画していますが、実装する必要がありますので、あなたがボタンをクリックすることができ、実際の場所は、変更されません。これを処理する独自のロジック。

+0

例を挙げますか?本当にありがとう。私は好きだった –

1

RelativeLayoutにすべてのボタンを追加し、余白を変更してボタンの移動位置を変更できます。私はアニメーションでプレーしてきた、あなたは最近TranslateAnimation

5

を使用することができ、

Button button = new Button(); 
    params = new RelativeLayout.LayoutParams(btnHeight, 
          btnWidth); 
    params.leftMargin = leftMargin; 
    params.topMargin = topMargin; 
    rootview.addView(button, params); 

がボタンを移動するには、同様の未遂:
は、以下のコードを使用して位置を初期化します。ここにクラスがあります。それは基本的に

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); 
     } 
    }; 
} 

使用上(あなたはさらに行くために数学を変えることができた)親ビューの周りにバウンス:

<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> 
0

あなたのボタンにこのアニメーション設定されたこの

ObjectAnimator mover = ObjectAnimator.ofFloat(aniView, 
       "translationX", -500f, 0f); 
      mover.setDuration(2000); 

をお試しください

関連する問題