How to change the displayed Bitmap in a ViewPager displaying ImageViews - android

I use a ViewPager for image sliding. 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. But it changes the first ImageView Bitmap, not the one displayed.
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?
my code if needed: https://gist.github.com/anonymous/e965a624b54b15a65f2c

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
imageView.setId(position);
in my setImage() function I can find the imageView with this code and can change the bitmap.
public void setFilterImage(){
ImageView currentImageView = (ImageView) findViewById(pager.getCurrentItem());
currentImageView.setImageDrawable( getResources().getDrawable( myNewBitmap ));
}

The custom PagerAdapter you are using will work fine. You can either change data set, the String[] image array in the adapter, or make changes in the instantiateItem() method. That is where the ImageView is created for each image. Remember to call notifyDataSetChanged() on your adapter when you have made a change, or the ViewPager won't reflect the changes.

Related

Android: Display images in full screen mode using Picasso

Hi i have some images displayed in gridView using Picasso, when i try to open each image (after onItemClickListener) in full screen mode (inside new acivity) it display only the first image in my gridView whatever the image i click, this is my problem.
This is my code where i display the images from the url inside gridView and OnItemClickListener.
Picasso.with(ToolDescription.this)
.load(SaveSettings.ServerURL +"Images/"+ imagepath)
.into(imageView);
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ToolDescription.this, ImageFull.class);
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
startActivity(intent);
}
});
return myView;
This is my second activity (named ImageFull) where i want to display the clicked image:
ImageView imageView = (ImageView) findViewById(R.id.fullImage);
String imagepath = getIntent().getStringExtra("imagepath");
Picasso.with(ImageFull.this)
.load(imagepath)
.noFade()
.resize(800, 800)
.centerCrop()
.into(imageView);
Either set an individual click listener to each imageview or use the int position to pull the correct imagepath from your gridview item array.
Grid View Android Doc
This link has a easy to follow code for exactly what you're trying to do.. Use the position to find which element you are clicking on an get the respective image url
Looks like what's happening is that you're calling ls.setOnItemClickListener() every time getView() returns.
AdapterView.OnItemClickListener is a single listener for the entire AdapterView, and you're intended to differentiate which item was clicked on by using its position argument.
I'm not sure where imagepath is coming from inside getView(), but I assume it's something like String imagepath = getItem(position).getImagePath(). If that is the case, you can fix your problem by:
1) Move ls.setOnItemClickListener() out of getView()... probably to your onCreate() or whever you set up your GridView.
2) Change
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
to
String currentImagePath = getItem(position).getImagePath();
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ currentImagePath);
Probably there will be syntax errors because I can't see the rest of your code, but hopefully this is enough for you to be able to work through them.

Shared elements animation with AppCompat inside a RecyclerView

I have a Fragment with some ImageViews, if I click on one of this ImageViews the app open a new activity where the just clicked "target" image is at the top of it.
In the first place I put the ImageView in the new Activity directly in the parent layout.
Now in this activity I've set a custom Header as the first item of the RecyclerView, the "target" ImageView is in this new header.
Now the transition doesn't work anymore. I've thought that the content of the RecyclerView is drawn later, so I've tryed with
ActivityCompat.postponeEnterTransition(this);
before setContentView (I dont know if it's correct)
and
ActivityCompat.startPostponedEnterTransition(mContext);
in onBindViewHolder of the RecyclerView.
But it doesn't work...what can I do?
I solved by using
setEnterSharedElementCallback(new SharedElementCallback() {
#Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
sharedElements.put("profile_user_img", image);
super.onMapSharedElements(names, sharedElements);
}
});
in conjunction with postponeEnterTransition and startPostponedEnterTransition

how to show new image from previous activity index value in android

I am working on gridview but when I use all drawable images to show in gridview. I am getting out of memory error. Its obvious. Now I am looking for workaround condition. I am taking help from android hive example. I want to show grid images in thumbnail and on click on that image I want to send the index of that to new activity. And in new activity I have new drawables array I want to set image based on index value I got from previous activity.
FullImage Activity-
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
Here I want to assign new image in above code-
imageView.setImageResource(imageAdapter.mThumbIds[position]);
New Image array that I want to include in above code-
public Integer[] nImages = {
R.drawable.kri1, R.drawable.kri2,
R.drawable.kri3, R.drawable.kri4,
R.drawable.kri5, R.drawable.kri6,
R.drawable.kri7, R.drawable.kri8,
R.drawable.kri9
};
Edit-
Gridview showing mThumbIds images to show. On click on any image I want to show full image So I am passing index to full size activity. Now I want to use that index and image from new array nImages that I am creating in this activity. So index value from previous activity and new image from full size activity to open.
Please help me.
Get the position when you click on the GridView. Then pass that position in put extra like this:
intent.putExtra("imageResourceID", nImages[position]);
mContext.startActivity(intent);
Get that int in fullimage activity like this:
int position = getIntent().getIntExtra("imageResourceID", 0);
Now from new array nImages pass this position in new one like
nImages[position] and get that image resource and set in ImageView resource.
I assume you are sending position value to your full image activity, instead send Image resource ID.
For example:
intent.putExtra("imageResourceID", nImages[position]);
mContext.startActivity(intent);
And inside full Image activity, get the image resource Id and display it in a simple way by using image.setImageResource().

Set ImageView visible/invisible from activity's adapter

I have an activity with a listview over an imageview (sort of a background for the list view). When the arraylist of the activity's adapter is empty, I want to set the imageview visible, and when there is one or more object(s) on my arraylist I want to set the imageview invisible.
Whenever I change the arraylist, I am calling this from the adapter:
((MainActivity)context).checkForLogo();
and the checkForLogo method on MainActivity:
public void checkForLogo()
{
ImageView logoView = (ImageView)findViewById(R.id.imageViewLogo);
if (adapter.getCount() == 0)
logoView.setVisibility(View.VISIBLE);
else
logoView.setVisibility(View.INVISIBLE);
}
I am getting NullPointerException. When I set visibility directly from the activity then it works (example from onCreate). But that won't work for my problem since I need to do it every time the adapter's arraylist is modified.
Try to declare your logoView global and initialize it inside onCreate

android gallery scroll like view pager

I have used both Gallery and ViewPager widgets, I am fetching images from web service,
but I couldn't place them in the ViewPager, but in Gallery it is easy.
I used this tutorial for ViewPager
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
I want android Gallery widget scroll like the ViewPager scroll,
is this possible, how can I do this.
Here is a bare bones example of the instantiateItem() method for your PagerAdapter that would fill it with image views dynamically.
#Override
public Object instantiateItem( final View pager, final int position )
{
//Note: if you do not have a local reference to the context make one and
//set it to the context that gets passed in to the constructor.
//Another option might be to use pager.getContext() which is how its
//done in the tutorial that you linked.
ImageView mImg = new ImageView(context);
/*Code to dynamically set your image goes here.
Exactly what it will be is going to depend on
how your images are stored.
In this example it would be if the images
are on the SD card and have filenames
with incrementing numbers like: (img0.png, img1.png, img2.png etc...)*/
Bitmap mBitmap = BitmapFactory.decodeFile(
Environment.getExternalStorageDirectory() + "/img" + position + ".png");
mImg.setImageBitmap(mBitmap);
((ViewPager) collection).addView(mImg, 0);
return mImg;
}

Categories

Resources