图库 - 查看多张图片

时间:2012-06-18 01:10:10

标签: android android-layout android-gallery android-menu

我有一个工作图库,一次显示一张图片,可以“刷”以旋转图像。我希望用户可以选择一次查看2或3张图片,方法是使用菜单并选择要显示的图片数量。到目前为止,我已经尝试调整Gallery宽度,并且LinearLayout params和所有崩溃活动。任何意见,将不胜感激。 我在这里声明并初始化Gallery并拥有onOptionsItemSelected方法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;
}

这是图库的图像适配器类

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

1 个答案:

答案 0 :(得分:0)

在你的适配器中, 使用View对象(自定义视图)并将ImageView放入其中。

这是你可以为每个视图放置多个图像。

根据用户选择,根据图像数量和使用正确的布局文件,可以实现最佳效果。

如果您需要更具体的代码模板,请告诉我。