我需要开发一个应用程序,我需要能够在用户share image
时ImageView in ViewPager
press on Share Button
。
我的问题:
当我press on Share Button
ImageView
缓存下一个Image
而不是当前。
public class MainActivity extends Activity { private ViewPager pager; private ImageView Imgv;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.pager); final Integer[] imageUrls={ R.drawable.img1 , R.drawable.img2 , R.drawable.img3 , R.drawable.img4 }; pager.setAdapter(new MyAdapter(imageUrls)); Button share = (Button) findViewById(R.id.share); share.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Imgv.setDrawingCacheEnabled(true); Bitmap icon = Imgv.getDrawingCache(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"); try { file.createNewFile(); FileOutputStream fo = new FileOutputStream(file); fo.write(bytes.toByteArray()); } catch (IOException e) { e.printStackTrace(); } Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("image/jpeg"); Uri uri = Uri.parse("file:///sdcard/temporary_file.jpg"); shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); shareIntent.putExtra(Intent.EXTRA_STREAM,uri); startActivity(shareIntent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } private class MyAdapter extends PagerAdapter { private LayoutInflater inflater; private Integer[] fimg; public MyAdapter(Integer[] fimg ) { this.fimg=fimg; inflater = getLayoutInflater(); } @Override public Object instantiateItem(View collection, int position ) { final View imageLayout = inflater.inflate(R.layout.imgview, null); Imgv = (ImageView)imageLayout.findViewById(R.id.full_image_view); Imgv.setBackgroundResource(fimg[position]); ((ViewPager) collection).addView(imageLayout ,0); return imageLayout; } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return super.getItemPosition(object); } @Override public int getCount() { return fimg.length; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { // TODO Auto-generated method stub return view==((View)object); } @Override public void restoreState(Parcelable arg0, ClassLoader loader) {} @Override public Parcelable saveState() { return null; } @Override public void startUpdate(View container) { } @Override public void finishUpdate(View container) { }
}
}
答案 0 :(得分:3)
这是因为您认为Imgv是ViewPager中当前可见页面的ImageView。但它不是。在你的情况下,Imgv是最后一个膨胀页面的ImageView,但不是当前可见页面。
我建议你为ImageView添加标签:
Imgv.setBackgroundResource(fimg[position]);
Imgv.setTag(position); // add this line
然后你可以通过标签找到你的ImageView:
ImageView Imgv = (ImageView)pager.findViewWithTag(pager.getCurrentItem());
Imgv.setDrawingCacheEnabled(true);
...
答案 1 :(得分:1)
为了避免使用有时意味着缩小图像的绘图缓存,我会检查ViewPager onPageChange listener的onPageSelected()事件。
保存当前索引,并创建要从原始资产中保存的文件。
答案 2 :(得分:0)
获取图片的最简单,最好的方法是使用http://developer.android.com/reference/android/support/v4/view/ViewPager.html#getCurrentItem()
这将为您提供当前页面的索引,该索引只是具有当前图像的imageUrls的索引。
所以使用BitmapFactory从imageUrls获取你的位图。
BitmapFactory.decodeResource(getResources(),imageUrls [pager.getCurrentItem()]);