如何将特定图像放在网格视图中列出的所有图像中?

时间:2014-12-17 05:20:37

标签: android xml image android-linearlayout android-gridview

我正在使用谷歌api做一个Android程序。我列出了需要在网格视图中上传到谷歌驱动器的所有图像。我需要的是我需要将特定图像放在所有图像的顶部为了首先,我需要将所有特定图像(一朵花)放在列出的所有图像的顶部。任何人都可以建议任何方法,这将是有帮助的。 提前谢谢.....

这是我列出网格视图中所有图像的代码.....

 public class MainActivity extends ActionBarActivity {
    protected Cursor mCursor;
    protected int columnIndex;
    protected GridView mGridView;
    protected ImageAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_gallery);

        // Get all the images on phone

        String[] projection = {
                MediaStore.Images.Thumbnails._ID,
                MediaStore.Images.Thumbnails.IMAGE_ID
        };

        mCursor = getContentResolver().query(
                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection,
                null,
                null,
                MediaStore.Images.Thumbnails.IMAGE_ID + " DESC"
        );

        columnIndex = mCursor.getColumnIndexOrThrow(projection[0]);

     // Get the GridView layout
        mGridView = (GridView) findViewById(R.id.gridView);
        mAdapter = new ImageAdapter(this);
        mGridView.setAdapter(mAdapter);
    }

    class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return mCursor.getCount();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        // Convert DP to PX

        public int dpToPx(int dps) {
            final float scale = getResources().getDisplayMetrics().density;
            int pixels = (int) (dps * scale + 0.5f);

            return pixels;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            int imageID = 0;

            // Want the width/height of the items
            // to be 120dp
            int wPixel = dpToPx(120);
            int hPixel = dpToPx(120);

            // Move cursor to current position
            mCursor.moveToPosition(position);
            // Get the current value for the requested column
            imageID = mCursor.getInt(columnIndex);

            if (convertView == null) {
                // If convertView is null then inflate the appropriate layout file
                convertView = LayoutInflater.from(mContext).inflate(R.layout.conversation_item, null);
            }
            else {

            }

            imageView = (ImageView) convertView.findViewById(R.id.imageView);

            // Set height and width constraints for the image view
            imageView.setLayoutParams(new LinearLayout.LayoutParams(wPixel, hPixel));

            // Set the content of the image based on the provided URI
            imageView.setImageURI(
                    Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID)
            );

            // Image should be cropped towards the center
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            // Set Padding for images
            imageView.setPadding(8, 8, 8, 8);

            // Crop the image to fit within its padding
            imageView.setCropToPadding(true);

            return convertView;
        }


    }


}

这些是我的xml文件

activity_photo_gallery.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="100dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:gravity="center" />

</LinearLayout>

conversation_item.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView" />
</LinearLayout>

注意:在视图中列出的所有图像的顶部放置一个特定图像(例如:一朵花)是我需要的东西......任何帮助都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

您可以使用框架布局来实现这一目标......

conversation_item.xml

 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/FrameLayout1"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:layout_gravity="center" >

        <ImageView
            android:id="@+id/thumbImage"
            android:layout_width="150dip"
            android:layout_height="150dip"
            android:scaleType="centerCrop" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="150dip"
            android:layout_height="150dip"
            android:layout_gravity="center"
            android:src="@drawable/select"
              />

    </FrameLayout>

答案 1 :(得分:-3)

杰克我想你需要将你的活动photo_gallery.xml文件编辑为:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:kuttichatan="error_pokatte"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView" />
</LinearLayout>