在下面的示例中,有一个带有输入字段和按钮的布局。用户将输入0到10之间的数字,并且鳍状肢将从屏幕10动画到输入的数字。
如果我在onClick上注释 for(int i = 10; .....; i - ){,每次单击按钮时屏幕都会滑动。如果我在屏幕中放入for循环直接进入屏幕编号而不滑动每个屏幕。循环过快,动画无法追赶。
是否可以拥有它,以便如果你输入2,例如鳍状肢将会:
Flipper 10
发生幻灯片动画
Flipper 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();
}
}
});
}
}
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"?>
<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_out.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>
<?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>
答案 0 :(得分:0)
答案 1 :(得分:0)
感谢链接,但我看不到如何使用Timer。我在处理程序上找到了一些例子并使用了它。下面的代码似乎有效,但不确定我是否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();
}
};
@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();
}
};