我在我的应用程序中使用ViewPager,所有图像都来自服务器。一切正常,但图片花了很多时间在屏幕上显示,任何人都可以告诉我们是什么问题,以下是我的代码片段。或者有没有有效的方法来加载图像?
public class FullImage extends Activity{
private String strtd;
String[] imgStr;
ImageView imageView;
ArrayList<String> userImgArrayList;
String[] myURLs;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image_view);
//strtd = getIntent().getStringExtra("profile_pic");
userImgArrayList = getIntent().getStringArrayListExtra("user_images");
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
imageView = (ImageView) findViewById(R.id.full_image_view);
}
public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth,
int bitmapHeight) {
return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
true);
}
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 0) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
public class ImageAdapter extends PagerAdapter {
Context context;
ImageAdapter(Context context)
{
this.context=context;
}
@Override
public int getCount() {
return userImgArrayList.size();
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.activity_horizontal_margin);
imageView.setPadding(padding, padding, padding, padding);
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Picasso.with(context).load(userImgArrayList.get(position)).into(imageView);
/*for(int i=0; i<myURLs.length;i++)
{
try {
url = new URL(myURLs[i]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bmp);
}
*/
// imageView.setImageURI(Uri.parse(imgStr[position]));
((ViewPager) container).addView(imageView, 0);
return imageView;
}
}
答案 0 :(得分:0)
我不知道调整大小的图像是否会解决您的问题无论如何都要尝试
x,y是图像所需的宽度和高度
Picasso.with(context).load(userImgArrayList.get(position)).resize(x, y) .into(imageView);
据我说,通用图像加载器要好得多,设置通用图像加载器有点痛苦,但我认为它有更好的图像缓存设施 您可以在此处查看示例Universal image loader sample
如果我说您可以在服务器端调整图像大小,那么将用户上传的所有图像调整为大小而不会丢失宽高比,这样大多数图像都会加载得更顺畅
答案 1 :(得分:0)
请浏览此Google的官方信息,可能会对您有所帮助:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
这是另一个很好解释的问题:Android - Efficient way to load multiple images from remote server
很抱歉有很多链接,但这些链接有足够的信息:http://android-vogue.blogspot.jp/2013/01/loading-large-bitmaps-efficiently-in.html