1

私はアンドロイドに不慣れです。今、要件があります。res / drawableフォルダーに約10枚の画像を追加する必要があり、アプリを実行すると、この画像をlistViewに表示する必要があります。画像のいずれかを選択すると、新しいアクティビティでこの画像を表示する必要があり、ズームインしてズームアウトする。サンプルコードでこれを理解するのを手伝ってください。

前もって感謝します。

4

3 に答える 3

5

ギャラリーを作成するこの例では、1 つを選択して 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
    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

        }
    });
}

と 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;
    }
}
于 2014-04-03T04:44:14.743 に答える
1

GalleryView を使用できます

詳細については、次のリンクを参照してください。

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

于 2012-11-02T11:20:43.970 に答える