我有类似的布局。
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mylayout" > </RelativeLayout>
java - 然后您可以使用以下代码动态更改布局的背景
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mylayout);
int images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
private Timer myTimer;
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
@Override
public void run()
{
TimerMethod();
}
}, 0, 9000);
}
private void TimerMethod()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//TODO after 9 sec
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}
}, 9000);
}
}
这是日志跟踪
01-04 01:08:15.307: E/AndroidRuntime(30200): FATAL EXCEPTION: Timer-0
01-04 01:08:15.307: E/AndroidRuntime(30200): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-04 01:08:15.307: E/AndroidRuntime(30200): at android.os.Handler.<init>(Handler.java:121)
01-04 01:08:15.307: E/AndroidRuntime(30200): at info.androidhive.slidingmenu.LoginActivity.TimerMethod(LoginActivity.java:55)
01-04 01:08:15.307: E/AndroidRuntime(30200): at info.androidhive.slidingmenu.LoginActivity.access$0(LoginActivity.java:53)
01-04 01:08:15.307: E/AndroidRuntime(30200): at info.androidhive.slidingmenu.LoginActivity$1.run(LoginActivity.java:48)
01-04 01:08:15.307: E/AndroidRuntime(30200): at java.util.Timer$TimerImpl.run(Timer.java:284)
我想尝试的是在活动时自动更改它。
答案 0 :(得分:3)
onCreate()
仅被调用一次。离开后,调用onPause()
,稍后当您返回活动时,会调用onResume()
。
因此,要在每次导航到“活动”时更改背景,请转换代码以将背景从onCreate()
更改为onResume()
。
public class MainActivity extends Activity {
RelativeLayout relativeLayout;
int images[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.mylayout);
images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
}
protected void onResume()
{
if(relativeLayout != null)
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}
private int getRandomNumber() {
//Note that general syntax is Random().nextInt(n)
//It results in range 0-4
//So it should be equal to number of images in images[] array
return new Random().nextInt(4);
}}
答案 1 :(得分:2)
您可以使用Timers
和Handlers
来完成此操作
试试这段代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mylayout);
int images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
private Timer myTimer;
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
@Override
public void run()
{
TimerMethod();
}
}, 0, 9000);
}
private void TimerMethod()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//TODO after 9 sec
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}
}, 9000);
}
}
答案 2 :(得分:0)
尝试拨打relativeLayout.setBackgroundResource(images[getRandomNumber()]);
在您的活动onResume()
这样的方法上。
protected void onResume()
{
if(relativeLayout != null){
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}
}
希望这有帮助。
答案 3 :(得分:0)
在onCreate
方法中,您的代码只会执行一次,如果您希望在一段时间间隔后更改代码,请使用TimerTask
,或者也可以使用Handler
。
答案 4 :(得分:0)
首先在drawable res中创建transition_drawable.xml
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/one"/>
<item android:drawable="@drawable/two"/>
<item android:drawable="@drawable/three"/>
`
并在你的布局中
android:background="@drawable/transition_drawable"
并在您的activity.java中
int DrawableImage[] = {R.drawable.one , R.drawable.two, R.drawable.three};
final Handler handler = new Handler();
final int[] i = {0};
final int[] j = {1};
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Resources res = getApplicationContext().getResources();
TransitionDrawable out = new TransitionDrawable(new Drawable[]{res.getDrawable(DrawableImage[i[0]]), res.getDrawable(DrawableImage[j[0]])});
out.setCrossFadeEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
yourlayout.setBackground(out);
}
out.startTransition(2000);
i[0]++;
j[0]++;
if (j[0] == DrawableImage.length) {
j[0] = 0;
}
if (i[0] == DrawableImage.length) {
i[0] = 0;
}
handler.postDelayed(this, 4000);
}
});
}
}, 0);
答案 5 :(得分:0)
我使它像这样工作:
private int[] backgroundImages = {R.drawable.bg1, R.drawable.bg2, R.drawable.bg3, R.drawable.bg4};
RelativeLayout background = findViewById(R.id.background);
background.setBackgroundResource(backgroundImages[new Random().nextInt(4)]);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
background.setBackgroundResource(backgroundImages[new Random().nextInt(4)]);
}
}, 3000);
}
}, 0, 3000);
答案 6 :(得分:-1)
我知道这个老问题,但是我会把我的答案发布给其他人,您可以使用以下代码
public class myActivity extends AppCompatActivity {
RelativeLayout relativeLayout;
int images[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
relativeLayout = (RelativeLayout) findViewById(R.id.mylayout);
images = new int[{R.drawable.fighter3,R.drawable.sky,R.drawable.yellow_bullet,R.drawable.chicken3};
//Create the timer object which will run the desired operation on a schedule or at a given time
Timer timer = new Timer();
//Create a task which the timer will execute. This should be an implementation of the TimerTask interface.
//I have created an inner class below which fits the bill.
MyTimer mt = new MyTimer();
//We schedule the timer task to run after 1000 ms and continue to run every 1000 ms.
timer.schedule(mt, 1000, 1000);
}
private int getRandomNumber() {
//Note that general syntax is Random().nextInt(n)
//It results in range 0-4
//So it should be equal to number of images in images[] array
return new Random().nextInt(4);
}
class MyTimer extends TimerTask {
public void run() {
//This runs in a background thread.
//We cannot call the UI from this thread, so we must call the main UI thread and pass a runnable
runOnUiThread(new Runnable() {
public void run() {
Random rand = new Random();
//The random generator creates values between [0,256) for use as RGB values used below to create a random color
//We call the RelativeLayout object and we change the color. The first parameter in argb() is the alpha.
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}
});
}
}
}