在图像上单击,链接到另一个活动,以完整大小显示该图像

时间:2011-02-23 05:07:20

标签: android

我创建了ListView并点击了我的第一个视图,我正在显示GridView个图像。现在我希望当我点击任何一个图像时,它会显示在另一个页面上。

你能把我推向正确的方向吗?

这是我的代码:

1产品:>调用活动

public class ImageExercise extends Activity{
     /** Called when the activity is first created. */
    public static int pos;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image);
        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)
                                     {
                                     Intent intent = new Intent(ImageExercise.this,FullImage.class);
                                     pos=position;



                                     startActivity(intent);
                                   // finish();
                                     }

        });

    }
private void showImage(View view, int position) {
    ImageView imgView = (ImageView) view; 
    imgView.setImageResource(position); 
    imgView.setId(position);
}


}
class ImageAdapter extends BaseAdapter {
public static Integer[] mThumbIds = {

  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_8,
    R.drawable.sample_9,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_8,
    R.drawable.sample_9


};
    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(mThumbIds[position]);
    return imageView;
}

2: - &GT;要调用链接的调用活动:

public class FullImage extends Activity{
     @Override


 public void onCreate(Bundle savedInstanceState) {
       // super.onCreate(savedInstanceState);




        setContentView(R.layout.fullimage);

 }

}

3 个答案:

答案 0 :(得分:0)

您可以将imagesourceid作为意图传递,有点像这样。

Intent newintent = new Intent(test.this, image.class);
newintent.putExtra("IMAGE", R.drawable.ico_settings);

然后在目标活动中使用并使用belw

int imgid = getIntent().getIntExtra("IMAGE", 0);
ImageView img = (ImageView)findViewById(R.id.img);
img.setImageResource(imgid);

答案 1 :(得分:0)

从网格项中的ImageExercise活动中单击:

 Intent intent = new Intent(ImageExercise.this,FullImage.class);
 pos=position;
 intent.putExtra("IMAGE", R.drawable.ico_settings);
 startActivity(intent);

在完整图像活动中:

int imgid = getIntent().getIntExtra("IMAGE", 0);
ImageView img = (ImageView)findViewById(R.id.img);
img.setImageResource(ImageAdapter.mThumbIds[imgId] );

答案 2 :(得分:0)

首先,在适配器的import * as React from 'react'; import {InsightsComponent, InsightsComponentProps} from './statisticsComponent'; import {shallow,ShallowWrapper} from 'enzyme'; describe('Testing InsightComponent', () => { const props = { index: 1, AssignmentTitle: 'string', AssignmentDescription: 'string', AssignmentUtilizedHours: 'string', AssignmentScheduledHours: 'string', AssignmentStartDate: '10:10:1985', AssingmentEndDate: '10-10-1985', }; let wrapper = ShallowWrapper<InsightsComponentProps,any>(<InsightsComponent {props} /> ); // getting compile-time error here it('trimZeroDecimal', ()=>{ //let obj = new InsightsComponent(props); //expect(mockPlaySoundFile.mock.calls.length).toEqual(0); }) }); I wanted to test trimZeroDecimal() method that is inside this component. But I am getting errro while creating instance to component class. below line is showing error. I just wanted to know how to create instance of react component which has multiple interface inherited ShallowWrapper<InsightsComponentProps,any>(<InsightsComponent {props} /> ); // getting compile-time error here 方法中返回可绘制的resId,以便可以通过onItemClick回调中的适配器对其进行访问

getItem()

在此之后,在onItemClick回调中,将被点击项的资源ID放入意向中

class ImageAdapter extends BaseAdapter {

    public int getItem(int position) {
        return mThumbIds[position];
    }
}

最后,在全屏活动中,从意向中获取资源ID并将其设置在imageView中

gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        int imgResId = (int) parent.getItemAtPosition(position);
        Intent intent = new Intent(ImageExercise.this,FullImage.class);
        intent.putExtra("ImgResId", imgResId);
        startActivity(intent);
    }
});