Java 如何多次更改LinearLayout视图背景色?
我想多次更改视图背景的颜色,就像一系列颜色一样。 例如:在3秒内涂成蓝色,然后在3秒内涂成绿色,然后在3秒内涂成红色,等等Java 如何多次更改LinearLayout视图背景色?,java,android,view,background-color,Java,Android,View,Background Color,我想多次更改视图背景的颜色,就像一系列颜色一样。 例如:在3秒内涂成蓝色,然后在3秒内涂成绿色,然后在3秒内涂成红色,等等 我尝试了setBackgroundColor方法。 我用不同的颜色打了两次电话,两次电话之间有一个停顿。 但只有最后一种颜色出现 这里是活动: 08-01 07:50:08.109 17450-17450/? I/art: Not late-enabling -Xcheck:jni (already on) 08-01 07:50:08.110 17450-17450/
我尝试了
setBackgroundColor
方法。
我用不同的颜色打了两次电话,两次电话之间有一个停顿。
但只有最后一种颜色出现
这里是活动:
08-01 07:50:08.109 17450-17450/? I/art: Not late-enabling -Xcheck:jni (already on)
08-01 07:50:08.110 17450-17450/? W/art: Unexpected CPU variant for X86 using defaults: x86
08-01 07:50:08.231 17450-17450/com.square.changecolortwotimes W/System: ClassLoader referenced unknown path: /data/app/com.square.changecolortwotimes-1/lib/x86
08-01 07:50:08.286 17450-17450/com.square.changecolortwotimes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-01 07:50:08.395 17450-17450/com.square.changecolortwotimes I/System.out: Change color once --in blue--
08-01 07:50:08.409 17450-17450/com.square.changecolortwotimes W/gralloc_ranchu: Gralloc pipe failed
[ 08-01 07:50:08.409 17450:17450 D/ ]
HostConnection::get() New Host Connection established 0xa6fba240, tid 17450
[ 08-01 07:50:08.475 17450:17477 D/ ]
HostConnection::get() New Host Connection established 0x9b6ff100, tid 17477
08-01 07:50:08.483 17450-17477/com.square.changecolortwotimes I/OpenGLRenderer: Initialized EGL, version 1.4
08-01 07:50:08.488 17450-17477/com.square.changecolortwotimes D/OpenGLRenderer: Swap behavior 1
package com.square.changeColor2次;
导入android.graphics.Color;
导入android.os.Handler;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.LinearLayout;
公共类MainActivity扩展了AppCompatActivity{
线性布局改变色彩布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changingColorLayout=(LinearLayout)findViewById(R.id.changingColorLayout);
//1
System.out.println(“更改颜色一次--蓝色--”);
changingColorLayout.setBackgroundColor(颜色.蓝色);
//等等
changingColorLayout.setBackgroundColor(颜色.蓝色);
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
changingColorLayout.setBackgroundColor(颜色.绿色);
}
},3000);
}
}
这里是活动XML布局:
08-01 07:50:08.109 17450-17450/? I/art: Not late-enabling -Xcheck:jni (already on)
08-01 07:50:08.110 17450-17450/? W/art: Unexpected CPU variant for X86 using defaults: x86
08-01 07:50:08.231 17450-17450/com.square.changecolortwotimes W/System: ClassLoader referenced unknown path: /data/app/com.square.changecolortwotimes-1/lib/x86
08-01 07:50:08.286 17450-17450/com.square.changecolortwotimes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-01 07:50:08.395 17450-17450/com.square.changecolortwotimes I/System.out: Change color once --in blue--
08-01 07:50:08.409 17450-17450/com.square.changecolortwotimes W/gralloc_ranchu: Gralloc pipe failed
[ 08-01 07:50:08.409 17450:17450 D/ ]
HostConnection::get() New Host Connection established 0xa6fba240, tid 17450
[ 08-01 07:50:08.475 17450:17477 D/ ]
HostConnection::get() New Host Connection established 0x9b6ff100, tid 17477
08-01 07:50:08.483 17450-17477/com.square.changecolortwotimes I/OpenGLRenderer: Initialized EGL, version 1.4
08-01 07:50:08.488 17450-17477/com.square.changecolortwotimes D/OpenGLRenderer: Swap behavior 1
- 3秒
- 绿色背景色
我想要的结果是:
08-01 07:50:08.109 17450-17450/? I/art: Not late-enabling -Xcheck:jni (already on)
08-01 07:50:08.110 17450-17450/? W/art: Unexpected CPU variant for X86 using defaults: x86
08-01 07:50:08.231 17450-17450/com.square.changecolortwotimes W/System: ClassLoader referenced unknown path: /data/app/com.square.changecolortwotimes-1/lib/x86
08-01 07:50:08.286 17450-17450/com.square.changecolortwotimes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-01 07:50:08.395 17450-17450/com.square.changecolortwotimes I/System.out: Change color once --in blue--
08-01 07:50:08.409 17450-17450/com.square.changecolortwotimes W/gralloc_ranchu: Gralloc pipe failed
[ 08-01 07:50:08.409 17450:17450 D/ ]
HostConnection::get() New Host Connection established 0xa6fba240, tid 17450
[ 08-01 07:50:08.475 17450:17477 D/ ]
HostConnection::get() New Host Connection established 0x9b6ff100, tid 17477
08-01 07:50:08.483 17450-17477/com.square.changecolortwotimes I/OpenGLRenderer: Initialized EGL, version 1.4
08-01 07:50:08.488 17450-17477/com.square.changecolortwotimes D/OpenGLRenderer: Swap behavior 1
- 蓝色背景色
- 3秒
- 绿色背景色
日志为:
08-01 07:50:08.109 17450-17450/? I/art: Not late-enabling -Xcheck:jni (already on)
08-01 07:50:08.110 17450-17450/? W/art: Unexpected CPU variant for X86 using defaults: x86
08-01 07:50:08.231 17450-17450/com.square.changecolortwotimes W/System: ClassLoader referenced unknown path: /data/app/com.square.changecolortwotimes-1/lib/x86
08-01 07:50:08.286 17450-17450/com.square.changecolortwotimes W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-01 07:50:08.395 17450-17450/com.square.changecolortwotimes I/System.out: Change color once --in blue--
08-01 07:50:08.409 17450-17450/com.square.changecolortwotimes W/gralloc_ranchu: Gralloc pipe failed
[ 08-01 07:50:08.409 17450:17450 D/ ]
HostConnection::get() New Host Connection established 0xa6fba240, tid 17450
[ 08-01 07:50:08.475 17450:17477 D/ ]
HostConnection::get() New Host Connection established 0x9b6ff100, tid 17477
08-01 07:50:08.483 17450-17477/com.square.changecolortwotimes I/OpenGLRenderer: Initialized EGL, version 1.4
08-01 07:50:08.488 17450-17477/com.square.changecolortwotimes D/OpenGLRenderer: Swap behavior 1
谢谢
我保留的解决方案
package com.square.moodlighting;
导入android.animation.argbeevaluator;
导入android.animation.ObjectAnimator;
导入android.animation.ValueAnimator;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.widget.LinearLayout;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
线性布局改变色彩布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changingColorLayout=(LinearLayout)findViewById(R.id.changingColorLayout);
ValueAnimator colorAnim=ObjectAnimator.ofit(更改ColorLayout,“backgroundColor”,generateColors());
彩色动画设定持续时间(30000);
colorAnim.setRepeatCount(500000000);
colorAnim.setEvaluator(新的argbeEvaluator());
colorAnim.start();
}
私有int[]生成颜色(){
列表颜色=新的ArrayList();
对于(intr=0;r<100;r++)颜色,添加(Color.rgb(r*255/100255,0));
对于(intg=100;g>0;g--)颜色,添加(Color.rgb(255,g*255/100,0));
对于(intb=0;b<100;b++)颜色,添加(Color.rgb(255,0,b*255/100));
对于(intr=100;r>0;r--)颜色,添加(Color.rgb(r*255/100,0255));
对于(intg=0;g<100;g++)颜色,添加(Color.rgb(0,g*255/100255));
对于(intb=100;b>0;b--)颜色,添加(Color.rgb(0255,b*255/100));
添加(Color.rgb(0,255,0));
返回阵列(颜色);
}
私有int[]toIntArray(列表){
int[]ret=new int[list.size()];
对于(int i=0;i
使用处理程序
更新用户界面
changingColorLayout.setBackgroundColor(Color.BLUE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
changingColorLayout.setBackgroundColor(Color.GREEN);
}
},3000);
不要在主线程中执行Thread.sleep()
它将在用户界面睡眠时停止用户界面
使用处理程序在延迟后更改颜色
changingColorLayout.setBackgroundColor(Color.BLUE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
changingColorLayout.setBackgroundColor(Color.RED);
}
},3000); //3000 milliseconds = 3 seconds
您可以使用ObjectAnimator:
package com.square.changeColor2次;
导入android.graphics.Color;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.LinearLayout;
公共类MainActivity扩展了AppCompatActivity{
线性布局改变色彩布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changingColorLayout=(LinearLayout)findViewById(R.id.changingColorLayout);
ValueAnimator colorAnim=ObjectAnimator.ofInt(更改ColorLayout,“backgroundColor”,Color.BLUE,Color.GREEN);
彩色动画设定持续时间(3000);
colorAnim.setEvaluator(新的argbeEvaluator());
colorAnim.start();
}
}
要重复此动画,可以在colorAnim.start()之前添加以下内容:
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
您可以阅读有关属性动画的更多信息。请勿使用线程,首先使用可运行的处理程序请勿使用线程使用处理程序更新主线程上的UInever dothread.sleep()
。您可以使用Handler.postDelayed(…)
通过Runnable.Changed with Handler Runnable组合来推迟executionChanged线程。谢谢汉克斯·纳提格。我使用了您的解决方案,因为它允许更改任意多的颜色