本文介绍了暂停制作ImageView的无形和可见的Andr​​oid之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Android应用程序,并遇到了一个问题,而我是编码应用程序的主要部分。

我需要有一个ImageView的消失一两秒钟,然后挖掘时再次出现。攻丝部分作品中,我使用XML,使其可以点击的,然后调用点击方法。这个问题在我调用该方法。我需要它:


  1. 消失

  2. 添加一到playerscore

  3. 重新显示playerscore

  4. 重新出现与几秒钟的延迟

我的问题是,它不会明显消失,无论我做什么。我preFER从Thread.sleep()方法躲得远远的,因为我希望用户能够利用其他事情,而它是无形的(如果我误解了Thread.sleep(),让我知道。如果有可能有充分的方法是自己的线程,请告诉我怎么样。我仍然pretty在新的Java)。但是,我也尝试使用Thread.sleep()方法,并暂停code的前行睡觉的声明,但并没有明显的影响消失,重新出现之间的时间量。 (这可能听起来有点混乱看到code将有助于解释它)。我还试图用一个while语句数到100,然后让它重新出现在计数器打了100,但没有奏效。我想过使用一个计时器,但我需要一个非常简明的例子,因为这将计时器需要进入24个不同的方法。这里是code,它应该帮助解释了一下。

这是我想的第一件事,这是我preferably不想用

 公共无效TB1(查看视图){
    b1.setVisibility(View.INVISIBLE); //使无形的ImageView
    playerscore ++;
    score.setText(SCORE:+ playerscore); //睡眠受影响该行出于某种原因
    视频下载(1000)
    b1.setVisibility(View.VISIBLE)//使ImageView的可见
 }

这是我试过的第二件事。

 公共无效TB1(查看视图)抛出InterruptedException的{
    INT I = 0;
    b1.setVisibility(View.INVISIBLE);
    playerscore ++;
    score.setText(SCORE:+ playerscore);
    而(ⅰ&小于100){
        我++;
    }
    如果(我== 100){
    b1.setVisibility(View.VISIBLE);
    }
    }

有关这一项,就好像我做了什么。没有推迟了。

我真的不知道什么尝试。我使用什么不对,或只是简单的使用了错误的事情吗?请耐心等待着我,我仍然在Java的新的和我不是很有经验。

感谢您预先的任何/所有帮助!



UPDATE
我说从答案code,但一切都没有改变。它消失,但没有再出现。难道我有什么语法错误或任何一个值,错了吗?

 公共无效TB1(查看视图){
    b1.setImageAlpha(0);
    。b1.animate()α(1F).setDuration(0).setStartDelay(3000)。开始();
    playerscore ++;
    score.setText(SCORE:+ playerscore);    }


解决方案

如果你不需要任何动画就用这个code,假设 B1 是你的类变量:

  b1.setVisibility(View.INVISIBLE);
b1.postDelayed(新的Runnable(){
    @覆盖
    公共无效的run(){
        b1.setVisibility(View.VISIBLE);
    }
},3000);

I'm writing an Android app, and ran into a problem while I was coding the main part of the app.

I need to have an ImageView disappear for a second or two, and then reappear when tapped. The tapping part works, I use XML to make it clickable and then call a method on click. The problem is in the method that I call. I need it to:

  1. Disappear
  2. Add one to the playerscore
  3. Re-display the playerscore
  4. Reappear with a delay of a few seconds

My problem is that it does not noticeably disappear no matter what I do. I prefer to stay away from Thread.sleep() because I want the user to be able to tap other things while it is invisible (If I am misunderstanding Thread.sleep(), let me know. If it is possible to have every method be its own thread, please show me how. I am still pretty new at Java.) However, I did try to use Thread.sleep(), and it paused the line of code BEFORE the sleep statement, but did not visibly affect the amount of time between disappear and reappear. (that probably sounds confusing seeing the code will help explain it). I also tried to use a while statement to count up to 100 and then make it reappear when the counter had hit 100, but that did not work. I thought about using a timer, but I would need a very condensed example, as this timer would need to go into 24 different methods. Here is the code, it should help explain it a bit.

This is the first thing I tried, which I preferably do not want to use

public void tb1(View view){
    b1.setVisibility(View.INVISIBLE); //Make the ImageView Invisible
    playerscore ++;
    score.setText("SCORE: " + playerscore); //The sleep affected this line for some reason
    Thread.sleep(1000)
    b1.setVisibility(View.VISIBLE) //Make the ImageView Visible
 }

This is the second thing I tried.

public void tb1(View view) throws InterruptedException{
    int i = 0;
    b1.setVisibility(View.INVISIBLE);
    playerscore ++;
    score.setText("SCORE: " + playerscore);
    while (i < 100){
        i++;
    }
    if(i == 100){
    b1.setVisibility(View.VISIBLE);
    }
    }

For this one, it was like I had done nothing. Nothing was delayed.

I really don't know what try. Am I using something wrong, or just plain using the wrong thing? Please be patient with me, I am still new at Java and am not very experienced.

Thank you in advance for any/all help!


UPDATEI added the code from the answers but nothing has changed. It disappears, but does not reappear. Do I have any syntax or any of the values wrong?

        public void tb1(View view){
    b1.setImageAlpha(0);
    b1.animate().alpha(1f).setDuration(0).setStartDelay(3000).start();
    playerscore++;
    score.setText("SCORE: " + playerscore);

    }
解决方案

If you don't need any animation then just use this code, assuming b1 is your class variable:

b1.setVisibility(View.INVISIBLE);
b1.postDelayed(new Runnable() {
    @Override
    public void run() {
        b1.setVisibility(View.VISIBLE);
    }
}, 3000);

这篇关于暂停制作ImageView的无形和可见的Andr​​oid之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 04:08