![](/img/trans.png)
[英]How can I fix this code so my android textview updates with the countdowntimer and shows the seconds that are left?
[英]How can I show the fade in animation and countdowntimer one time for each textview?
我正在使用Android Studio制作基于文本的游戏。 这是活动:
public class MainActivity extends Activity {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt1 = (TextView)findViewById(R.id.textView5);
final TextView txt3 = (TextView)findViewById(R.id.textView6);
final TextView txt2 = (TextView)findViewById(R.id.textView7);
final TextView txt4 = (TextView)findViewById(R.id.textView8);
final ImageView img1 = (ImageView)findViewById(R.id.imageView2);
new CountDownTimer(2000, 1000) {
@Override
public void onTick(long millisUntilFinished) {}
@Override
public void onFinish() {
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt1.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt2.startAnimation(fadeIn2);
txt1.setText("Text");
txt1.setVisibility(View.VISIBLE);
txt2.setText("Text");
txt2.setVisibility(View.VISIBLE);
}
}.start();
new CountDownTimer(5000, 1000){
@Override
public void onTick(long millisUntilFinished){}
@Override
public void onFinish(){
Animation fadeIn_img = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
img1.startAnimation(fadeIn_img);
Animation fadeIn = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt3.startAnimation(fadeIn);
Animation fadeIn2 = AnimationUtils.loadAnimation(GameActivity.this, R.anim.fadein);
txt4.startAnimation(fadeIn2);
img1.setImageResource(R.drawable.img);
txt3.setText("Text");
txt3.setVisibility(View.VISIBLE);
txt4.setText("Text");
txt4.setVisibility(View.VISIBLE);
}
}.start();
我希望动画和倒数计时器对于每个textview只工作一次。 当我重新启动游戏时,textviews必须不显示任何内容。 这就像每2秒连续显示一次的文本视图列表。 当用户关闭然后重新启动应用程序时,他必须找到完成状态。 我怎样才能做到这一点?
谢谢。
因此,看起来像是昏厥的文本视图,对不对? 这是一个例子
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
LinearLayout Container;
ArrayList<View> textList = new ArrayList<>();
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Container = (LinearLayout) findViewById(R.id.Container);
for (int i = 0; i < Container.getChildCount(); i++) {
textList.add(Container.getChildAt(i));
}
fadeView(textList.get(0));
}
private void fadeView(final View view) {
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.fadein);
count++;
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
if (count == textList.size()) return;
fadeView(textList.get(count));
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
view.startAnimation(anim);
}
}
fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1.0"
android:repeatCount="0"
android:toAlpha="0.0" />
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is first"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is second"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is third"
android:textSize="30dp" />
</LinearLayout>
只需确保将所有视图(在本例中为textviews)放入数组中即可使其变得整洁而正确。 希望能帮助到你。 欢呼快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.