2011-11-15 42 views
0

在下面的例子中,有一个带有输入字段和按钮的布局。用户将输入一个介于0到10之间的数字,并且鳍状肢将从屏幕10开始动画到输入的数字。你可以循环根据用户输入的鳍状肢动画吗?

如果我在onClick上注释掉(int i = 10; .....; i - ){,则每次单击该按钮时屏幕都会滑动。如果我将屏幕中的for循环放入屏幕中,不会滑动每个屏幕。循环速度过快,动画难以赶上。

是否有可能把它因此,如果您例如输入2脚蹼会去:

翻转10

滑盖动画出现

挡板9

滑盖动画出现

翻转器8 .....

主要活动:

package com.jk.flippertest; 

import com.jk.flippertest.R; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ViewFlipper; 

public class FlipperTestActivity extends Activity { 
/** Called when the activity is first created. */ 
private Animation slideRightIn; 
private Animation slideRightOut; 
private ViewFlipper viewFlipper; 
TextView loopNumber; 
int loopValue; 
Button goBtn; 
ViewFlipper fSpace; 
LayoutInflater inflater; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    loopNumber = (TextView)findViewById(R.id.editText1); 
    goBtn = (Button)findViewById(R.id.button1);  

    fSpace = (ViewFlipper)findViewById(R.id.viewFlipper1); 
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    for (int i=0;i<11;i++){ 
     TextView tvi = new TextView(this); 
     tvi.setText("Flipper "+i); 
     tvi.setTextSize(50); 
     tvi.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     LinearLayout lli = new LinearLayout(this); 
     lli = new LinearLayout(this); 
     lli.setOrientation(LinearLayout.VERTICAL); 
     lli.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     lli.addView(tvi); 

     fSpace.addView(lli); 
     } 

     viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1); 
     slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in); 
     slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out); 

     goBtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       Toast.makeText(getBaseContext(), "Value is "+loopNumber.getText(), Toast.LENGTH_LONG).show(); 
       for(int i=10;i>=Integer.parseInt(loopNumber.getText().toString());i--){ 
       viewFlipper.setInAnimation(slideRightIn); 
       viewFlipper.setOutAnimation(slideRightOut);      
       viewFlipper.showPrevious(); 
       } 
      } 
     }); 

} 

}

主要布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:hint="Enter numb between 0 to 10."> 
     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Go" /> 

    <ViewFlipper 
     android:id="@+id/viewFlipper1" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" > 
    </ViewFlipper> 

</LinearLayout> 

slide_right_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="800"/> 
</set> 

slide_right_out.xml

<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="800"/> 
</set> 

+0

你可以发布slide_right_out.xml的代码吗? – joshhendo

回答

0

为什么你玩循环,只需使用计时器,并放置一段时间(1秒..),并在那只是改变你的意见(幻灯片)。

编辑:

CountDownTimer

Updating the UI from a Timer

+0

谢谢dmon,但我不确定计时器是如何工作的。我会尝试找到一些定时器和翻转视图的例子。如果有人可以附上一些示例代码,让我开始,将不胜感激。 – John

+0

看看我编辑的答案,你可以找到如何使用计时器的链接。 – user370305

0

感谢您的链接,但我看不出如何使用定时器。我在处理程序中找到了一些例子并使用它。下面的代码似乎工作,但不知道如果我得到它100%正确。

  @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       loopToNumber(); 
      } 
     }); 
} 
protected void loopToNumber() { 
    // TODO Auto-generated method stub 
    new Thread() { 
     public void run() { 
      for(int i=10;i>=Integer.parseInt(loopNumber.getText().toString());i--){ 
      try { 
       Thread.sleep(1600); 
       } catch (InterruptedException e) { 
       } 
      delayHandler.sendEmptyMessage(0); 
      } 
     } 
    }.start(); 
} 
private Handler delayHandler = new Handler() { 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 
     viewFlipper.setInAnimation(slideRightIn); 
     viewFlipper.setOutAnimation(slideRightOut);      
     viewFlipper.showPrevious(); 
    } 
}; 

相关问题