[英]How to change the displayed Bitmap in a ViewPager displaying ImageViews
I use a ViewPager for image sliding. 我使用ViewPager进行图像滑动。 On a menu button click I want to replace the displaced Bitmap with a new processed Bitmap. 在菜单按钮上,单击“我想用新处理的位图替换置换的位图”。
I tried to change the ImageView at the Activity. 我试图在活动中更改ImageView。 But it changes the first ImageView Bitmap, not the one displayed. 但是它将更改第一个ImageView位图,而不更改显示的位图。
public void replaceImage(Bitmap newBitmap){
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setBitmap(newBitmap));
}
I think I have to change the picture at the adapter. 我想我必须在适配器上更改图片。 Does the FragmentPagerAdapter give me the functionality I need? FragmentPagerAdapter是否可以为我提供所需的功能?
my code if needed: https://gist.github.com/anonymous/e965a624b54b15a65f2c 我的代码是否需要: https : //gist.github.com/anonymous/e965a624b54b15a65f2c
The custom PagerAdapter you are using will work fine. 您正在使用的自定义PagerAdapter将正常工作。 You can either change data set, the String[] image array in the adapter, or make changes in the instantiateItem() method. 您可以更改数据集,适配器中的String []图像数组,也可以在InstantiateItem()方法中进行更改。 That is where the ImageView is created for each image. 那就是为每个图像创建ImageView的地方。 Remember to call notifyDataSetChanged() on your adapter when you have made a change, or the ViewPager won't reflect the changes. 进行更改后,请记住在适配器上调用notifyDataSetChanged(),否则ViewPager不会反映所做的更改。
I can change the current displayed Image view with this code. 我可以使用此代码更改当前显示的图像视图。
on the instantiateItem()
method of the adapter I give every ImageView a id with 在适配器的instantiateItem()
方法上,我给每个ImageView一个ID
imageView.setId(position);
in my setImage() function I can find the imageView with this code and can change the bitmap. 在我的setImage()函数中,可以使用此代码找到imageView并可以更改位图。
public void setFilterImage(){
ImageView currentImageView = (ImageView) findViewById(pager.getCurrentItem());
currentImageView.setImageDrawable( getResources().getDrawable( myNewBitmap ));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.