在我的应用程序中,我正在从服务器下载图像并将图像设置为查看寻呼机。一切正常,但在下载图像时有一点延迟(比如Wifi上8-10秒)。图像大小范围为200kb-800kb。我在下载过程中也添加了一个进度条,但进度条没有显示。 有人可以帮我这个。 以下是代码。
public void fetchImageUrl() {
progressBar.setVisibility(View.VISIBLE);
String url = URLMap.getUploadImgChangeStatus("get_amazonserver_image");
url = url.replace("{id}", companyId).replace("{crid}", callRecordId);
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("Response0000" + response);
if (response.equals("[]")) {
setView();
} else {
getAmazonImageUrl(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("ErrorListner=", error.getMessage());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
public void getAmazonImageUrl(String response) {
try {
imgUrl = new ArrayList<>();
String[] arr;
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
if (jArray.getJSONObject(i).has("ImagePath")) {
arr = jArray.getJSONObject(i).getString("ImagePath").split("\\s+");
imgUrl = Arrays.asList(arr);
}
}
} catch (JSONException e) {
}
imageViewAdapter = new ImageViewAdapter(this, iv, imgUrl, viewPager, this);
viewPager.setAdapter(imageViewAdapter);
progressBar.setVisibility(View.GONE);
}
以下是我的适配器类
public class ImageViewAdapter extends android.support.v4.view.PagerAdapter {
private Context context;
ImageView imageView;
ProgressBar progressBar;
private LayoutInflater layoutInflater;
ViewPager viewPager;
Activity activity;
List<String> imgUrl;
public ImageViewAdapter(Context context, ImageView imageView, List<String> imgUrl,
ViewPager viewPager, Activity activity){
System.out.println("Inside ImageViewAdapter");
this.context=context;
this.imageView = imageView;
this.viewPager = viewPager;
this.activity = activity;
this.imgUrl=imgUrl;
System.out.println(imgUrl.size());
}
@Override
public int getCount() {
return imgUrl.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View item_view = layoutInflater.inflate(R.layout.image_swipe_custom_layout, container, false);
//setting custom font
Typeface custom_font = Typeface.createFromAsset(context.getAssets(),"fonts/DroidSerif.ttf");
TextView tv = (TextView) item_view.findViewById(R.id.image_count);
tv.setTypeface(custom_font);
imageView = (ImageView) item_view.findViewById(R.id.imageView1);
progressBar=(ProgressBar)item_view.findViewById(R.id.imgLoadingPbar);
// UNIVERSAL IMAGE LOADER SETUP
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);
// UNIVERSAL IMAGE LOADER OPTION SETUP
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.ic_user)
.showImageOnFail(R.drawable.noimgavailable)
.showImageOnLoading(R.drawable.spinner_border).build();
/*final Animation animation= AnimationUtils.loadAnimation(context,R.anim.rotate);*/
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
imageLoader.displayImage(imgUrl.get(position), imageView, options, new ImageLoadingListener() {
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
if(position == 0) {
tv.setText("Image " + "1");
tv.setTypeface(custom_font);
}else if(position == 1) {
tv.setText("Image " + "2");
tv.setTypeface(custom_font);
} else if(position == 2) {
tv.setText("Image " + "3");
tv.setTypeface(custom_font);
}
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//((ViewPager)container).removeView((View)object);
container.removeView((LinearLayout) object);
}
}
我尝试过使用毕加索,异步任务,但一切都是一样的..毕加索和通用图像加载器几乎需要相同的时间,但异步任务更晚。 在活动类中设置的进度条显示一秒钟或两秒钟。但是当它转到适配器类时,我有另一个没有显示的进度条。 我的问题是 1.有什么方法可以更快地下载图像 2.如何正确使用进度条。 提前致谢
答案 0 :(得分:2)
您可以在特定位置添加进度条。示例代码在下方。如果有任何问题,请点击此链接Universal Image Loader description
imageLoader.displayImage(imageUri, imageView, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
...
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
...
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
...
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
...
}
},new ImageLoadingProgressListener(){ @覆盖 public void onProgressUpdate(String imageUri,View view,int current,int total){ ... } });
答案 1 :(得分:0)
好的,我得到了解决方案。除了进度条,我们还需要设置视图。 我发布了代码。
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
imageLoader.displayImage(imgUrl.get(position), imageView, options, new ImageLoadingListener() {
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingStarted(String imageUri, View view) {
//progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});