2014-10-06 33 views
-1

我有一個簡單的球視圖,我想動畫(反彈)。我在我的活動有這樣的方法:在android中的簡單球動畫

public void beginAnimation() { 
    boolean boola = true; 
    while(boola) { 
     try { 
      Thread.sleep(10); 
      ball.setY(ball.getY() + 10); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     if (ball.getY() > 200) { 
      boola = false; 
     } 
    } 

} 

當我把它的onCreate(),而沒有顯示動畫了,看來我需要爲這個特殊的線程,但因爲線程不能修改UI元素, 我怎樣才能解決這個問題?謝謝!

+1

,一定不要調用'的Thread.sleep(10)'主線程。它會阻止用戶輸入。我很驚訝它不會拋出異常。你可以使用handler.postDelayed來稍後在主線程中運行代碼,但是Marcin的答案更好,所以我不會將其作爲答案發布。 – 2014-10-06 19:14:23

回答

1

您需要覆蓋View的onDraw(Canvas c)方法,並從那裏調用beginAnimation(),而不是從Activity調用。請檢查這個例子http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/

+0

太棒了!謝謝!簡短的問題,因爲這也是我不明白的: 如果我創建一個矩形,xPos = 50&yPos = 50,在獲得活動中的頭寸後,我會得到這些數字,還是我會得到0?我想知道如何將視圖中的這種移動方法與通過觸摸觸發的後續移動連接起來。 – 2014-10-06 19:17:55

0
<?xml version="1.0" encoding="utf-8"?> 
<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="com.example.shwetachauhan.animationstarter.MainActivity"> 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" 
    android:src="@mipmap/ball" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="87dp" /> 

+0

你能否解釋一下這個問題是如何解決的? – mnwsmit 2016-06-08 10:25:37

0
public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

private static final String TAG = "AnimationStarter"; 
Button bounceBtn; 
ImageView imagView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    initialization(); 
    setListener(); 
} 

private void initialization() { 
    bounceBtn = (Button) findViewById(R.id.button); 
    imagView = (ImageView) findViewById(R.id.imageView); 
} 

private void setListener() { 
    bounceBtn.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    imagView.clearAnimation(); 
    // imagView.setVisibility(View.VISIBLE); 
//    TranslateAnimation transAnim=new TranslateAnimation(0,0,0,-getDisplayHeight()/2); 

TranslateAnimation transAnim = new TranslateAnimation(0, 0, -getDisplayHeight()/2, 0); 
transAnim.setStartOffset(500); 
transAnim.setDuration(3000); 
transAnim.setFillAfter(true); 
transAnim.setInterpolator(new BounceInterpolator()); 
transAnim.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 
     Log.i(TAG, "Starting button dropdown animation"); 
    } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      Log.i(TAG, 
        "Ending button dropdown animation. Clearing animation and setting layout"); 
      imagView.clearAnimation(); 
      final int left = imagView.getLeft(); 
      final int top = imagView.getTop(); 
      final int right = imagView.getRight(); 
      final int bottom = imagView.getBottom(); 
      imagView.layout(left, top, right, bottom); 


     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    }); 
    imagView.startAnimation(transAnim); 
} 

private int getDisplayHeight() { 
    return this.getResources().getDisplayMetrics().heightPixels; 
} 

}