2011-09-10 44 views
0

嗨我是一個初學者的android操作系統編程,我陷入了一個問題,我無法弄清楚如何做一個動態的背景,基於計時器(說每10秒一個背景變化到另一個)我有一些代碼,但它有錯誤出現,這裏有一個例子:基於定時器(線性佈局)的動態背景,如何?

private static final long GET_DATA_INTERVAL = 10000; 
int images[] = {R.drawable.smothie1,R.drawable.omletherb1}; 
int index = 0; 
ImageView img; 
Handler hand = new Handler(); 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.main); 
    LinearLayout layout= (LinearLayout)findViewById(R.id.LinearView1); 
    hand.postDelayed(run, GET_DATA_INTERVAL); 
} 

Runnable run = new Runnable() { 
    public void run() { 
     layout.setBackgroundResource(LinearView1).getDrawable(images[index++]); 
     if (index == images.length) 
      index = 0; 
     hand.postDelayed(run, GET_DATA_INTERVAL); 

任何幫助將大大apprieciated:d感謝

編輯:我得到的錯誤是在這條線:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]); 

它說:

-layout不能得到解決

-the方法getDrawable(INT)是未定義的類型對象


此錯誤:

layout.setBackgroundResource(LinearView1).getDrawable(images[index++]); 

它說即:

-layout無法解析

-the方法getDrawable(INT)是未定義的類型對象

請幫助:)

+1

什麼錯誤到底是什麼? – Staven

+0

請仔細閱讀下面的內容:D我把它作爲一個aswer,這似乎不接受代碼;) – Bercik

+0

@Albert:如果你有額外的信息添加到你的問題,請編輯你的問題,而不是添加一個答案。你添加的內容不是'答案',因爲它不能回答你的問題。 –

回答

1

我終於算出來,去掉了一些錯誤後,我已經想到了這一點(和其工作):

公共類CookBookActivity extends活動{0}當活動首次創建時調用。 */

private static final long GET_DATA_INTERVAL = 1000; 
int images[] = {R.drawable.omletherb1,R.drawable.smothie1}; 
int index = 0; 
LinearLayout img; 
Handler hand = new Handler(); 
private LinearLayout layout; 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.main); 
    layout = (LinearLayout)findViewById(R.layout.main); 
    hand.postDelayed(run, GET_DATA_INTERVAL); 

    Typeface tf2 = Typeface.createFromAsset(getAssets(), 
      "fonts/BPreplay.otf"); 
    TextView tv2 = (TextView) findViewById(R.id.textView2); 
    tv2.setTypeface(tf2); 


    Typeface tf = Typeface.createFromAsset(getAssets(), 
      "fonts/BPreplay.otf"); 
    TextView tv = (TextView) findViewById(R.id.textView1); 
    tv.setTypeface(tf); 


    Button mainNext = (Button) findViewById(R.id.nextScreen1); 
    mainNext.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      Intent i = new Intent(); 
      i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1"); 
      startActivity(i); 

     } 
    }); 
} 

Runnable run = new Runnable() { 
    public void run() { 
     layout.setBackgroundDrawable(getDrawable(index++)); 
     if (index == images.length) 
      index = 0; 
     hand.postDelayed(run, GET_DATA_INTERVAL); 

    } 
}; 

protected Drawable getDrawable(int i) { 
    // TODO Auto-generated method stub 
    return getResources().getDrawable(images[i%2]); 
} 

}