长按后,上下文操作栏太慢

时间:2015-04-27 00:44:33

标签: java android

我有一个活动,显示存储在SD卡上的图像列表,虽然正在工作,但却产生两种不良情况。

1)取消进度对话框后会有大约3秒的延迟。我想在进度对话框消失后立即显示图像。

2)长时间点击图片后,上下文操作栏显示的时间太长(~4秒)。

...   
04-26 21:23:03.902  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 235K, 9% free 26140K/28604K, paused 21ms, total 21ms
04-26 21:23:04.702  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 21K, 9% free 26453K/28936K, paused 20ms, total 20ms
04-26 21:23:04.712  18438-18438/co.glurl.appoid I/dalvikvm-heap﹕ Grow heap (frag case) to 31.831MB for 4147216-byte allocation
04-26 21:23:04.782  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 17K, 8% free 30519K/32988K, paused 17ms, total 18ms
04-26 21:23:05.362  18438-18438/co.glurl.appoid I/Choreographer﹕ Skipped 301 frames!  The application may be doing too much work on its main thread.
04-26 21:23:09.902  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 11106K, 35% free 24621K/37752K, paused 18ms, total 18ms
04-26 21:23:11.452  18438-18438/co.glurl.appoid D/dalvikvm﹕ GC_FOR_ALLOC freed 157K, 30% free 26459K/37752K, paused 18ms, total 18ms
04-26 21:23:12.072  18438-18438/co.glurl.appoid I/Choreographer﹕ Skipped 291 frames!  The application may be doing too much work on its main thread.

代码:

public class PhotosGridViewActivity extends Activity {

private PhotosUtils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private PhotosGridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;
private int items;
private ProgressDialog progressDialog;
private boolean setEditableMode = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photos_grid_view);
    //Initialize a LoadViewTask object and call the execute() method
    new LoadViewTask().execute();
}

//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
    //Before running code in the separate thread
    @Override
    protected void onPreExecute() {
        //Create a new progress dialog
        progressDialog = new ProgressDialog(PhotosGridViewActivity.this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) {
        // Initilizing Grid View
        InitilizeGridLayout();
        // loading all image paths from SD card
        imagePaths = utils.getFilePaths();
        // Gridview adapter
        adapter = new PhotosGridViewImageAdapter(PhotosGridViewActivity.this, imagePaths,
                columnWidth);
        try {
            //Get the current thread's token
            synchronized (this) {
                //Initialize an integer (that will act as a counter) to zero
                int counter = 0;
                //While the counter is smaller than four
                while(counter <= 4) {
                    //Wait 850 milliseconds
                    this.wait(1000);
                    //Increment the counter
                    counter++;
                    //Set the current progress.
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    //Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) {
        //set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    //after executing the code in the thread
    @Override
    protected void onPostExecute(Void result){
        //close the progress dialog
       try {
            progressDialog.dismiss();
            //initialize the View
        } catch (Exception e) {
            e.printStackTrace();
        }
        // setting grid view adapter
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                gridView.getItemAtPosition(position);
                    Intent i = new Intent(PhotosGridViewActivity.this, PhotosScreenViewActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    i.putExtra("position", position);
                    PhotosGridViewActivity.this.startActivity(i);
            }
        });
    }
}

private void InitilizeGridLayout() {
    utils = new PhotosUtils(PhotosGridViewActivity.this);
    gridView = (GridView) findViewById(R.id.grid_view);
    Resources r = getResources();
    float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            AppConstant.GRID_PADDING, r.getDisplayMetrics());

    columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);


    gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
    gridView.setColumnWidth(columnWidth);
    gridView.setStretchMode(GridView.NO_STRETCH);
    gridView.setPadding((int) padding, (int) padding, (int) padding,
            (int) padding);
    gridView.setHorizontalSpacing((int) padding);
    gridView.setVerticalSpacing((int) padding);

    gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
    gridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
}

public class MultiChoiceModeListener implements
        GridView.MultiChoiceModeListener {
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle("Select Items");
        mode.setSubtitle("One item selected");
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                          long id, boolean checked) {
        int selectCount = gridView.getCheckedItemCount();
        switch (selectCount) {
            case 1:
                mode.setSubtitle("One item selected");
                break;
            default:
                mode.setSubtitle("" + selectCount + " items selected");
                break;
        }
    }
}

}

出了什么问题?

2 个答案:

答案 0 :(得分:0)

问题出在这里

  while(counter <= 4) {
                    //Wait 1 second <-this means 5 seconds you will wait because it will be repeated 5 times
                    this.wait(1000); //edit this or remove it
                    //Increment the counter
                    counter++;
                    //Set the current progress.
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }

答案 1 :(得分:0)

好的......因为我在这里无法帮助,我用Google搜索并发现有必要异步加载我的getView()。对于further引用。