简体   繁体   English

为什么我不能用一种方法在 android 工作室的按钮中更改 2 colors

[英]why i cant change 2 colors in a button on android studio in one method

public void onClick(View v) {
        if (v == b1) {
            b1.setBackgroundColor(Color.WHITE);
            delay(300);
            b1.setBackgroundColor(Color.GREEN);
        }

//-------------------------------------------------------------- /** * @param ms time of delay */ //------------------------------------------------ -------------- /** * @param ms 延迟时间 */

public void delay(int ms) {
    checkDelayArgument(ms);
    Thread thread = Thread.currentThread();
    if (!thread.isInterrupted()) {
        try {
            Thread.sleep(ms);
        } catch (final InterruptedException ignored) {
            thread.interrupt(); // Preserve interrupt status
        }
    }
}

/**
 * @param ms time of delay
 */

private static void checkDelayArgument(int ms) {
    if (ms < 0 || ms > 60000) {
        throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
    }
}
//--------------------------------------------------------------

Result: Only green color change and i don't see the change of white color结果:只有绿色变化,我看不到白色的变化

Your delay method isn't great, it will block the UI thread.您的延迟方法不是很好,它会阻塞 UI 线程。

Try something like this:尝试这样的事情:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
   public void run() {
      runOnUiThread() {
         b1.setBackgroundColor(Color.GREEN);
      }
   }, 300);

but even then that is wonky.但即便如此,这也很不稳定。 What you really want is an Animation instead of this.您真正想要的是 Animation 而不是这个。 Or an AnimationSet.或者一个动画集。

Try something like this, it might set you on the right path.尝试这样的事情,它可能会让你走上正确的道路。

    public static Animator backgroundColorChangeAnimation(final View targetView, int startColor, int endColor, long duration) {
        ArgbEvaluator evaluator = new ArgbEvaluator();
        ValueAnimator animator = new ValueAnimator();
        animator.setIntValues(startColor, endColor);
        animator.setEvaluator(evaluator);
        animator.setDuration(duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int color = (int) animation.getAnimatedValue();
                targetView.setBackgroundColor(color);
            }
        });
        animator.start();

        return animator;
    }

and use it like this:并像这样使用它:

        currentAnim = AnimUtils.backgroundColorChangeAnimation(
                selectedTextView,
                getResources().getColor(R.color.white),
                getResources().getColor(R.color.green),
                300
        );

that will immediatelly run the animator, which will start out at white color and end up in green.这将立即运行动画师,动画师将从白色开始并以绿色结束。

more info is required to hit the nail (of your question) on the head, but you certainly don't want to delay things by calling Thread.sleep().需要更多信息才能准确地说出(您的问题),但您当然不想通过调用 Thread.sleep() 来延迟事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM