ViewPager + Picasso + 来自 URL 字符串数组的多张图片
ViewPager + Picasso + multiple images from URL String array
请帮助我。我正在关注 Chris Banes (https://github.com/chrisbanes/PhotoView) 示例 PhotoView 中的示例 ViewPagerActivity,我想用 Picasso URL 图像更改可绘制对象。因此,我正在尝试通过 Picasso 字符串数组将多张图片放入 HackyViewPager。
我的 xml ViewPager:
<my.package.HackyViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp" />
在 onCreate 中:
...
ViewPager viewPager = (HackyViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new SamplePagerAdapter());
我有一个 URL 的字符串数组,是从一个字符串 stringDefinitionGemstonesPhoto
中获得的
String[] strAry = stringDefinitionGemstonesPhoto.split(",");
SamplePageAdapter 中的最终代码是:
static class SamplePagerAdapter extends PagerAdapter {
private String stringDefinitionGemstonesPhoto = ("https://geology.com/minerals/photos/calcite-marble-127.jpg, https://geology.com/minerals/photos/calcite-concrete.jpg");
private String[] strAry = stringDefinitionGemstonesPhoto.split(",");
@Override
public int getCount() {
return strAry.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
只显示一张图片,滑动时下一张照片是empty/blank。在日志系统中打印出正确拆分的链接。
怎么了?
我看错了,就是
for(int i= 0; i < numbers.length; i++){
System.out.println(strAry[i]);
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
}
应该是
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
更新
我发现 Picasso
或 Glide
在 instantiateItem()
、see more
中无法正常工作的问题
所以我的解决方案是不再使用上面的库,我们尝试手动请求url,这种方式不是更好,因为性能问题,但它是有效的。
这是我的解决方案
class SamplePagerAdapter extends PagerAdapter {
private String stringDefinitionGemstonesPhoto = ("https://geology.com/minerals/photos/calcite-marble-127.jpg, https://geology.com/minerals/photos/calcite-concrete.jpg");
private String[] strAry = stringDefinitionGemstonesPhoto.split(",");
@Override
public int getCount() {
return strAry.length;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
try {
new DownloadImageTask(photoView)
.execute(strAry[position]);
} catch (Exception ex) {
Log.e("Error", ex.getMessage());
}
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView bmImage;
DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urlDisplay = urls[0];
Bitmap myImage = null;
try {
InputStream in = new java.net.URL(urlDisplay).openStream();
myImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return myImage;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
无需单独使用数字数组,更新适配器 class
static class SamplePagerAdapter extends PagerAdapter {
private static String[] strAry = stringDefinitionGemstonesPhoto.split(",");
@Override
public int getCount() {
return strAry.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
请帮助我。我正在关注 Chris Banes (https://github.com/chrisbanes/PhotoView) 示例 PhotoView 中的示例 ViewPagerActivity,我想用 Picasso URL 图像更改可绘制对象。因此,我正在尝试通过 Picasso 字符串数组将多张图片放入 HackyViewPager。
我的 xml ViewPager:
<my.package.HackyViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp" />
在 onCreate 中:
...
ViewPager viewPager = (HackyViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new SamplePagerAdapter());
我有一个 URL 的字符串数组,是从一个字符串 stringDefinitionGemstonesPhoto
中获得的String[] strAry = stringDefinitionGemstonesPhoto.split(",");
SamplePageAdapter 中的最终代码是:
static class SamplePagerAdapter extends PagerAdapter {
private String stringDefinitionGemstonesPhoto = ("https://geology.com/minerals/photos/calcite-marble-127.jpg, https://geology.com/minerals/photos/calcite-concrete.jpg");
private String[] strAry = stringDefinitionGemstonesPhoto.split(",");
@Override
public int getCount() {
return strAry.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
只显示一张图片,滑动时下一张照片是empty/blank。在日志系统中打印出正确拆分的链接。 怎么了?
我看错了,就是
for(int i= 0; i < numbers.length; i++){
System.out.println(strAry[i]);
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
}
应该是
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
更新
我发现 Picasso
或 Glide
在 instantiateItem()
、see more
所以我的解决方案是不再使用上面的库,我们尝试手动请求url,这种方式不是更好,因为性能问题,但它是有效的。
这是我的解决方案
class SamplePagerAdapter extends PagerAdapter {
private String stringDefinitionGemstonesPhoto = ("https://geology.com/minerals/photos/calcite-marble-127.jpg, https://geology.com/minerals/photos/calcite-concrete.jpg");
private String[] strAry = stringDefinitionGemstonesPhoto.split(",");
@Override
public int getCount() {
return strAry.length;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
try {
new DownloadImageTask(photoView)
.execute(strAry[position]);
} catch (Exception ex) {
Log.e("Error", ex.getMessage());
}
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView bmImage;
DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urlDisplay = urls[0];
Bitmap myImage = null;
try {
InputStream in = new java.net.URL(urlDisplay).openStream();
myImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return myImage;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
无需单独使用数字数组,更新适配器 class
static class SamplePagerAdapter extends PagerAdapter {
private static String[] strAry = stringDefinitionGemstonesPhoto.split(",");
@Override
public int getCount() {
return strAry.length;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
System.out.println(strAry[position]);
Picasso.get().load(strAry[position]).into(photoView);
container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
return photoView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}