我想在图库加载时加载图像,而不是在用户滚动之后,想要显示加载指示符,而不是在显示新图像之后。 在我的下面的代码中,它采取所有数据(包括图像)和显示。
这是我的代码:
ProductDetailsActivity.java
public class ProductsDetailActivity extends BaseActivity implements
OnClickListener, OnItemSelectedListener {
Gallery gallery;
TextView prod_descri, productTitle1, productTitle2;
ImageAdapter imageAdapter;
ImageView leftArrow, rightArrow;
private int selectedImagePosition = 0;
private List<Drawable> drawables;
String title, url, title1;
Bundle b;
Products product;
ProductDetails pDetails;
ArrayList<ProductDetails> productList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.products_details);
init();
setTitle(getResources().getString(R.string.title_product));
b = getIntent().getExtras();
product = (Products) b.getSerializable("product");
if (b != null) {
if (Global.isNetworkAvailable(ProductsDetailActivity.this)) {
new ProductAsyncTask().execute(Constants.url,
product.product_id);
} else {
Intent intent = new Intent(ProductsDetailActivity.this,
OfflineActivity.class);
startActivity(intent);
}
} else {
Log.v("Bundle", "Bundle null");
}
}
private void init() {
productList = new ArrayList<ProductDetails>();
gallery = (Gallery) findViewById(R.id.product_gallery);
prod_descri = (TextView) findViewById(R.id.product_desc);
productTitle1 = (TextView) findViewById(R.id.productTitle1);
productTitle2 = (TextView) findViewById(R.id.productTitle2);
leftArrow = (ImageView) findViewById(R.id.left_arrow_imageview);
rightArrow = (ImageView) findViewById(R.id.right_arrow_imageview);
leftArrow.setOnClickListener(this);
rightArrow.setOnClickListener(this);
gallery.setOnItemSelectedListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_arrow_imageview:
if (selectedImagePosition > 0) {
// leftArrow.setVisibility(View.VISIBLE);
--selectedImagePosition;
} else if (selectedImagePosition == 0) {
leftArrow.setVisibility(View.INVISIBLE);
rightArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
case R.id.right_arrow_imageview:
if (selectedImagePosition < drawables.size() - 1) {
// rightArrow.setVisibility(View.VISIBLE);
++selectedImagePosition;
} else if (selectedImagePosition == drawables.size() - 1) {
rightArrow.setVisibility(View.INVISIBLE);
leftArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
default:
break;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
selectedImagePosition = position;
productTitle1.setText(product.title);
productTitle2.setText(product.title);
prod_descri.setText(product.long_description);
if (selectedImagePosition > 0
&& selectedImagePosition < drawables.size() - 1) {
leftArrow.setVisibility(View.VISIBLE);
rightArrow.setVisibility(View.VISIBLE);
} else if (selectedImagePosition == 0 && drawables.size() > 1) {
leftArrow.setVisibility(View.INVISIBLE);
rightArrow.setVisibility(View.VISIBLE);
} else if (selectedImagePosition == drawables.size() - 1
&& drawables.size() > 1) {
rightArrow.setVisibility(View.INVISIBLE);
leftArrow.setVisibility(View.VISIBLE);
} else if (drawables.size() == 1) {
rightArrow.setVisibility(View.INVISIBLE);
leftArrow.setVisibility(View.INVISIBLE);
}
// setSelectedImage(selectedImagePosition);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
// AsyncTask
public class ProductAsyncTask extends
AsyncTask<String, String, ArrayList<ProductDetails>> {
private Context getDialogContext() {
Context context;
if (getParent() != null)
context = getParent();
else
context = ProductsDetailActivity.this;
return context;
}
String responseString = null;
ProgressDialog progressDialog;
public ProductAsyncTask() {
super();
// progressDialog = new ProgressDialog(ProductsDetailActivity.this);
progressDialog = new ProgressDialog(getDialogContext());
progressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected ArrayList<ProductDetails> doInBackground(String... params) {
responseString = readJSONSFeed(params[0], params[1]);
if (responseString != null) {
return processProductDetailsJSON(responseString);
} else {
return null;
}
}
@Override
protected void onPostExecute(ArrayList<ProductDetails> result) {
super.onPostExecute(result);
try {
if (result.size() == 0 || result == null) {
}
progressDialog.dismiss();
drawables = createDrawables(result);
imageAdapter = new ImageAdapter(ProductsDetailActivity.this,
drawables);
if (drawables.size() > 0) {
gallery.setSelection(selectedImagePosition, true);
}
if (drawables.size() == 1) {
leftArrow.setVisibility(View.INVISIBLE);
rightArrow.setVisibility(View.INVISIBLE);
}
gallery.setAdapter(imageAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
private List<Drawable> createDrawables(ArrayList<ProductDetails> result) {
List<Drawable> drawablesUrl = new ArrayList<Drawable>();
try {
for (ProductDetails objProDetails : result) {
drawablesUrl.add(Global.getBitmapFromURL(
ProductsDetailActivity.this,
objProDetails.image_path));
// drawablesUrl.add(loadImageFromURL(objProDetails.image_path));
}
return drawablesUrl;
} catch (Exception e) {
return null;
}
}
@SuppressWarnings("unused")
private Drawable loadImageFromURL(String image_path) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return getResources().getDrawable(R.drawable.noimage);
}
}
private String readJSONSFeed(String url, String productID) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_PRODUCT_IMAGE
+ productID + Constants.REQ_FOR_OFFER);
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("password", "password");
httpGet.setHeader("username", "user");
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statuscode = statusLine.getStatusCode();
if (statuscode == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(inputStream));
String line = null;
if ((line = buffer.readLine()) != null) {
stringBuilder.append(line);
}
// buffer.close();
inputStream.close();
} else {
}
} catch (Exception e) {
}
return stringBuilder.toString();
}
private synchronized ArrayList<ProductDetails> processProductDetailsJSON(
String response) {
try {
JSONObject json = new JSONObject(response);
JSONArray Records = json
.getJSONArray(Constants.JSON_OBJECT_DATA);
for (int i = 0; i < Records.length(); i++) {
JSONObject Record = Records.getJSONObject(i);
String tTitle = Record.getString("title");
String tImagePath = Record.getString("image_path");
String tDescription = Record.getString("description");
tImagePath = Constants.REQ_IMAGE_PRODUCT + tImagePath;
pDetails = new ProductDetails(tDescription, tTitle,
tImagePath);
if (pDetails == null) {
} else {
productList.add(pDetails);
}
}
} catch (JSONException ex) {
System.out.println(ex);
return null;
}
return productList;
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Activity context;
ImageView imageView;
private List<Drawable> totalImages;
private static ViewHolder holder;
ArrayList<Object> imageGallery;
int width;
int height;
Display display;
int[] images = { R.drawable.ic_launcher, R.drawable.ic_launcher };
public ImageAdapter(Context c, List<Drawable> drawables) {
this.context = (Activity) c;
imageGallery = new ArrayList<Object>();
this.totalImages = drawables;
display = context.getWindowManager().getDefaultDisplay();
// this.imageGallery = objects;
// width = display.getWidth();
// height = display.getHeight();
}
@Override
public int getCount() {
return totalImages.size();
// return 1;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
holder = new ViewHolder();
imageView = new ImageView(this.context);
convertView = imageView;
holder.imageView = imageView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageDrawable(totalImages.get(position));
holder.imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
} catch (Exception e) {
e.printStackTrace();
return imageView;
}
}
static class ViewHolder {
public ViewHolder() {
}
ImageView imageView;
}
}
Here is my CustomArrayAdapter
CustomArrayAdapter.java
public class CustomArrayAdapter extends ArrayAdapter<Object> {
Context mContext;
ArrayList<Products> product;
int layoutId;
ImageLoader imageLoader;
public CustomArrayAdapter(Context context, int textViewResourceId,
ArrayList<Products> objects) {
super(context, textViewResourceId);
this.mContext = context;
this.layoutId = textViewResourceId;
this.product = objects;
imageLoader = new ImageLoader(context, R.drawable.noimage);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(layoutId, parent, false);
holder = new ViewHolder(convertView);
holder.setValues(product.get(position));
return convertView;
}
public int getCount() {
return product.size();
}
public class ViewHolder {
TextView tvProductTitle;
TextView tvProductDescription;
ImageView ivProductLogo, imgArrow;
public ProgressBar progress;
public ViewHolder(View v) {
tvProductTitle = (TextView) v.findViewById(R.id.tvTitle);
tvProductDescription = (TextView) v
.findViewById(R.id.tvDescriptions);
ivProductLogo = (ImageView) v.findViewById(R.id.ivLogoProduct);
imgArrow = (ImageView) v.findViewById(R.id.imgArrow);
progress = (ProgressBar) v.findViewById(R.id.progress_bar);
}
public void setValues(Products products) {
tvProductTitle.setText(products.title);
tvProductDescription.setText(products.short_description);
// imageLoader.displayImage(products.image, imageView, options);
try {
ImageLoader imageLoader = new ImageLoader(mContext);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisc().build();
imageLoader.DisplayImage(products.image, ivProductLogo);
if (Global.fromProduct) {
imgArrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cell_arrow_right));
} else {
imgArrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.info_btn));
}
} catch (Exception e) {
e.printStackTrace();
}
// try {
}
@SuppressWarnings("unused")
private boolean cancelPotentialDownload(String url, ImageView imageView) {
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
if (bitmapDownloaderTask != null) {
String bitmapUrl = bitmapDownloaderTask.url;
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
bitmapDownloaderTask.cancel(true);
} else {
// The same URL is already being downloaded.
return false;
}
}
return true;
}
private BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof DownloadedDrawable) {
DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
}
}'
答案 0 :(得分:2)
从https://github.com/nostra13/Android-Universal-Image-Loader下载通用图片加载器。
将jar文件添加到项目的libs文件夹中
注意:通过从互联网下载图像显示以下内容。您还可以从SD卡文件夹中提供图像路径。
Universal Image Loader是Lazy Loading的改进版本。异步加载图像。如果您正在从互联网下载图像
,则可以在SD卡或手机记忆库中缓存图像main.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">
<GridView
android:id="@+id/PhoneImageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
row_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivv"
android:layout_gravity="center"
android:layout_width="300dp"
android:layout_height="300dp"
/>
<ProgressBar
android:id="@+id/pb"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
AndroidCustomGalleryActivity.java
public class AndroidCustomGalleryActivity extends Activity {
private ImageAdapter imageAdapter;
private String[] mStrings={
"http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
"http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook.png",
"http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
"http://a1.twimg.com/profile_images/74724754/android_logo_normal.png",
"http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
"http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
"http://a3.twimg.com/profile_images/548410609/icon_8_73.png",
"http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
"http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
"http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/121630227/Droid.jpg",
"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
"http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter_normal.png",
"http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
"http://a1.twimg.com/profile_images/97470808/icon.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
"http://a1.twimg.com/profile_images/74724754/android_logo.png",
"http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
"http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
"http://a3.twimg.com/profile_images/548410609/icon_8_73_normal.png",
"http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
"http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
"http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto.jpg",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
"http://a1.twimg.com/profile_images/957149154/twitterhalf.jpg",
"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
ImageLoader imageLoader;
DisplayImageOptions options;
public ImageAdapter() {
//Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(AndroidCustomGalleryActivity.this)
// You can pass your own memory cache implementation
.discCacheExtraOptions(1024, 1024, CompressFormat.PNG, 100)
// You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_launcher)
.resetViewBeforeLoading()
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(300))
.build();
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return mStrings.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.rowimage, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.ivv);
holder.pb = (ProgressBar) convertView.findViewById(R.id.pb);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
display(holder.imageview, mStrings[position].toString(), holder.pb);
// imageLoader.displayImage(data.get(position).toString(), holder.imageview,options);
return convertView;
}
public void display(ImageView img, String url, final ProgressBar spinner)
{
imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
}
class ViewHolder {
ImageView imageview;
ProgressBar pb;
}
}
答案 1 :(得分:0)
我认为你应该使用线程看起来如下:
logoImageView.setImageDrawable(R.id.drawable.company_logo);
Thread.sleep(1000); // sleep for a second (1000 ms)
nameImageView.setImageDrawable(R.id.drawable.company_name);
Thread.sleep(1000);
sloganImageView.setImageDrawable(R.id.drawable.company_slogan);
它只是为了了解你。