简体   繁体   English

使用res文件夹中的可绘制图像创建图像库

[英]Creating Image Gallery using Drawable images in res folder

I am new to android. 我是Android新手。 I have a requirement now. 我现在有一个要求。 I need to add around 10 images in the res/drawable folder and on running the app i should display this images on a listView.and on selecting any of the image i should display this image in the new activity should be able to zoom in and zoom out. 我需要在res / drawable文件夹中添加大约10张图像,并且在运行该应用程序时,我应该在listView上显示该图像。在选择任何图像时,我应该在新活动中显示该图像,并且应该能够放大并缩小。 Please help me out to figure out this with the sample code. 请帮我找出示例代码。

Thanks in advance. 提前致谢。

this example for create Gallery, select one and set to selectedImageView. 在创建图库的此示例中,选择一个并设置为selectedImageView。 so after that you can do everything with selectedImageView. 因此,之后您可以使用selectedImageView进行所有操作。

  public class MyActivity extends Activity{

    private int selectedImagePosition = 0;
    private ImageView selectedImageView;
    private List<Drawable> drawables;
    private Gallery gallery;    


    @Override
    public void onCreate(Bundle savedInstanceState) {
      selectedImageView = (ImageView) view.findViewById(R.id.selected_imageview);
      getDrawablesList();

      gallery = (Gallery) view.findViewById(R.id.Gallery);
      gallery.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));

      gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {

        setSelectedImage(selectedImagePosition);

    }
    });



     private void getDrawablesList() {
            drawables = new ArrayList<Drawable>();
            drawables.add(getResources().getDrawable(R.drawable.res1));
            drawables.add(getResources().getDrawable(R.drawable.res2));
            drawables.add(getResources().getDrawable(R.drawable.res3));
            drawables.add(getResources().getDrawable(R.drawable.res4));
        }

    private void setSelectedImage(int selectedImagePosition) {

            BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
            Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
            selectedImageView.setImageBitmap(b);
            selectedImageView.setScaleType(ScaleType.FIT_XY);

        }

You can use a GalleryView 您可以使用GalleryView

Refer to the link below for more help: 请参阅下面的链接以获取更多帮助:

http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views

    private ImageView selectedImageView;
    private TextView _nameTextView;
    private Gallery gallery;
    Integer[] imageIDs = { R.drawable.hbath, R.drawable.hfood,
        R.drawable.hmedicine, R.drawable.htherapy, R.drawable.htoilet,
        R.drawable.hother };

@Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            selectedImageView = (ImageView) findViewById(R.id.imageSwitcher1);
            _nameTextView = (TextView) findViewById(R.id.NameTextView);
            gallery = (Gallery) findViewById(R.id.gallery1);
            gallery.setAdapter(new ImageAdapter(this));
            gallery.setOnItemClickListener(new OnItemClickListener() {
            gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub

            if (position == 0) {
                selectedImageView
                        .setImageResource(R.drawable.hbathbackground);
                _nameTextView.setText("Toilet");
            } else if (position == 1) {
                selectedImageView
                        .setImageResource(R.drawable.hfoodbackground);
                _nameTextView.setText("Food");
            } else if (position == 2) {
                selectedImageView
                        .setImageResource(R.drawable.hmedicinebackground);
                _nameTextView.setText("Medicine");
            } else if (position == 3) {
                selectedImageView
                        .setImageResource(R.drawable.htherapybackground);
                _nameTextView.setText("Therapy");
            } else if (position == 4) {
                selectedImageView
                        .setImageResource(R.drawable.htoiletbackground);
                _nameTextView.setText("Bath");
            } else if (position == 5) {
                selectedImageView
                        .setImageResource(R.drawable.hotherbackground);
                _nameTextView.setText("Other");
            }

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
}

and ImageAdapter 和ImageAdapter

    public class ImageAdapter extends BaseAdapter {
    private Context context;
    private int itemBackground;

    public ImageAdapter(Context c) {
        context = c;
        // ---setting the style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }

    // ---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    // ---returns the ID of an item---
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    // ---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setBackgroundColor(0xFF000000);
        // imgView.setImageBitmap(bitmap);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(155, 235));
        imageView.setBackgroundResource(R.drawable.customborder2);
        return imageView;
    }
}

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

相关问题 使用文件路径无法绘制图像创建图像库时出现问题 - Problems creating Image Gallery using file paths not drawable images Android:下载图片并将其放在res / drawable文件夹中 - Android: download image and put it in the res/drawable folder 使用带前缀且不带xml的Drawable创建图库 - Creating Image Gallery using Drawable with prefix and without xml 将图像文件从res / drawable保存到android中的Gallery App - Saving an image file from res/drawable to Gallery App in android res文件夹中的可绘制文件夹? - Drawable folders in res folder? 如何从Android上可绘制文件夹中的图像加载gridview? - How to load gridview with images from res drawable folder on Android? 是否可以将图像从res / drawable文件夹传输到android中的SQLiteDB? - Is it possible to transfer the images from the res/drawable folder to the SQLiteDB in android? 将图像存储在可绘制文件夹中,并通过使用数据库中的imagePath来加载图像 - Storing images in drawable folder and getting the image to load by using the imagePath in database 使用Universal Image Loader从Drawable文件夹显示图像 - Display images from Drawable folder using Universal Image Loader 如何使用res文件夹的drawable-hdpi,drawable-mdpi,drawable-ldpi中存在的图像? - how to use images present in drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM