自动切换背景

时间:2012-04-06 07:41:31

标签: android background-image

如何自动切换背景

布局

android:background="@drawable/image">
我有两张照片 我想允许自动切换

3 个答案:

答案 0 :(得分:1)

Timer timer = new Timer();
TimerTask timerTask  = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(loadFirst){
loadFirst = false;
layout_reference.setBackgroundResource(R.drawable.imagename1);
}else{
loadFirst = true;
layout_reference.setBackgroundResource(R.drawable.imagename2);
}
}
};
timer.schedule(timerTask,0,10000);//fires every 10secs 

在代码中尝试此代码,并在oncreate方法中声明一个布尔变量“boolean loadFirst = false”()

答案 1 :(得分:0)

创建一个线程并分配睡眠时间并将layout.setBackgroundDrawable(drawable)与sleep方法放在一起,并且可以使用flag变量在两个背景drawable之间切换,如果1 set 2和2 set 1 < / p>

或者您可以将layout.setBackgroundDrawable(drawable)代码放入runOnUIThread。

答案 2 :(得分:0)

尝试这种方式:

public class mainActivity extends Activity 
    {
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Resources res = getResources(); //resource handle
        Drawable drawable = res.getDrawable(R.drawable.images1); //new Image that was added to the res folder
        LinearLayout linearLayout =   (LinearLayout)findViewById(R.id.etxtLayout); 
        linearLayout.setBackgroundDrawable(drawable);
        }
    }