Android Gallery显示两项

时间:2016-10-06 12:17:01

标签: android gallery

你好我做了android画廊。我添加了捕获图像相机或图库,但我只在显示中显示一个图像如何在图库中显示两个图库项目例如enter image description here

这是我的图库代码xml

<Gallery
android:id="@+id/main_list_view"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_alignLeft="@+id/btnAdd"
android:layout_below="@+id/btnAdd"
android:layout_marginTop="28dp"
android:cacheColorHint="@color/deepPurple1"/>

和我的主要活动

@SuppressLint("NewApi")

公共类MainActivity扩展了Activity {

private ArrayList<MyImage> images;
private ImageAdapter imageAdapter;
private Gallery listView;
private Uri mCapturedImageURI;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
private Handler handler;
Dialog dialog ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialog_box);
    dialog.setTitle("Alert Dialog View");


    // Construct the data source
    images = new ArrayList<MyImage>();
    // Create the adapter to convert the array to views
    imageAdapter = new ImageAdapter(this, images);
    // Attach the adapter to a ListView
    listView = (Gallery) findViewById(R.id.main_list_view);
    listView.setAdapter(imageAdapter);


    listView.setOnItemClickListener(new OnItemClickListener() {

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


                Button btnExit = (Button) dialog.findViewById(R.id.btnExit);
                btnExit.setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.findViewById(R.id.btnChoosePath)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                                activeGallery();
                            }
                        });
                dialog.findViewById(R.id.btnTakePhoto)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                                activeTakePhoto();
                            }
                        });

                // show dialog on screen
                dialog.show();

        }

    });

    listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub

            imageAdapter.remove(images.remove(position));

            return false;
        }
    });


}

public void btnAddOnClick(View view) {
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialog_box);
    dialog.setTitle("Alert Dialog View");
    Button btnExit = (Button) dialog.findViewById(R.id.btnExit);
    btnExit.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.findViewById(R.id.btnChoosePath)
            .setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    activeGallery();
                }
            });
    dialog.findViewById(R.id.btnTakePhoto)
            .setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    activeTakePhoto();
                }
            });

    // show dialog on screen
    dialog.show();
}

/**
 * take a photo
 */
private void activeTakePhoto() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        String fileName = "temp.jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        mCapturedImageURI = getContentResolver()
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        values);
        takePictureIntent
                .putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

/**
 * to gallery
 */
private void activeGallery() {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  startActivityForResult(intent, RESULT_LOAD_IMAGE);
}




@Override 
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case RESULT_LOAD_IMAGE:
            if (requestCode == RESULT_LOAD_IMAGE &&
                    resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver()
                        .query(selectedImage, filePathColumn, null, null,
                                null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
                MyImage image = new MyImage();
                image.setTitle("Test");
                image.setDescription(" add list view");
                image.setDatetime(System.currentTimeMillis());
                image.setPath(picturePath);
                images.add(image);

                imageAdapter = new ImageAdapter(this, images);
                // Attach the adapter to a ListView
                listView = (Gallery) findViewById(R.id.main_list_view);
                listView.setSpacing(-20);
                listView.setAdapter(imageAdapter);


            //    listView.setSelection(imageAdapter.getCount()-1);

                imageAdapter.notifyDataSetChanged();
            }
        case REQUEST_IMAGE_CAPTURE:
            if (requestCode == REQUEST_IMAGE_CAPTURE &&
                    resultCode == RESULT_OK) {
                String[] projection = {MediaStore.Images.Media.DATA};
                Cursor cursor =
                        managedQuery(mCapturedImageURI, projection, null,
                                null, null);
                int column_index_data = cursor.getColumnIndexOrThrow(
                        MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String picturePath = cursor.getString(column_index_data);
                MyImage image = new MyImage();
                image.setTitle("Test");
                image.setDescription("list view");
                image.setDatetime(System.currentTimeMillis());
                image.setPath(picturePath);
                images.add(image);

                imageAdapter = new ImageAdapter(this, images);
                // Attach the adapter to a ListView
                listView = (Gallery) findViewById(R.id.main_list_view);
                listView.setSpacing(-20);
                listView.setAdapter(imageAdapter);   

             //   listView.setSelection(imageAdapter.getCount()-1);
                imageAdapter.notifyDataSetChanged();                   

            }
    }
}

和我的适配器

public class ImageAdapter  extends ArrayAdapter<MyImage>{

 /**
 * applying ViewHolder pattern to speed up ListView, smoother and faster
 * item loading by caching view in A ViewHolder object
 */
private static class ViewHolder {
    ImageView imgIcon;
    TextView description;
}

public ImageAdapter(Context context, ArrayList<MyImage> images) {
    super(context, 0, images);
}

@Override public View getView(int position, View convertView,
        ViewGroup parent) {
    // view lookup cache stored in tag
    ViewHolder viewHolder;
    // Check if an existing view is being reused, otherwise inflate the
    // item view
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(getContext())
                .inflate(R.layout.item_image, parent, false);
        /*
        viewHolder.description =
                (TextView) convertView.findViewById(R.id.item_img_infor);
                */
        viewHolder.imgIcon =
                (ImageView) convertView.findViewById(R.id.item_img_icon);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // Get the data item for this position
    MyImage image = getItem(position);
    // set description text
   // viewHolder.description.setText(image.toString());
    // set image icon
    final int THUMBSIZE = 100;
    //        viewHolder.imgIcon.setImageURI(Uri.fromFile(new File(image
    // .getPath())));
    viewHolder.imgIcon.setImageBitmap(ThumbnailUtils
            .extractThumbnail(BitmapFactory.decodeFile(image.getPath()),
                    THUMBSIZE, THUMBSIZE));      
    viewHolder.imgIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
        viewHolder.imgIcon.setBackgroundResource(R.drawable.ios_retina_toggle_frame);
    // Return the completed view to render on screen
    return convertView;
}

}

我的图像类

public class MyImage {

 private String title, description, path;
    private Calendar datetime;
    private long datetimeLong;
    protected SimpleDateFormat df = new SimpleDateFormat("MMMM d, yy  h:mm");


    public String getTitle() { return title; }  

    public Calendar getDatetime() { return datetime; }

      public void setDatetime(long datetimeLong) {
        this.datetimeLong = datetimeLong;
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(datetimeLong);
        this.datetime = cal;
    }
    public void setDatetime(Calendar datetime) { this.datetime = datetime; }

    public String getDescription() { return description; }

    public void setTitle(String title) { this.title = title; }

    public long getDatetimeLong() { return datetimeLong; }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setPath(String path) { this.path = path; }

    public String getPath() { return path; }

    @Override public String toString() {
        return "Title:" + title + "   " + df.format(datetime.getTime()) +
                "\nDescription:" + description + "\nPath:" + path;
    }

}

0 个答案:

没有答案