我编写了以下代码,首先增加ImageView的大小,然后在100ms后减小同一ImageView的大小。但是,此代码会增加ImageView的大小,但不会减小它的大小,或者100ms延迟后的代码不会影响imageView的尺寸。

我究竟做错了什么?

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

问候

最佳答案

您在同一UI线程中两次更改了布局,因此只有最后一次更改才能生效。
您应该将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);

10-07 12:04