简体   繁体   English

如何将图像从图库设置为滑块?

[英]How do I set images from gallery to slider?

I have one fragment, there is an imageView and 2 buttons (previous and next). 我有一个片段,有一个imageView和2个按钮(上一个和下一个)。 So in that imageView I'm adding the first picture from gallery. 因此,在该imageView中,我要添加图库中的第一张图片。 Then clicking next I want to see the next image from gallery. 然后单击下一步,我想查看图库中的下一张图像。 The path of images I already got. 我已经得到的图像的路径。

Let me show you the code. 让我向您展示代码。

public class PhotoFragment extends BaseFragment{
View mainView;
private ImageView photoView;
private Button prev, next;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.photo_fragment, container, false);

    photoView = (ImageView) mainView.findViewById(R.id.photoView);
    prev = (Button) mainView.findViewById(R.id.prevPhoto);
    next = (Button) mainView.findViewById(R.id.nextPhoto);

    getAllShownImagesPath(getActivity());
    return mainView;
}

ArrayList<String> getAllShownImagesPath(Activity activity) {
    Uri uri;
    Cursor cursor;
    int column_index_data;
    final ArrayList<String> listOfAllImages = new ArrayList<>();
    String absolutePathOfImage;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    String[] projection = {MediaStore.MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

    cursor = activity.getContentResolver().query(uri, projection, null,
            null, null);

    column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    while (cursor.moveToNext()) {
        absolutePathOfImage = cursor.getString(column_index_data);
        listOfAllImages.add(absolutePathOfImage);
    }

    Bitmap myBitmap = BitmapFactory.decodeFile(listOfAllImages.get(0));
    photoView.setImageBitmap(myBitmap);
    photoView.setRotation(90);

    Bitmap myBitmap1 = BitmapFactory.decodeFile(listOfAllImages.get(1));
    Bitmap myBitmap2 = BitmapFactory.decodeFile(listOfAllImages.get(2));
    Bitmap myBitmap3 = BitmapFactory.decodeFile(listOfAllImages.get(3));
    Bitmap myBitmap4 = BitmapFactory.decodeFile(listOfAllImages.get(4));
    Bitmap myBitmap5 = BitmapFactory.decodeFile(listOfAllImages.get(5));
    Bitmap myBitmap6 = BitmapFactory.decodeFile(listOfAllImages.get(6));

    final ArrayList<Bitmap> bitmap = new ArrayList<>();
    bitmap.add(myBitmap);
    bitmap.add(myBitmap1);
    bitmap.add(myBitmap2);
    bitmap.add(myBitmap3);
    bitmap.add(myBitmap4);
    bitmap.add(myBitmap5);
    bitmap.add(myBitmap6);


    prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    cursor.close();
    return listOfAllImages;
}

You see here I'm getting the first image and setting it in imageView. 您会在这里看到我正在获取第一张图像并将其设置在imageView中。

Bitmap myBitmap = BitmapFactory.decodeFile(listOfAllImages.get(0));
    photoView.setImageBitmap(myBitmap);

But I can't do the sliding part. 但是我不能做滑动部分。 How can I do this? 我怎样才能做到这一点?

int imagePosition = 0;

next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          imagePosition++;
      photoView.setImageBitmap(BitmapFactory.decodeFile(listOfAllImages.get(imagePosition)););
          photoView.setRotation(90);
        }
    });

prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          imagePosition--;
      photoView.setImageBitmap(BitmapFactory.decodeFile(listOfAllImages.get(imagePosition)););
          photoView.setRotation(90);
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置setImageResource,我有手机的图库图片的URI - How to set setImageResource, I have URIs of the gallery images of my phone 当我在列表中找到这些图像的Uris时,如何使用画廊使用意图从外部存储查看多个图像 - How do I use gallery to view multiple images from external storage using intents when I hava Uris of those images in a list 我如何从 android 工作室的图库中获取 select 多个图像 - how I can select multiple images from gallery in android studio 如何将String变量设置为与滑块上的值相等? - How do I set a String variable equal to that of the value on a slider? 如何保存从图库中挑选的图像? - How to save images which are picked from gallery? 如何仅从图库中获取图像 - How to get only images from gallery 如何从图库中选择多个图像? - How to select multiple images from gallery? 如何将图库中的图像动态添加到我的 gridview? 我可以 go 到画廊,但图像没有被添加到我的 gridview - How can I add images from the gallery to my gridview dynamically ? I can go to the gallery but images aren't getting added to my gridview 如何从资产图像制作图像 slider - How to make image slider from assets images 我如何让用户从图库中选择多个图像,并在选择它们时将它们按顺序放入队列中? - How can I have a user select multiple images from the gallery, and as they are selected have them put into a queue in order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM