【问题标题】:How to put a particular image inside all images listed in a grid view?如何将特定图像放在网格视图中列出的所有图像中?
【发布时间】:2014-12-17 05:20:36
【问题描述】:

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

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

 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>

注意:将特定图像(例如:一朵花)放在视图内列出的所有图像的顶部是我需要的......任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    您可能可以使用框架布局来实现此目的...

    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>
    

    【讨论】:

    • 为什么要投反对票!!投反对票的请注明并更正答案..
    【解决方案2】:

    杰克,我认为你需要将你的 activity_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>
    

    【讨论】:

    • 哦,萨西先生,我想你不明白我的问题。无论如何,谢谢
    • @sasi: 你能解释一下 android:kuttichatan="error_pokatte" 你是从哪个 SDK 使用这个标签的吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2014-01-15
    • 2023-03-10
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多