我正在向viewpager滑动显示图像。如果用户喜欢图片而不是按钮点击按钮。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_full_image, container, false);
viewPager = (ImageViewPager) v.findViewById(R.id.viewpager);
images = (ArrayList<Photos>) getArguments().getSerializable("url");
selectedPosition = getArguments().getInt("position");
final Button save_img = (Button) v.findViewById(R.id.save_img);
final View view2 = viewPager.findViewWithTag("myview" +
viewPager.getCurrentItem());
save_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveImage();
}
});
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
setCurrentItem(selectedPosition);
return v;
}
这是instantiateItem方法 -
@Override
public Object instantiateItem( final ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
final ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
imageViewPreview.setOnTouchListener(new ImageMatrixTouchHandler(view.getContext()));
Photos image = images.get(position);
final String img_title = image.getTitle();
final String img_weight = image.getWeight();
Glide.with(imageViewPreview.getContext())
.load(image.getUrl())
.asBitmap()
.fitCenter().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageViewPreview.setImageBitmap(resource);
}
});
view.setTag("myview" + position);
container.addView(view);
return view;
}
现在,当用户点击按钮时,如何从滑行中获取图像资源?我有一个保存图像方法,可以在库中保存位图。
答案 0 :(得分:1)
//在instantiateItem内使用位置
为图像制作标签Glide.with(context).load(images.get(position))
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(myImage);
container.addView(view);
yourimageview.setTag(position);
//在活动中定义此变量
final File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/SmartPhoto");
boolean success = false;
//在活动中使用此方法保存图像
public void SaveImage()
{
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "image" + n + ".png";
myDir.mkdirs();
File image = new File(myDir, fname);
ImageView Imgv = (ImageView) yourviewpager.findViewWithTag(mPager.getCurrentItem());
Imgv.setDrawingCacheEnabled(true);
Bitmap bitmap = Imgv.getDrawingCache();
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), R.string.savedimage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), R.string.errorloadingimage, Toast.LENGTH_LONG).show();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
final Uri contentUri = Uri.fromFile(image);
scanIntent.setData(contentUri);
sendBroadcast(scanIntent);
} else {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://mnt/sdcard/" + Environment.getExternalStorageDirectory())));
}
}