【问题标题】:Android: Image gallery with preview left and right and landscape and portrait imagesAndroid:具有左右预览以及横向和纵向图像的图片库
【发布时间】:2014-07-11 21:29:24
【问题描述】:

我目前正在努力向用户展示我的活动中的图片库。此图片库必须显示带有左右预览的图像。此外,我必须能够放入具有纵向或横向方向的图像。

我对此进行了一些研究,发现了三种方法。

选项 1: Viewpager

viewpager 似乎很容易实现,加上负边距在:viewpager.setPageMargin(int) 方法我可以实现左右图像的预览。

问题

我似乎无法设置视图的单独宽度,因此当我在 ViewPager 中有两个彼此相邻的纵向图像时,我的图像之间没有巨大的空白。

选项 2: Horizo​​ntalScrollView

很棒的组件。喜欢我手机底部的滚动条。正确实施后,向用户显示图库中有多少张图片。

问题

我必须自己编写捕捉逻辑。有一些示例可以为我提供捕捉逻辑,但没有一个示例通过预览上一张和下一张图像来实现任何东西。现在我可以在这方面投入相当多的时间,但这不是我期待做的事情。

选项 3: HorizontalPager

我喜欢视图的简单性,并且我可以将我的 ImageViews 推送到这个组件,但是当它必须处理多个 ImageViews 时,这个组件会保持多快?

问题

在 ImageViews 之间添加填充时,Horizo​​ntalPager 会滞后。使用填充或边距时,偏移量也不正确。

最后两个似乎使用 ImageViews 的 MeasuredWidth,我想知道它们是否包括填充或边距。无论哪种方式,对你们来说,什么似乎是最好的解决方案,什么是最容易修改的?

你们有这方面的经验吗?

感谢您的帮助。

【问题讨论】:

    标签: android


    【解决方案1】:

    我终于弄清楚了我想要它的方式。我终于在使用 ViewPager 时成功地做到了这一点:

    画廊适配器:

    public class GalleryAdapter extends PagerAdapter {
    
    /**
     * The collection of images contained in the view pager.
     */
    public Integer[] mImages;
    
    private InternalDatabase dbhelper;
    private RuntimeExceptionDao<GalleryImage, Integer> imagesDao;
    
    /**
     * Constructor for the adapter. Initializes the images needed.
     */
    public GalleryAdapter(Context context, Integer[] galleryImages) {
    
        dbhelper = OpenHelperManager.getHelper(context, InternalDatabase.class);
        imagesDao = dbhelper.getRuntimeExceptionDao(GalleryImage.class);
    
        // TODO: create the proper collection to be able to display the images in the collection.
        mImages = galleryImages;
    
    }
    
    @Override
    public int getCount() {
        if (mImages != null) {
            if (mImages.length > 0) {
                return mImages.length;
            }
        }
        return 0;
    }
    
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }
    
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    
        // Get the context from which the adapter is created.
        // Need the context to create new views.
        Context context = container.getContext();
    
        // Create a new image view that will contain the image.
        ImageView imageView = new ImageView(context);
    
        LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(params);
    
        // Scaling the image inside the wrapping view pager.
        //imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    
        // Setting the image.
        if (mImages != null) {
    
            if (mImages.length > 0) {
    
                Integer id = mImages[position];
                GalleryImage image = imagesDao.queryForId(id);
    
                Bitmap decodedByte = BitmapFactory.decodeByteArray(image.getImage(), 0, image.getImage().length); 
                imageView.setImageBitmap(decodedByte);
    
            }
    
        }
    
        // Add the image to the view pager.
        ((ViewPager) container).addView(imageView);
        return imageView;
    }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }
    }
    

    线性布局视图:

    public class CardMiniatureGallery extends AbstractCompoundLinearLayout {
    
    /**
     * The viewpager that is used as a miniature gallery.
     */
    public ViewPager mGallery;
    
    private Integer[] mIDs;
    
    public CardMiniatureGallery(Context context) {
        super(context);
    }
    
    public CardMiniatureGallery(Context context, Integer[] images) {
        super(context);
        mIDs = images;
    
        createLayout(context, mIDs);
    }
    
    public CardMiniatureGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        createLayout(context, mIDs);
    }
    
    public CardMiniatureGallery(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        createLayout(context, mIDs);
    }
    
    protected void createLayout(Context context) {
    
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.card_miniature_gallery, this, true);
    
    }
    
    private void createLayout(Context context, Integer[] ids) {
    
        mGallery = (ViewPager) getChildAt(0);
    
        GalleryAdapter adapter = new GalleryAdapter(context, mIDs);
        mGallery.setAdapter(adapter);
        mGallery.setOffscreenPageLimit(adapter.getCount());
        mGallery.setClipToPadding(false);
    
        int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 43, getResources().getDisplayMetrics());
        mGallery.setPageMargin(margin);
    
    }
    
    }
    

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:overScrollMode="never"
        android:paddingLeft="85dp"
        android:paddingRight="85dp"
        android:layout_marginBottom="@dimen/padding_medium" />
    

    有了这些类和我用来从数据库中获取数据的整数数组,我可以根据需要显示图像。现在我只需要弄清楚如何通过查看它必须加载的图像来动态设置 ViewPager 高度。

    【讨论】:

      猜你喜欢
      • 2014-08-02
      • 2014-02-07
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多