I have a task to show 5 images periodically. I'm using ViewFlipper for this. The images are loaded dynamically from gallery or camera.
I got stuck with modifying the image source at runtime i.e user should be able to change the current image he is viewing.
I can get the current index, now I want to change the image source from gallery at run time
// code to initialise the flipper
vf_profile_slide = (ViewFlipper) this.findViewById(R.id.profile_slide_vf);
int gallery_grid_Images[]={R.drawable.image1,R.drawable.image2};
for(int i=0;i<gallery_grid_Images.length;i++)
{
setFlipperImage(gallery_grid_Images[i]);
}
/* tempBmp = BitmapFactory.decodeFile(imgPath);
addNewImageToFlipper(mContext, tempBmp);
*/
vf_profile_slide.setAutoStart(true);
vf_profile_slide.setFlipInterval(5000);
vf_profile_slide.startFlipping();
//On button click i m feteching the image index
PROFILE_INDEX = vf_profile_slide.getDisplayedChild();
//calling this function...
private void addImageToFlipperAt(Context mContext,int index,Bitmap bm)
{
ImageView iv_new = (ImageView) findViewById(vf_profile_slide.getChildAt(index).getId());
iv_new.setImageBitmap(bm);
vf_profile_slide.addView(iv_new,index);
}
the new image is added successfully.... but i want the current image at that index gone... that is not happening .... tell me where i m going wrong
Is this possible?
Related
I'm new to picasso.using it i want to dynamically fetch images and be able to update the images whenever some new link has been updated. currently i'm only able to do this for a single Image. the code that i'm using is :
picasso.with(this).load(url).into(image1)
where url is the url to the image and image1 is an imageview. i want to diaplay 5 images into 5 different imageviews, iteratively. how can i do that ?
also i wan to delete the cached images of picasso, so that i can update it with newer images. any help would be appreciated.
In your xml just add only this,
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ViewFlipper>
lets Say your URL Images Array like this.
String ImgAry[] = {"url1","url2","url3","url4","url5"}
In your onCreate()
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
for(int i=0;i<ImgAry.length;i++)
{
// create dynamic image view and add them to ViewFlipper
setImageInFlipr(ImgAry[i]);
}
method in Your Activity file
private void setImageInFlipr(String imgUrl) {
ImageView image = new ImageView(getApplicationContext());
picasso.with(this).load(imgUrl).into(image);
viewFlipper.addView(image);
}
private void setImageInFlipr(String imgUrl) {
ImageView image = new ImageView(getApplicationContext());
picasso.get().load(imgUrl).into(image);
viewFlipper.addView(image);
}
I am overlapping couple of view's dynamically in my app over a bitmap. I would like to delete those view's before saving the bitmap to gallery. Below in the function which adds view's on my bitmap
public void add()
{
relLayout.addView(newRect);
relLayout.addView(newSpeech);
relLayout.addView(editImgv);
relLayout.addView(resizeImgv);
}
There is a button on pressing it the above add() function gets called and all those views get added over my bitmap again.
Before saving the bitmap I would like to delete all editImgv and resizeImgv which were added
over my bitmap.
Any ideas on how to do it?
Thanks in advance :)
I have resolved it by using Vector arrays - I placed all the 'resizeImgv' and 'editImgv' image views which I am adding dynamically in a vector array of type 'ImageView' and when I am about to save I am just setting their visibility to 'GONE' one by one. It was a simple solution in the end :)
//Global
Vector<ImageView> imgv = new Vector<ImageView>();
....
//Adding views to my vector array
public void setImageViewArray(ImageView imgview){
imgv.add(imgview);
}
.....
//when I am about to save
for(int i = 0; i < imgv.size(); i++ ){
if(imgv.get(i).getVisibility() == View.VISIBLE){
imgv.get(i).setVisibility(View.GONE);
}
}
one again facing a "strange" problem.
Im using a Viewpager to display 2 separate images (Viewpager contains just a layout and 2 imageviews).
The concept would be, display a low resoltion image from local file cache (immediately) and load the high-res picutre meanwhile and show it.
the problem is: only using low res pictures, pictures are showing immediately and everything perfect, but as soon as high-res pictures are enabled (to be shown),
if the user swipes really fast, the screen stays black for "a short time" (500ms to 1,5s) and low resolution images are never displayed.
just the high-res pictures..
maybe anyone faced a similar problem, any assistance appriciated :) thank you!
ViewPager code:
/**
* Create and add a new page of the image at the given position.
*
* #param collection
* #param position
*/
#Override
public Object instantiateItem (View collection, final int position) {
Log.v(TAG, "instantiateItem: pos: " +position);
final Context context = collection.getContext();
RelativeLayout layout = new RelativeLayout(context);
ImageViewTouch imageView = new ImageViewTouch(context);
ImageViewTouch imageView2 = new ImageViewTouch(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
layout.addView(imageView, lp);
layout.addView(imageView2, lp);
imageView.setOnSingleTapConfirmedListener((OnImageViewSingleTapConfirmedListener) context);
imageView2.setOnSingleTapConfirmedListener((OnImageViewSingleTapConfirmedListener) context);
imageView2.setVisibility(View.GONE);
if (position == restorePageNumber) {
loadBitmap(context, position, imageView, imageView2, restoreZoomLevel);
Log.w(TAG, "zoom level regocnized for pos: " + position + " resetting...");
restorePageNumber = Constants.INVALID_INT;
restoreZoomLevel = Constants.INVALID_LONG;
} else {
loadBitmap(context, position, imageView, imageView2, Constants.INVALID_LONG);
}
imageView.setFitToScreen(true);
imageView2.setFitToScreen(true);
activePages.put(position, imageView2);
((ViewPager) collection).addView(layout);
return layout;
}
protected void loadBitmap (Context context, int position,
ImageViewTouch imageView, ImageViewTouch imageView2,
float restoreZoomLevel) {
Photo photo = getPhotoAtPosition(position);
Log.v(TAG, "loading photo. pos: " + position + " id: " + photo.getId());
// show small image first
if (!(photo instanceof CameraPhoto)) {
StorageManager.retrieveBitmapBackgroundWithImageview(context, photo,
Photo.SIZE_SMALL, imageView, true, Constants.INVALID_LONG);
}
// afterwards replace with big image
StorageManager.retrieveBitmapBackgroundWithImageview(context, photo,
Photo.SIZE_LARGE, imageView2, true, restoreZoomLevel);
}
in Those methods (retrieveBitmapBackgroundWithImageview) Images are loaded in Background, and afterwards set to the imageview.
It seems that it has some problem with setting the large bitmap.
Even if the Imageview with the large bitmap stays hidden (View.GONE), and only local cache images are shown, the ViewPager stays black for some "time" (as above, 500ms to 1.5s) on loading pages, if swiping fast :)
thx :)
If you mean swiping fast to alot of pages it just has problems with loading all those views. Do you cancel/interrupt the loadBitmap tasks that are out of the range of the ViewPager (not in view or cache)?
And, do you handle concurrency in threads? If you always want to load the pages that are in the ViewPager's view or cache first, you should Override Comparator and let it compare on the page. The comparator can be used in a PriorityBlockingQueue which can be used in a Executor
Currently my code displays a single image from a file path (in SDCard). This is in onCreate() method for now:
ImageView imgView01 = (ImageView) findViewById(R.id.imageView1);
File dir = new File("/sdcard/WallpapersHD/");
File file[]=dir.listFiles();
for (int i=0;i<file.length;i++) {
Drawable d = (Drawable) Drawable.createFromPath(file[i].toString());
imgView01.setImageDrawable(d);
}
I want to display all the images in that particular folder one after the other using a time delay of say 5 seconds. If I can create a new drawable for each image in the folder, How do I do it? and how do I change the image in ImageView to set that drawable's path?
You could use an ImageSwitcher for convenience, and then do:
imageSwitcher.postDelayed(
new Runnable() {
#Override
public void run() {
i++;
imageSwitcher.setImageURI(Uri.fromFile(file[i]));
imageSwitcher.postDelayed(this, millisBetweenImages);
}
},
millisBetweenImages);
it also has a setImageDrawable-method if you want to keep your images as Drawables.
Create a drawable for each image, and then keep changing the image in your ImageView, perhaps in a Handler or TimerTask.
I would like to change an image each time the user touches the screen. I would like to have user tap the screen and the image changes each time and at the 5th touch it would loop back to the original image with added text.
I have looked at
How would I set an ImageButton to change only when my finger is touching it
I have tried both IUevents and StateListdrawable. I couldn't figure out how to actually change the image after each touch (anywhere in the screen).
And why can't you use the normal ImageView? Do this:
ArrayList<Integer> picList = new ArrayList<Integer>();
// populate the list with the int value of the pictures in drawable.
picList.add(R.drawable.apple);
picList.add(R.drawable.horse);
//etc
int touches = 0;
int loop = 0;
ImageView picture = (ImageView) findViewById(R.id.your_picture);
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (touches < 4)
v.setBackgroundDrawable(picList.get(touches));
touches++;
} else {
touches = 0;
loop = 1;
v.setBackgroundDrawable(picList.get(touches));
}
if (loop = 1){
// Create a TextView and set the text or make it in the xml with Visible="Gone" and make is visible here.
}
});
you need to create the Custom class which extends the ImageView now display this imageview to whole screen.
> First store the path/location of images into an array/list etc.
> In custom class implement the onTouch event.
> create the counter object in this custom class.
> now in onTouch check in down event that check the counter value with array size if counter==array.length()-1 then set counter as 0 otherwise increment in counter object.
> now get the path of the image and set into the imageview as background