简体   繁体   中英

Gallery- viewing more than one picture

I have a working Gallery that shows one picture at a time and can be "swiped" to rotate through the images. I want to have the option of the user to view 2 or 3 pictures at a time by using the menu and selecting how many to show. So far Ive tried adjusting the Gallery width, and LinearLayout params and all crash the Activity. any advice would be appreciated. I declare and initialize the Gallery here and have the onOptionsItemSelected method sekeleton.

public class SpeechAppActivity extends Activity implements OnClickListener{
//Menu Items


// Class variables
Gallery myGallery;
ImageView imageView;
MyDBAdapter db;
Item item1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = new MyDBAdapter(this);

    db.insertEntry(item1 = new Item("Bathtub", "Bathroom", "Typical", "Clean", "fill, wash", "Round, deep", "Bathroom", "Toilet, Bathroom", R.drawable.ic_launcher));
    Log.i("item", "item: " + item1.toString());

 // Bind the gallery defined in the main.xml
        // Apply a new (customized) ImageAdapter to it.

        myGallery = (Gallery) findViewById(R.id.myGallery);

        myGallery.setAdapter(new ImageAdapter(this));
        //myGallery.setLayoutParams(new Gallery.LayoutParams(250, 250));

        myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View v,
                    int position, long id) {

            }

            public void onNothingSelected(AdapterView<?> parent) {

            }


        });
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case R.id.settings:
        startActivity(new Intent(this, Prefs.class));
        return true;

    case R.id.show1:

        //myGallery.findViewById(R.id.myGallery).setLayoutParams(new Gallery.LayoutParams(500, 250));
        return true;

    case R.id.show2:

        //myGallery.findViewById(R.id.myGallery).setLayoutParams(new Gallery.LayoutParams(500, 250));

        return true;
    case R.id.show3:

        //myGallery.findViewById(R.id.myGallery).setLayoutParams(new Gallery.LayoutParams(500, 250));
        return true;
    }
    return false;
}

This is the Image Adapter class for the Gallery

public class ImageAdapter extends BaseAdapter {
/** The parent context */
private Context myContext;
// Put some images to project-folder: /res/drawable/
// format: jpg, gif, png, bmp, ...
private int[] myImageIds = { R.drawable.apple, R.drawable.orange,
               R.drawable.ic_launcher };

/** Simple Constructor saving the 'parent' context. */
public ImageAdapter(Context c) {
    this.myContext = c;
}

// inherited abstract methods - must be implemented
// Returns count of images, and individual IDs
public int getCount() {
    return this.myImageIds.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}
// Returns a new ImageView to be displayed,
public View getView(int position, View convertView, 
        ViewGroup parent) {

    // Get a View to display image data                     
    ImageView iv = new ImageView(this.myContext);
    iv.setImageResource(this.myImageIds[position]);

    // Image should be scaled somehow
    //iv.setScaleType(ImageView.ScaleType.CENTER);
    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);           
    //iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    //iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
    //iv.setScaleType(ImageView.ScaleType.FIT_XY);
    //iv.setScaleType(ImageView.ScaleType.FIT_END);
    //iv.setScaleType(ImageView.ScaleType.FIT_START);


    // Set the Width & Height of the individual images
        //get scale for finding dip of a set # of pixels
    final float scale = parent.getContext().getResources().getDisplayMetrics().density;

    iv.setLayoutParams(new Gallery.LayoutParams((int) (300 * scale + 0.5f), (int) (250 * scale + 0.5f)));

    return iv;
}

}// ImageAdapter

In your adapter, Use View object (witn custom view) and put ImageView inside it.

This was you can put more than one images per view.

Best results can be achieved by few layout files based on number of images and using correct one according to user selection.

Let me know if you need more specific code template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM