Android UI滞后于设置图像

时间:2016-04-04 16:03:42

标签: android imageview android-drawable

我正在尝试创建一个简单的应用程序来了解导航抽屉和设置图像。我将此应用程序基于Android Studio为我创建的模板。

我读到得到drawable可能是一个漫长的过程,所以我为它做了一个Asynctask。尽管如此,我的UI仍然滞后,我得到的消息是,由于主线程上的工作太多,帧被跳过。我发现以下行导致了这些问题:

pic.setImageDrawable(drawable);    //(in onPostExecute)

我在后台完成了所有处理,这一行应该只是设置drawable。为什么这么滞后?

我在某处读到设置尺寸可能会有所不同。现在我将两个维度设置为match_parent,并且图像在视图中居中。我不想给它一个确定的大小。

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private ImageView pic;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    pic = (ImageView) findViewById(R.id.pic);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //set first item to be selected and set its image
    navigationView.getMenu().getItem(0).setChecked(true);
    new ImageSetter().execute();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    try {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    } catch (NullPointerException e) {
        Log.e("back pressed", e.getStackTrace().toString());
    }
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    new ImageSetter(id).execute();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

/**
 * class for getting the image drawable in the background and then setting it
 */
private class ImageSetter extends AsyncTask<Integer, Void, Drawable> {

    int id = //image1;

    public ImageSetter() {
    }

    public ImageSetter(int id) {
        this.id = id;
    }

    /**
     * Get drawable in background
     * Precondition: id has already been set
     * @param params
     * @return drawable of the picture to set
     */
    @Override
    protected Drawable doInBackground(Integer... params) {
        int pic = R.drawable.b1;

        switch (id) {
           //set pic
        }

        return getResources().getDrawable(pic);
    }

    // Once complete, see if ImageView is still around and set drawable
    @Override
    protected void onPostExecute(Drawable drawable) {
        if (pic != null) {
            pic.setImageDrawable(drawable);
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

代码看起来很好。使用setImageDrawable()不应该太糟糕,并且需要在UI线程中运行。您已将任务卸载到另一个线程,这很好。

您获取的源图像有多大? Android在处理大型图像方面非常糟糕......

如果您的源图片太大,我会从Android开发者网站查看Loading Large Bitmaps Efficiently。这将为您提供有关处理位图的一些提示。