Мне нужно реализовать индикатор выполнения в проекте Android, когда намерение передается через одно действие, другое, и во втором упражнении загружается довольно много данных из Интернета, и поэтому существует заметная задержка между тем, когдапользователь нажимает на элемент, и когда отображается действие.
Я пробовал несколько разных подходов для этого, но, похоже, ничего не работает так, как хотелось бы.Я использую следующий код для этого.И диалог прогресса идет внутри бесконечного цикла.
public class BackgroundAsyncTask extends AsyncTask<String, Integer, Bitmap> {
int myProgress;
@Override
protected void onPostExecute(Bitmap result) {
iv.setVisibility(View.VISIBLE);
iv.setImageBitmap(result);
dialog.cancel();
dialog.dismiss();
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(ProfileNormalUserPhotos.this, "Loading...", "Please wait...");
}
@Override
protected Bitmap doInBackground(String...paths) {
return DownloadFile(imageUrl);
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
progressBar.setProgress(values[0]);
}
}
public Bitmap DownloadFile(String url){
URL myFileUrl;
Bitmap bitmap=null;
try {
myFileUrl = new URL(imageUrl);
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
bitmap = BitmapFactory.decodeStream((InputStream) new URL(
imageUrl).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap,70 , 70, true);
System.out.println("name of bitmap"+bitmap.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e);
}
return bitmap;
}
И вот мой класс адаптера
public class ImageAdapter extends BaseAdapter {
Context mContext;
private String[] stringOnTextView;
public ImageAdapter(Context c) {
mContext = c;
}
// BitmapManager.INSTANCE. setPlaceholder(BitmapFactory.decodeResource(
// context.getResources(), R.drawable.icon));
public ImageAdapter(Context Context,
String[] stringOnTextView) {
this.mContext=Context;
this.stringOnTextView=stringOnTextView;
}
public int getCount() {
return stringOnTextView.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("**************"+position);
View v = null;
if(convertView==null){
try{
{
System.gc();
// dialog = ProgressDialog.show(ProfileNormalUserPhotos.this, "Loading...", "Please wait...");
imageUrl = "http://ondamove.it/English/images/users/";
imageUrl=imageUrl+stringOnTextView[position];
System.out.println(imageUrl);
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
TextView tv = (TextView)v.findViewById(R.id.text);
tv.setText("Profile Image "+(position+1));
/* URL myFileUrl = new URL(imageUrl);
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
imageUrl).getContent());
bitmap = Bitmap.createScaledBitmap(bitmap, 80, 80, true);*/
// new ImageView(mContext);
iv= (ImageView)v.findViewById(R.id.image);
new BackgroundAsyncTask().execute(imageUrl);
//iv.setImageBitmap(bitmap);
//dialog.cancel();
// System.out.println(bitmap.getHeight());
System.out.println("++++++++"+R.id.image);
}
}catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
else
{
try{
v = convertView;}
catch(Exception e){
System.out.println(e);
}
}
return v;
}
}
Может кто-нибудь помочь мне в этом?