如何在全屏显示选定的网格图像

时间:2014-06-14 09:35:34

标签: java android xml gridview android-activity

我使用教程创建了一个网格视图应用程序:

请参阅教程链接: http://techiedreams.com/android-custom-gridview-scalable-auto-adjusting-col-width/

我希望在其中添加新功能,我想在新的全屏活动中显示所选的网格图像。

我试过这样但我收到了错误:请阅读我的代码:

错误:

  1. 描述资源路径位置类型ImageAdapter不能 解析为类型AndroidGridLayoutActivity.java / TD的副本 GridView / src / com / td / gridview第21行Java问题

  2. 描述资源路径位置类型ImageAdapter不能     解析为类型FullImageActivity.java / TD的副本     GridView / src / com / td / gridview第20行Java问题

  3. 描述资源路径位置类型构造函数     MainActivity(FullImageActivity)是     undefined FullImageActivity.java / TD的副本     GridView / src / com / td / gridview第20行Java问题

  4. 我的代码这是教程中使用的原始代码/请参阅此帖子之上的链接 这是第一个教程中仅使用网格视图图像的代码

    MainActivity.java

    package com.td.gridview;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.view.ViewTreeObserver;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        private GridView photoGrid;
        private int mPhotoSize, mPhotoSpacing;
        private ImageAdapter imageAdapter;
    
        // Some items to add to the GRID
        private static final String[] CONTENT = new String[] { "Akon", "Justin Bieber", "AlRight", "Big Sean",
                "Britney Spears", "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean",
                "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears",
                "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary",
                "Micheal Buble" };
        private static final int[] ICONS = new int[] { R.drawable.cover_akon, R.drawable.cover_justin,
                R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
                R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
                R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
                R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
                R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
                R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
                R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
                R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
                R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
                R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
                R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
                R.drawable.cover_hilary, R.drawable.cover_mb };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // get the photo size and spacing
            mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
            mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);
    
            // initialize image adapter
            imageAdapter = new ImageAdapter();
    
            photoGrid = (GridView) findViewById(R.id.albumGrid);
    
            // set image adapter to the GridView
            photoGrid.setAdapter(imageAdapter);
    
            // get the view tree observer of the grid and set the height and numcols dynamically
            photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (imageAdapter.getNumColumns() == 0) {
                        final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                        if (numColumns > 0) {
                            final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                            imageAdapter.setNumColumns(numColumns);
                            imageAdapter.setItemHeight(columnWidth);
    
                        }
                    }
                }
            });
        }
    
        // ///////// ImageAdapter class /////////////////
        public class ImageAdapter extends BaseAdapter {
            private LayoutInflater mInflater;
            private int mItemHeight = 0;
            private int mNumColumns = 0;
            private RelativeLayout.LayoutParams mImageViewLayoutParams;
    
            public ImageAdapter() {
                mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT);
            }
    
            public int getCount() {
                return CONTENT.length;
            }
    
            // set numcols
            public void setNumColumns(int numColumns) {
                mNumColumns = numColumns;
            }
    
            public int getNumColumns() {
                return mNumColumns;
            }
    
            // set photo item height
            public void setItemHeight(int height) {
                if (height == mItemHeight) {
                    return;
                }
                mItemHeight = height;
                mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
                notifyDataSetChanged();
            }
    
            public Object getItem(int position) {
                return position;
            }
    
            public long getItemId(int position) {
                return position;
            }
    
            public View getView(final int position, View view, ViewGroup parent) {
    
                if (view == null)
                    view = mInflater.inflate(R.layout.photo_item, null);
    
                ImageView cover = (ImageView) view.findViewById(R.id.cover);
                TextView title = (TextView) view.findViewById(R.id.title);
    
                cover.setLayoutParams(mImageViewLayoutParams);
    
                // Check the height matches our calculated column width
                if (cover.getLayoutParams().height != mItemHeight) {
                    cover.setLayoutParams(mImageViewLayoutParams);
                }
    
                cover.setImageResource(ICONS[position % ICONS.length]);
                title.setText(CONTENT[position % CONTENT.length]);
    
                return view;
            }
        }
    
    }
    

    activity_main.xml中

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/albumGrid"
        style="@style/PhotoGrid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/black"
        android:columnWidth="@dimen/photo_size"
        android:horizontalSpacing="@dimen/photo_spacing"
        android:numColumns="auto_fit"
        android:padding="4dp"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:verticalSpacing="@dimen/photo_spacing" />
    

    photo_item.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/album_item"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/cover"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/cover"
            android:background="#70000000"
            android:padding="6dp" >
    
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="@color/white"
                android:textSize="12sp"
                android:textStyle="bold" />
        </LinearLayout>
    
    </RelativeLayout>
    

    的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.td.gridview"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.td.gridview.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:name="com.td.gridview.AndroidGridLayoutActivity"
                android:label="@string/app_name" >            
            </activity>
            <activity
                android:name="com.td.gridview.FullImageActivity"
                android:label="@string/app_name" >            
            </activity>
        </application>
    
    </manifest>
    

    以下代码我添加了以全屏显示所选网格图像 我从本教程获得此代码,请参阅链接

    http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

    这是第二个以全屏显示所选网格图像的教程/请参阅本教程中的“在新活动中显示所选网格图像(全屏)”。

    本教程有两部分用于基本网格,第二部分用于在新活动(全屏)中显示选定的网格图像,

    请记住我只使用本教程的第2部分。而不是本教程的第1部分,我使用第一个网格视图教程。

    我的网格视图第1部分的第1部分是好的,第2部分需要帮助第2部分在新活动中显示选定的网格图像(全屏)

    full_image.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ImageView 
                android:id="@+id/full_image_view"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
    
    </LinearLayout>
    

    <强烈> FullImageActivity.java

    package com.td.gridview;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class FullImageActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.full_image);
    
            // get intent data
            Intent i = getIntent();
    
            // Selected image id
            int position = i.getExtras().getInt("id");
            ImageAdapter imageAdapter = new ImageAdapter(this);
    
            ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
            imageView.setImageResource(imageAdapter.mThumbIds[position]);
        }
    
    }
    

    AndroidGridLayoutActivity.java

    package com.td.gridview;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.GridView;
    
    public class AndroidGridLayoutActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            GridView gridView = (GridView) findViewById(R.id.albumGrid);
    
            // Instance of ImageAdapter Class
            gridView.setAdapter(new ImageAdapter(this));
    
            /**
             * On Click event for Single Gridview Item
             * */
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {
    
                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                    // passing array index
                    i.putExtra("id", position);
                    startActivity(i);
                }
            });
        }
    }
    

    我的编程技巧不好所以请帮助调整我的代码,这样我就可以在新的全屏活动中显示网格图像。

    网格图像运行良好(使用第一个教程),但是当我从第二个教程中添加新代码时,我遇到了问题。我没有得到如何调整第二个教程代码以在新的全屏显示选定的网格图像。

    我添加了AndroidGridLayoutActivity.java,FullImageActivity.java。在使用第二篇教程后的manifest.xml中

    (请查看提供的教程链接,以便了解实际代码是什么以及我正在尝试的内容)

    请在这里帮助我,尝试给予公平的答案,而不仅仅是评论并将我传递给我在那里,因为我被困在这里没有得到如何调整我的网格视图图像中的“在新活动(全屏)中显示选定的网格图像”代码。

    请告诉我我做错了什么,并希望更改/调整这些代码

    我希望我提供公平的细节。

    __________________________________________________________________________________

    我得到了答案

    经过一些更改我的工作代码为:

    MainActivity.java

    package com.td.gridview;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.view.ViewTreeObserver;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public class MainActivity extends Activity {
    
        private GridView photoGrid;
        private int mPhotoSize, mPhotoSpacing;
        private ImageAdapter imageAdapter;
    
        // Some items to add to the GRID
        private static final String[] CONTENT = new String[] { "Akon", "Justin Bieber", "AlRight", "Big Sean",
                "Britney Spears", "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean",
                "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
                "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears",
                "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary",
                "Micheal Buble" };
        static final int[] ICONS = new int[] { R.drawable.cover_akon, R.drawable.cover_justin,
                R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
                R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
                R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
                R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
                R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
                R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
                R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
                R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
                R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
                R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
                R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
                R.drawable.cover_hilary, R.drawable.cover_mb };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // get the photo size and spacing
            mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
            mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);
    
            // initialize image adapter
            imageAdapter = new ImageAdapter();
    
            photoGrid = (GridView) findViewById(R.id.albumGrid);
    
            //start sent image to full screen             
    
            /**
             * On Click event for Single Gridview Item
             * */
            photoGrid.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {
    
                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                    // passing array index
                    i.putExtra("id", position);
                    startActivity(i);
                }
            });
            //end sent image to full screen
    
            // set image adapter to the GridView
            photoGrid.setAdapter(imageAdapter);
    
            // get the view tree observer of the grid and set the height and numcols dynamically
            photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (imageAdapter.getNumColumns() == 0) {
                        final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                        if (numColumns > 0) {
                            final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                            imageAdapter.setNumColumns(numColumns);
                            imageAdapter.setItemHeight(columnWidth);
    
                        }
                    }
                }
            });
        }
    
        // ///////// ImageAdapter class /////////////////
        public class ImageAdapter extends BaseAdapter {
            private LayoutInflater mInflater;
            private int mItemHeight = 0;
            private int mNumColumns = 0;
            private RelativeLayout.LayoutParams mImageViewLayoutParams;
    
            public ImageAdapter() {
                mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT);
            }
    
            public int getCount() {
                return CONTENT.length;
            }
    
            // set numcols
            public void setNumColumns(int numColumns) {
                mNumColumns = numColumns;
            }
    
            public int getNumColumns() {
                return mNumColumns;
            }
    
            // set photo item height
            public void setItemHeight(int height) {
                if (height == mItemHeight) {
                    return;
                }
                mItemHeight = height;
                mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
                notifyDataSetChanged();
            }
    
            public Object getItem(int position) {
                return position;
            }
    
            public long getItemId(int position) {
                return position;
            }
    
            public View getView(final int position, View view, ViewGroup parent) {
    
                if (view == null)
                    view = mInflater.inflate(R.layout.photo_item, null);
    
                ImageView cover = (ImageView) view.findViewById(R.id.cover);
                TextView title = (TextView) view.findViewById(R.id.title);
    
                cover.setLayoutParams(mImageViewLayoutParams);
    
                // Check the height matches our calculated column width
                if (cover.getLayoutParams().height != mItemHeight) {
                    cover.setLayoutParams(mImageViewLayoutParams);
                }
    
                cover.setImageResource(ICONS[position % ICONS.length]);
                title.setText(CONTENT[position % CONTENT.length]);
    
                return view;
            }
        }
    
    }
    

    FullImageActivity.java

    package com.td.gridview;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class FullImageActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.full_image);
    
            // get intent data
            Intent i = getIntent();
    
            // Selected image id
            int position = i.getExtras().getInt("id");
            ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
            imageView.setImageResource(MainActivity.ICONS[position]);
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

什么是imageAdapter.mThumbIds [position]?我想你应该试试这个。

imageView.setImageResource(MainActivity.ICONS [位置]);