2014-01-13 65 views
3

我已經寫了下面的代碼來首先增加ImageView的大小,並在100ms後減小相同的ImageView的大小。然而,這段代碼增加了ImageView的大小,但它不會減小它的大小,或者延遲100ms後的代碼不會影響imageView的尺寸。setLayoutParams不能第二次工作

我在做什麼錯?

uiHandler.post(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     FrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams(); 
     layout.height = (int) (2*iconsSizeInDP); 
     layout.width = (int) (2*iconsSizeInDP); 
     imageView.setLayoutParams(layout); 
     try 
     { 
      Thread.sleep(50); 
     } 
     catch (InterruptedException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show(); 
     // following code block doesnt'affect imageView dimensions 
     layout.height = (int) iconsSizeInDP; 
     layout.width = (int) iconsSizeInDP; 
     imageView.setLayoutParams(layout); 
    } 
}); 

問候

+0

嘗試創建'LayoutParams'再次在的地方,你說的代碼無法正常工作'FrameLayout.LayoutParams佈局=(android.widget.FrameLayout.LayoutParams)ImageView的。 getLayoutParams();'。同時增加'Thread.sleep()'中的時間。看到差異可能太困難了。 –

+0

當你用睡眠阻止UI線程時,你認爲重新佈局發生在哪個線程中? – laalto

+0

你確定第二個Set沒有被調用嗎?請使用'AsyncTask'而不是'uiHandler'並再次測試。複製'onPreExecute()'中的第一部分''在'onBackground'中休眠並在'onPostExeute()'中設置第二個參數' –

回答

1

更改佈局2倍,在相同的用戶界面線程,因此只有最後一次更改才能生效。 你應該分開爲2 UI線程,就像這樣:

uiHandler.post(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     FrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams(); 
     layout.height = (int) (2*iconsSizeInDP); 
     layout.width = (int) (2*iconsSizeInDP); 
     imageView.setLayoutParams(layout); 
    } 
}; 
uiHandler.postDelayed(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show(); 
     // following code block doesnt'affect imageView dimensions 
     layout.height = (int) iconsSizeInDP; 
     layout.width = (int) iconsSizeInDP; 
     imageView.setLayoutParams(layout); 
    } 
},50); 
+0

Dude偉大的解決方案!順便說一句,爲什麼它沒有在同一個電話中工作? –

+0

因爲UI只會在UI線程結束時更改。 – xtr

+0

好的。剩下的唯一問題是當用戶將他的手指放在imageView上時,imageView會多次調整大小。任何想法如何解決它? –

0

請有上線一看FrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams();

有一個嘗試做的時候,請看看FrameLayout.LayoutParams layout全球