1

我是安卓新手。我现在有个要求。我需要在 res/drawable 文件夹中添加大约 10 张图像,并且在运行应用程序时,我应该在 listView 上显示这些图像。在选择任何图像时,我应该在新活动中显示这个图像应该能够放大并缩小。请帮助我用示例代码弄清楚这一点。

提前致谢。

4

3 回答 3

5

此示例用于创建 Gallery,选择一个并设置为 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);

        }
于 2012-11-02T11:15:22.817 回答
1

您可以使用 GalleryView

请参阅以下链接以获取更多帮助:

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

于 2012-11-02T11:20:43.970 回答
1
    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

        }
    });
}

和图像适配器

    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;
    }
}
于 2014-04-03T04:44:14.743 回答