在“库”视图中显示特定文件夹中的图像

时间:2013-01-30 09:30:03

标签: android gallery galleryview

我有一个图库视图,根据项目内的drawable显示图像。但现在我希望它显示特定文件夹的图像,我不确定如何让它以这种方式工作。图像文件夹“mnt / sdcard / ff /”的示例任何评论和回答都将受到高度赞赏!以下是基于drawables显示图像的代码。

private void setupUI() {

    leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
    rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);
    gallery = (Gallery) findViewById(R.id.gallery);

    leftArrowImageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (selectedImagePosition > 0) { --selectedImagePosition; }
            gallery.setSelection(selectedImagePosition, false);
        }
    });

    rightArrowImageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (selectedImagePosition < drawables.size() - 1) { ++selectedImagePosition; }
            gallery.setSelection(selectedImagePosition, false);
        }
    });

    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            selectedImagePosition = pos;
            if (selectedImagePosition > 0 && selectedImagePosition < drawables.size() - 1) {
                leftArrowImageView.setImageDrawable(getResources()
                                  .getDrawable(R.drawable.arrow_left_enabled));
                rightArrowImageView.setImageDrawable(getResources()
                                   .getDrawable(R.drawable.arrow_right_enabled));
            } else if (selectedImagePosition == 0) {
                leftArrowImageView.setImageDrawable(getResources()
                                  .getDrawable(R.drawable.arrow_left_disabled));
            } else if (selectedImagePosition == drawables.size() - 1) {
                rightArrowImageView.setImageDrawable(getResources()
                                   .getDrawable(R.drawable.arrow_right_disabled));
            }
            changeBorderForSelectedImage(selectedImagePosition);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) { }
    });

    galImageAdapter = new GalleryImageAdapter(this, drawables);
    gallery.setAdapter(galImageAdapter);

    if (drawables.size() > 0) {
        gallery.setSelection(selectedImagePosition, false);
    }

    if (drawables.size() == 1) {
        rightArrowImageView.setImageDrawable(getResources()
                           .getDrawable(R.drawable.arrow_right_disabled));
    }

    gallery.setCallbackDuringFling(false);
}

private void changeBorderForSelectedImage(int selectedItemPos) {
    int count = gallery.getChildCount();

    for (int i = 0; i < count; i++) {
        ImageView imageView = (ImageView) gallery.getChildAt(i);
        imageView.setBackgroundDrawable(getResources()
                 .getDrawable(R.drawable.image_border));
        imageView.setPadding(3, 3, 3, 3);
    }

    ImageView imageView = (ImageView) gallery.getSelectedView();
    imageView.setBackgroundDrawable(getResources()
             .getDrawable(R.drawable.selected_image_border));
    imageView.setPadding(3, 3, 3, 3);
}

private void getDrawablesList() {
    drawables = new ArrayList<Drawable>();
    drawables.add(getResources().getDrawable(R.drawable.ads1));
    drawables.add(getResources().getDrawable(R.drawable.ads2));
    drawables.add(getResources().getDrawable(R.drawable.ads3));
    drawables.add(getResources().getDrawable(R.drawable.ads4));
    drawables.add(getResources().getDrawable(R.drawable.ads5));
    drawables.add(getResources().getDrawable(R.drawable.ads6));
    drawables.add(getResources().getDrawable(R.drawable.ads7));
}

1 个答案:

答案 0 :(得分:1)

在res中创建名为raw(res / raw)的新文件夹并将图像放在此处。

并获取图像R.raw.imagename ................