我正在使用离子库(from this link)在gridview
中显示来自手机的图像和视频。我只需要在gridview
中单击它就可以获取图像,并在另一个活动中显示图像。通常我会使用Integer [position]和getItem()
来全屏显示图像。但是当我使用Ion库时如何做到这一点?
public class MainActivity extends Activity {
private MyAdapter mAdapter;
private GridView view;
// Adapter to populate and imageview from an url contained in the array adapter
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context) {
super(context, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// see if we need to load more to get 40, otherwise populate the adapter
if (position > getCount() - 4)
loadMore();
if (convertView == null)
convertView = getLayoutInflater().inflate(R.layout.image, null);
// find the image view
final ImageView iv = (ImageView) convertView.findViewById(R.id.image);
// select the image view
Ion.with(iv)
.centerCrop()
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.load(getItem(position));
return convertView;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);
setContentView(R.layout.activity_main);
int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi * 2;
view = (GridView) findViewById(R.id.results);
view.setNumColumns(cols);
mAdapter = new MyAdapter(this);
view.setAdapter(mAdapter);
loadMore();
view.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
// HERE I WANT TO GIVE THE INTENT TO DISPLAY IMAGE IN FULL SCREEN
}
});
}
Cursor mediaCursor;
public void loadMore() {
if (mediaCursor == null) {
mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("external"), null, null, null, null);
}
int loaded = 0;
while (mediaCursor.moveToNext() && loaded < 10) {
// get the media type. ion can show images for both regular images AND video.
int mediaType = mediaCursor.getInt(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE));
if (mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
&& mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO) {
continue;
}
loaded++;
String uri = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
File file = new File(uri);
// turn this into a file uri if necessary/possible
if (file.exists())
mAdapter.add(file.toURI().toString());
else
mAdapter.add(uri);
}
}
}
答案 0 :(得分:3)
view.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
// HERE I WANT TO GIVE THE INTENT TO DISPLAY IMAGE IN FULL SCREEN
Intent i = new Intent(MainActivity.this, FullScreenViewActivity.class);
i.putExtra("fullimagepath", mAdapter.get(position));
MainActivity.this.startActivity(i);
}
FullScreenViewActivity
public class FullScreenViewActivity extends Activity {
ImageView fullImage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);
setContentView(R.layout.activity_main);
String s = getIntent().getStringExtra("fullimagepath");
fullImage = (ImageView) findViewById(R.id.fullimage);
Ion.with(fullImage)
.centerCrop()
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.load(s);
}}
答案 1 :(得分:0)
在getView()方法的适配器中,设置转换视图标记图像网址。之后
view.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(thisclass, toClass);
intent.putExtra(view.getTag()); // pass the url to other activity
startActivity(intent);
}
});
在另一个活动中,从字符串extra获取url并从url加载图像。
答案 2 :(得分:0)
您的活动应该实现AdapterView.OnItemClickListener。
在网格视图上添加项目单击侦听器
view.setOnItemClickListener(this);
从网格视图中获取所选图片,并在另一项活动中全屏打开:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String imageUrl = (String) parent.getItemAtPosition(position);
Intent intent = new Intent(MainActivity.this, FullScreenImageActivity.class);
intent.putExtra("IMAGE_URL", imageUrl);
startActivity(intent);
}
在FullScreenImageActivity中:
String imageUrl = getIntent().getStringExtra("IMAGE_URL");
// Load image