简体   繁体   English

Android UI滞后于设置图像

[英]Android UI lag from setting image

I'm trying to create a simple app to learn about navigation drawers and setting images. 我正在尝试创建一个简单的应用程序,以了解导航抽屉和设置图像。 I based this app on the template that Android Studio created for me. 我将此应用程序基于Android Studio为我创建的模板。

I read that getting the drawable can be a long process so I made an Asynctask for it. 我读到获取drawable可能是一个漫长的过程,因此我为此创建了一个Asynctask。 Despite this, my UI still lags and I get the message that frames are being skipped due to too much work on the main thread. 尽管如此,我的UI仍然滞后,并且我收到消息,由于主线程上的过多工作而导致帧被跳过。 I figured out that the following line is causing those issues: 我发现以下行导致了这些问题:

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

I did all the processing in the background and this line should just be setting the drawable. 我在后台进行了所有处理,此行应仅设置可绘制对象。 Why is this lagging so much? 为什么这个滞后如此之大?

I read somewhere that setting the dimensions might make a difference. 我在某处读到,设置尺寸可能会有所不同。 Right now I have both dimensions set to match_parent and the image is centered in the view. 现在,我将两个尺寸都设置为match_parent,并且图像在视图中居中。 I don't want to give it a definite size. 我不想给它一个确定的大小。

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

} }

The code looks fine to me. 代码对我来说看起来不错。 using setImageDrawable() shouldn't be lagging too bad and does need to be run in the UI thread. 使用setImageDrawable()不应太落后,并且需要在UI线程中运行。 You have offloaded the task to another thread, which is good. 您已将任务卸载到另一个线程,这很好。

How big is the source image that you are fetching? 您要提取的源图像有多大? Android is notoriously bad at handling large images... 众所周知,Android在处理大图像方面很差。

If your source image is too big, I would look at Loading Large Bitmaps Efficiently from the Android developer site. 如果您的源图像太大,我会从Android开发人员站点查看“ 有效加载大型位图” This will give you some tips on handling Bitmaps. 这将为您提供一些处理位图的技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM