4

I have written the following code to first increase the size of ImageView and after 100ms decrease the size of the same ImageView. However this code increases the size of ImageView but it doesn't decrease it's size or the code after 100ms delay doesn't affect the imageView dimensions.

What am I doing wrong?

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);
    }
});

Regards

Soma
  • 861
  • 2
  • 17
  • 32
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • Try creating `LayoutParams` again at place where you said code is not working `FrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams();`. Also increase time in `Thread.sleep()`. It might be too dificult to see the difference. – Bipin Bhandari Jan 13 '14 at 08:46
  • When you keep blocking the UI thread with sleep, in which thread you think the re-layout takes place? – laalto Jan 13 '14 at 08:51
  • are you sure that second Set is not called ? please use `AsyncTask` instead of `uiHandler` and test again.copy first part in `onPreExecute()` sleep in `onBackground` and set second params in `onPostExeute()` – Shayan Pourvatan Jan 13 '14 at 09:43

2 Answers2

3

You change the layout 2 times in the same UI thread, so only the last change can take effect. You should separate to 2 UI thread, like this:

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);
xtr
  • 5,870
  • 1
  • 21
  • 23
  • Ok. The only problem left is the imageView resizes multiple times when the user has put his finger on the imageView. Any idea on how to solve it? – Umer Farooq Jan 14 '14 at 07:03
0

please have a look on the line FrameLayout.LayoutParams layout = (android.widget.FrameLayout.LayoutParams) imageView.getLayoutParams();

please have a look when have a try to make FrameLayout.LayoutParams layout global

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43