1

我正在尝试使用 asynctask 加载全屏图库菜单。我看过很多关于 asynctask 的代码来加载图像列表,但不是以画廊的形式。我希望用户在查看 main.xml 之前先查看 loadingscreen.xml。但据我了解,Asynctask 不是这样工作的。

所以:(如果我错了,请纠正我)
当画廊类文件启动时,它开始异步并在后台执行操作。
视图切换器允许用户切换视图。

部分代码取自 hello world 站点和其他参考资料。目前,我必须为加载屏幕和画廊编写单独的代码(能够运行良好)。我认为它以某种方式工作,但我错了。

GalleryLoop.java

public class GalleryLoop extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   // startDownload();
    // Reference the Gallery view
    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setSpacing(2);

    // Set the adapter to our custom adapter (below)
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                switch( position )
                {
                case 0:
                     Intent newActivity0 = new Intent(GalleryLoop.this, bo_seng.class);     
                     startActivity(newActivity0);
                    break;
                case 1:  Intent newActivity = new Intent(GalleryLoop.this, war_memorial.class);     
                startActivity(newActivity);
                break;
                }
            }
    }); 

}


//    private void startDownload() {
    // TODO Auto-generated method stub
 //     new Intent(GalleryLoop.this, LoadingScreenActivity.class);
//  }


public class ImageAdapter extends BaseAdapter {
    int  mGalleryItemBackground;

    public ImageAdapter(Context c) {
        mContext = c;
    }


    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setBackgroundResource(mGalleryItemBackground);
        imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        return imageView;

    }

    private Context mContext;


    private Integer[] mImageIds = {
            R.drawable.gallery_photo_1,
            R.drawable.gallery_photo_2,
            R.drawable.gallery_photo_3,
            R.drawable.gallery_photo_4,
            R.drawable.gallery_photo_5,
            R.drawable.gallery_photo_6,
            R.drawable.gallery_photo_7,
            R.drawable.gallery_photo_8
    };

}
}

LoadingScreenActivity.java

public class LoadingScreenActivity extends Activity 
{
//creates a ViewSwitcher object, to switch between Views
private ViewSwitcher viewSwitcher;
public View ImageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    //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>
{
    //A TextView object and a ProgressBar object
    private TextView tv_progress;
    private ProgressBar pb_progressBar;

    //Before running code in the separate thread
    @Override
    protected void onPreExecute() 
    {
        //Initialize the ViewSwitcher object
        viewSwitcher = new ViewSwitcher(LoadingScreenActivity.this);
        /* Initialize the loading screen with data from the 'loadingscreen.xml' layout xml file. 
         * Add the initialized View to the viewSwitcher.*/
        viewSwitcher.addView(ViewSwitcher.inflate(LoadingScreenActivity.this, R.layout.loadingscreen, null));

        //Initialize the TextView and ProgressBar instances - IMPORTANT: call findViewById() from viewSwitcher.
        tv_progress = (TextView) viewSwitcher.findViewById(R.id.tv_progress);
        pb_progressBar = (ProgressBar) viewSwitcher.findViewById(R.id.pb_progressbar);
        //Sets the maximum value of the progress bar to 100             
        pb_progressBar.setMax(100);

        //Set ViewSwitcher instance as the current View.
        setContentView(viewSwitcher);
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) 
    {
        /* This is just a code that delays the thread execution 4 times, 
         * during 850 milliseconds and updates the current progress. This 
         * is where the code that is going to be executed on a background
         * thread must be placed. 
         */
        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(850);
                    //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 TextView and the progress at progress bar
    @Override
    protected void onProgressUpdate(Integer... values) 
    {
        //Update the progress at the UI if progress value is smaller than 100
        if(values[0] <= 100)
        {
            tv_progress.setText("Progress: " + Integer.toString(values[0]) + "%");
            pb_progressBar.setProgress(values[0]);
        }
    }

    //After executing the code in the thread
    @Override
    protected void onPostExecute(Void result) 
    {
        /* Initialize the application's main interface from the 'main.xml' layout xml file. 
         * Add the initialized View to the viewSwitcher.*/
        viewSwitcher.addView(ViewSwitcher.inflate(LoadingScreenActivity.this, R.layout.main, null));
        //Switch the Views
        viewSwitcher.showNext();
        //ImageView = viewSwitcher.findViewById(R.id.imageView1);
        setContentView(R.layout.main);
    }
}

//Override the default back key behavior
@Override
public void onBackPressed() 
{
    //Emulate the progressDialog.setCancelable(false) behavior
    //If the first view is being shown
    if(viewSwitcher.getDisplayedChild() == 0)
    {
        //Do nothing
        return;
    }
    else
    {
        //Finishes the current Activity
        super.onBackPressed();
    }
}
}
4

0 回答 0