用户从android中的图库中选择图像后如何加载进度条圆?

时间:2014-06-04 11:27:49

标签: android android-gallery android-progressbar

我正在做一个应用程序,用户从图库中选择一个图像,然后从图库中选择图像进入第二个活动,并在第二个活动中显示它。但是需要一些时间(约3秒)才能进入第二个活动用户点击图库中的图像。我希望在用户从图库中选择图像后显示进度条圆圈,并且当执行移动到第二个活动时想要使进度条圆不可见。我不知道如何做我的任务?我应该使用任何AsyncTask吗?请帮助我。我被困在这里。 我正在提供我的示例代码。

我的第一个活动是

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
 import android.widget.ImageButton;



 public class LauncherActivity extends Activity 
{
private static int RESULT_LOAD_IMAGE = 2;
ImageButton gallery;
Bitmap bitmap_for_gallery;



 protected void onCreate(Bundle paramBundle)
 {
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (ImageButton)findViewById(R.id.select_photo);

  gallery.setOnClickListener(new OnClickListener() {

    @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub          
        Intent gallery_intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(gallery_intent, RESULT_LOAD_IMAGE);

    }
  });
    }
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)     {
      Uri selectedImage = data.getData();    
     ProgressDialog progress=new ProgressDialog(getApplicationContext());
     progress.setIndeterminate(true);
     progress.setTitle("Please wait");
     progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     progress.show();


      String[] filePathColumn = { MediaStore.Images.Media.DATA };
      Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
      cursor.moveToFirst();
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String picturePath = cursor.getString(columnIndex);
      cursor.close();

      progress.dismiss();

      Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
      intent.putExtra("path", picturePath); 
      startActivity(intent); 
     }
 }
}

我的第一个活动xml

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"
 >
  <ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/homepage"
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:scaleType="fitXY"/>

  <ImageButton
    android:id="@+id/select_photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="126dp"
    android:background="@android:color/transparent"
    android:src="@drawable/select_photo" />

  </RelativeLayout>

我的第二项活动是

   import android.app.Activity;
   import android.graphics.Bitmap;
   import android.graphics.BitmapFactory;
   import android.os.Bundle;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  import android.widget.RelativeLayout.LayoutParams;


  public class MainActivity extends Activity {
  ImageView background;
  Bitmap transfered;
   FrameLayout.LayoutParams layoutParams;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  background=(ImageView)findViewById(R.id.imageView1);

 layoutParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
 Bundle extras = getIntent().getExtras();

    String picturePath=extras.getString("path");
     transfered=BitmapFactory.decodeFile(picturePath);  
 background.setImageBitmap(transfered); 
 background.setAdjustViewBounds(true);
 background.setLayoutParams(layoutParams);
}

  }

我的第二个活动xml是

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.progressbarcircle.MainActivity"
  tools:ignore="MergeRootFrame" >

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_gravity="center" />

  </FrameLayout>

提前致谢。请帮助我。

1 个答案:

答案 0 :(得分:0)

如果您的第一个Activity花了太多时间,请在

之后创建一个ProgressDialog对象和show()
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)    {
  Uri selectedImage = data.getData();          
dismiss()

之前

startActivity(intent);

如果花费的时间过长,您可以对第二个Activity执行相同操作。