从gridview中选择图像并将其显示在其他意图上

时间:2012-07-17 08:37:51

标签: android android-intent gallery

我正在创建画廊。 我有gridlayout显示所有图像。 现在我想在我们选择一个图像时这样做,然后另一个意图将开始我已经完成,但现在我想在这个新意图中显示所选图像。

提前完成。

2 个答案:

答案 0 :(得分:2)

请使用以下gridview代码。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center" />

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

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

    public int getCount() {
        return mThumbIds.length;
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(Constants.mThumbIds[position]);
        return imageView;
    }
}

Constants.java

public class Constants {
    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

MainActivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intentt mInDisplay=new Intent(MainActivity.this, DisplayActivity.class);
                mInDisplay.putExtra("Index", position);
                startActivity(mInDisplay);
            }
        });
    }
}

display.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/mImgView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

DisplayActivity.java

public class DisplayActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bundle bdl=getIntent().getExtras();
        int index=bdl.getInt("Index");      
        ImageView mImage = (ImageView) findViewById(R.id.mImgView1);
        mImage.setImageResource(Constants.mThumbIds[index]);
    }
}

并将这两个活动声明到您的Android清单文件中。

答案 1 :(得分:0)

您可以通过setOnClickListener在每个图像上设置点击监听器,然后监听器可以发送意图以启动新活动。

我们还希望发送具有意图的图像路径。为此,您可以使用putExtra  使用您选择作为第一个参数的名称和作为第二个参数的图像路径的意图。

然后,您应该可以在新活动中使用getStringExtra来检索图像路径。您需要通过在活动中调用getIntent()来获取意图

祝你好运