【问题标题】:Android Horizontal scrolling image galleryAndroid 水平滚动图片库
【发布时间】:2015-06-08 09:19:03
【问题描述】:

我想创建带有水平图片库的应用程序(一行和多列)。 首先我尝试使用gridview,但它只能用作垂直滚动。 我可以为此目的使用ListView 或GridView 吗?

【问题讨论】:

标签: android listview gridview


【解决方案1】:

使用 Horizo​​ntalScrollView 而不是 ListView 或 GirdView http://developer.android.com/reference/android/widget/HorizontalScrollView.html

【讨论】:

    【解决方案2】:

    TwoWayView 对我很有效。 https://github.com/lucasr/twoway-view/

    【讨论】:

      【解决方案3】:

      HorizontalScrollView Documentation

      要使用 Horizo​​ntalScrollView,您应该只有一个孩子。我之前使用它的方法是创建一个 TableLayout 并将图像添加到 TableRows 中。 TableLayout 可以有很多行或只有 1 行和很多列。

      您可以将 ImageViews(或任何其他视图)添加到每一行,然后最后将 TableRow 添加到 TableLayout。完成 TableLayout 后,您需要做的就是:

          //createTable method is where you would loop through the images to add
          TableLayout table = createTable(rowCount, columnCount);
          HorizontalScrollView hozView = new HorizontalScrollView(this);
          hozView.addView(table);
          setContentView(hozView);
      

      【讨论】:

        【解决方案4】:

        在 Horizo​​ntalScrollView 中创建 LinearLayout,然后动态创建 imageView 并将该 imageview 添加到 linearLayout。

        示例代码:

        <HorizontalScrollView 
        android:id="@+id/horizontal_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        
            <LinearLayout
            android:id="@+id/linear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            </LinearLayout>
        
        </HorizontalScrollView>
        

        在onCreate()方法中,从xml文件中获取linearLayout的id,并将动态创建的ImageView添加到linearlayout中:

            LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
            for (int i = 0; i < 10; i++) {
                ImageView imageView = new ImageView(this);
                imageView.setId(i);
                imageView.setPadding(2, 2, 2, 2);
                imageView.setImageBitmap(BitmapFactory.decodeResource(
                        getResources(), R.drawable.ic_launcher));
                imageView.setScaleType(ScaleType.FIT_XY);
                layout.addView(imageView);
            }
        

        【讨论】:

        • 我有不同纵横比的不同图片。我希望它们都填充线性布局的高度,然后填充整个 Horizo​​ntalScrollView。我应该如何使用您的解决方案来解决这个问题?
        • 这很棒。你如何使用这种方法进行延迟加载?
        • 工作谢谢,但我想从 arraylist 加载不同的图像。如何实现这一点。
        【解决方案5】:

        由于我们no longer have Gallery 小部件需要一点 DIY 噪音。您可以使用HorizontalScrollView 和嵌套的LinearLayout 创建一个水平滚动的缩略图条,并从您的活动中动态添加图像:

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="72dp"
            android:layout_gravity="bottom"
            android:background="@color/black">
            <LinearLayout
                android:id="@+id/thumbnails"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:paddingTop="2dp"/>
        </HorizontalScrollView>
        

        以下代码取自本教程:http://sourcey.com/android-horizontally-scrolling-pan-scan-and-zoom-image-gallery/ GalleryActivity 实现了您正在寻找的内容,并通过添加用于显示所选图像的ViewPager 元素和SubsamplingScaleImageView 来扩展您的请求,以便您可以平移、扫描和缩放所选图像:

        package com.sourcey.imagegallerydemo;
        
        import android.content.Context;
        import android.graphics.Bitmap;
        import android.os.Bundle;
        import android.support.v4.view.PagerAdapter;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v4.view.ViewPager;
        import android.util.Log;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.FrameLayout;
        import android.widget.ImageButton;
        import android.widget.ImageView;
        import android.widget.LinearLayout;
        
        import com.bumptech.glide.Glide;
        import com.bumptech.glide.request.animation.GlideAnimation;
        import com.bumptech.glide.request.target.SimpleTarget;
        import com.davemorrissey.labs.subscaleview.ImageSource;
        import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
        
        import junit.framework.Assert;
        
        import java.util.ArrayList;
        
        import butterknife.ButterKnife;
        import butterknife.InjectView;
        
        public class GalleryActivity extends AppCompatActivity {
            public static final String TAG = "GalleryActivity";
            public static final String EXTRA_NAME = "images";
        
            private ArrayList<String> _images;
            private GalleryPagerAdapter _adapter;
        
            @InjectView(R.id.pager) ViewPager _pager;
            @InjectView(R.id.thumbnails) LinearLayout _thumbnails;
            @InjectView(R.id.btn_close) ImageButton _closeButton;
        
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_gallery);
                ButterKnife.inject(this);
        
                _images = (ArrayList<String>) getIntent().getSerializableExtra(EXTRA_NAME);
                Assert.assertNotNull(_images);
        
                _adapter = new GalleryPagerAdapter(this);
                _pager.setAdapter(_adapter);
                _pager.setOffscreenPageLimit(6); // how many images to load into memory
        
                _closeButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "Close clicked");
                        finish();
                    }
                });
            }
        
            class GalleryPagerAdapter extends PagerAdapter {
        
                Context _context;
                LayoutInflater _inflater;
        
                public GalleryPagerAdapter(Context context) {
                    _context = context;
                    _inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }
        
                @Override
                public int getCount() {
                    return _images.size();
                }
        
                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view == ((LinearLayout) object);
                }
        
                @Override
                public Object instantiateItem(ViewGroup container, final int position) {
                    View itemView = _inflater.inflate(R.layout.pager_gallery_item, container, false);
                    container.addView(itemView);
        
                    // Get the border size to show around each image
                    int borderSize = _thumbnails.getPaddingTop();
        
                    // Get the size of the actual thumbnail image
                    int thumbnailSize = ((FrameLayout.LayoutParams)
                            _pager.getLayoutParams()).bottomMargin - (borderSize*2);
        
                    // Set the thumbnail layout parameters. Adjust as required
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(thumbnailSize, thumbnailSize);
                    params.setMargins(0, 0, borderSize, 0);
        
                    // You could also set like so to remove borders
                    //ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                    //        ViewGroup.LayoutParams.WRAP_CONTENT,
                    //        ViewGroup.LayoutParams.WRAP_CONTENT);
        
                    final ImageView thumbView = new ImageView(_context);
                    thumbView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    thumbView.setLayoutParams(params);
                    thumbView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.d(TAG, "Thumbnail clicked");
        
                            // Set the pager position when thumbnail clicked
                            _pager.setCurrentItem(position);
                        }
                    });
                    _thumbnails.addView(thumbView);
        
                    final SubsamplingScaleImageView imageView =
                            (SubsamplingScaleImageView) itemView.findViewById(R.id.image);
        
                    // Asynchronously load the image and set the thumbnail and pager view
                    Glide.with(_context)
                            .load(_images.get(position))
                            .asBitmap()
                            .into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                                    imageView.setImage(ImageSource.bitmap(bitmap));
                                    thumbView.setImageBitmap(bitmap);
                                }
                            });
        
                    return itemView;
                }
        
                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    container.removeView((LinearLayout) object);
                }
            }
        }
        

        完整的 Android 项目已在 Github 上发布:https://github.com/sourcey/imagegallerydemo

        如果您有所需的内容,请选择一个答案...

        【讨论】:

        • 你为什么使用 InjectView 而不是经典的 findViewById ?是不是更好?谢谢。
        • Net 肯定更好,但 Java 是一种非常冗长的语言,它有助于创建更简洁的代码。
        【解决方案6】:

        查看工作中的demo from here

        随着 RecyclerView 库的发布,您可以轻松实现水平和垂直列表方向。这可以通过使用 LinearLayoutManager 来实现,您可以为其指定水平或垂直方向,如下所示...

         LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
        

        你可以read more

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-09
          • 2011-10-27
          • 1970-01-01
          • 1970-01-01
          • 2012-10-05
          相关资源
          最近更新 更多