2

请帮我。我正在关注 Chris Banes ( https://github.com/chrisbanes/PhotoView ) 的示例 PhotoView 中的示例 ViewPagerActivity ,我想使用 Picasso URL 图像更改可绘制对象。所以,我试图通过 Picasso String 的 url 数组将多个图像放入 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());

我有一个从单个 String stringDefinitionGemstonesPhoto 获得的 URL 字符串数组

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;
    }

}

仅显示一张图像,当我滑动时,下一张照片为空/空白。在日志系统中打印出正确拆分的链接。怎么了?

4

2 回答 2

2

我看到了一些错误,那就是

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()查看更多

所以我的解决方案不再使用上面的lib,我们尝试手动请求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);
        }
    }
}
于 2018-12-11T07:06:15.227 回答
2

无需单独使用数字数组,更新适配器类

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;
    }

}
于 2018-12-11T07:11:03.957 回答