主要Activity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_grid);
// populate data
products = new ArrayList();
products.add(new Product("http://image1.jpg"));
products.add(new Product("http://image2.jpg"));
products.add(new Product("http://image3"));
products.add(new Product("http://image4"));
//
gvProducts = (GridView) findViewById( R.id.grid_products);
adapterProducts = new ProductListAdapterWithCache(this, products);
gvProducts.setAdapter(adapterProducts);
gvProducts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String path = (MainActivity.this, position);
Intent intent = new Intent(MainActivity.this, FullImageActivity.class);
intent.putExtra("IMAGEPATH",path);
startActivity(intent);
}
});
}
我正在android studio中开发简单的图像处理应用程序。我正在从url
下载图像以在网格中显示如上所示..我想在点击网格时显示页面中的完整图像..尝试通过意图传递位置但在此行上显示错误。
String path = (MainActivity.this, position);
FullImageActivity.java
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_fullscreen_image);
// selected item
int imageInt = getIntent().getIntExtra("IMAGEPATH", R.drawable.spinner);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(imageInt);
}
}
我是初学者,如果我在基础知识上做错了什么,请指导我。
谢谢,
答案 0 :(得分:2)
尝试以下代码:
gvProducts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(MainActivity.this, FullImageActivity.class);
intent.putExtra("IMAGEPATH",products.get(position).imagePath);
startActivity(intent);
}
});
活动:
String path = getIntent().getStringExtra("IMAGEPATH");
答案 1 :(得分:1)
我建议你使用PhotoView和Universal Image Loader,参考PhotoView项目中的示例代码,我也提供一些代码,希望这会有所帮助。
Java Code片段:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.diary_imageview);
imageUrl = getIntent().getStringExtra(IMAGE_URL);
mProgress.setVisibility(View.VISIBLE);
mAttacher = new PhotoViewAttacher(mPhotoView);
mAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
@Override
public void onPhotoTap(View view, float v, float v2) {
finish();
}
});
ImageLoader.getInstance().displayImage(imageUrl, mPhotoView, Util.getDisplayImageOptions(), new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
mProgress.setVisibility(View.GONE);
mAttacher.update();
}
});
}
XML布局:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<uk.co.senab.photoview.PhotoView
android:id="@+id/photoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<ProgressBar
android:id="@+id/progress"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</merge>