How can I add the Progress dialog in ImageView until I loading image from server in android. When image download complete then display the image.
try {
cachedImage = imageLoader.loadImage(mTopicList.getTopicImage(j),
new ImageLoadedListener() {
public void imageLoaded(Bitmap imageBitmap)
{
imageView.setImageBitmap(imageBitmap);
imageView.invalidate();
}
});
} catch (Exception e) {
e.printStackTrace();
}
Approach which I followed to do this. Use the Frame Layout and have a ImageView and ProgressBar inside it. I've used the Universal Image Loader Library, which have callback once image has been downloaded. Initially show the ProgressBar and hide the ImageView to invisible. Once image has been downloaded then hide the ProgressBar and show the image in the ImageView.
Initially
imageView.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
Load your image now. After loading it, call this:
progressBar.setVisibility(View.INVISIBLE);
imageView.setVisibility(View.VISIBLE);
Hope this help to you all!
Related
I am using RecyclerView and volley's NetworkImageView to render images once they are downloaded. The view consists of an author image, some text fields and a picture. Following is the code snippet to populate the view:
// vh is the viewholder
vh.picture.setDefaultImageResId(R.drawable.default_image);
vh.picture.setImageUrl(post.getImageUrl(), mImageLoader);
The problem I am facing is when scrolling, out of say 20 images, mostly ~18 show up. I see from the logs that all images are downloaded and are in the cache, but some are not rendered. Even the default image is not displayed for those views. If the view is invalidated (scroll up and down again), the images show up.
Funny thing is, for the views where the picture is not displayed, even the author pic is not displayed, even if I can see the same author pic in a post just above it. Its as if the entire view has a problem displaying images.
Is there any way to call invalidate() or postInvalidate() on NetworkImageView manually once the images are downloaded? Or any other ideas?
This was also asked here. I finally got around to this problem by not using NetworkImageView at all. I started using the regualar ImageView, am still fetching the images through volley through a custom image request and onResponse() applying the image on the view. This seems to work pretty well.
public void getImage(String url, final ImageView v) {
if (TextUtils.isEmpty(url)) return; // don't fetch a null url
ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap response) {
v.setImageBitmap(response);
}
}, 0, 0, null, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error- " + error.getMessage());
}
});
mRequestQueue.addToRequestQueue(imageRequest);
}
I have implemented a shared element activity transition animation between MainActivity (GridView) and DetailActivity. When item is clicked, the image in Grid item will zoom-in to become the background image in DetailActivity. I have made the transition very smooth by saving the image on the MainActivity into a file, then use it as placeholder image in DetailActivity before the higher quality image is downloaded via picasso. When picasso finished its task the higher quality image will replace the placeholder one very neatly.
Code from onCreateView() in DetailActivityFragment.java
ImageView iv = (ImageView) mRootview.findViewById(R.id.movie_poster);
try {
// Prepare "InterimPoster" jpeg file to be placeholder image
Bitmap bitmap = BitmapFactory.decodeStream(c.openFileInput("InterimPoster"));
Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
.placeholder(new BitmapDrawable(getResources(), bitmap))
.fit()
.centerCrop()
.noFade()
.into(iv);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
As for animation, I implement it like in the link here step 1-3 https://guides.codepath.com/android/Shared-Element-Activity-Transition
However, sometimes there's flicker when the higher quality image finished download before the transition animation is completed. The zooming-in image will be replaced with new image while moving which is an unpleasant animation.
So I wonder how can I fix this flicker? One thing I can think of is to delay the image download because I already have lower quality image as a placeholder. How can I do that?
Here the video.
Usually, on my test device it's smooth 80% of the time, but luckily in this video it flicker most of the time.
what's animation ? load image while animation end
TranslateAnimation translateAnimation = new TranslateAnimation(0,1.5f,0,1.5f);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//load image when animation end
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
and
LayoutTransition.TransitionListener transitionListener = new LayoutTransition.TransitionListener() {
#Override
public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
}
#Override
public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
//load image when animation end
}
};
Now I have found a way to delay Picasso image loading (or any other task). It's very simple really with the use of Handler and Runnable.
Now my code looks like this. The image replacement is pretty smooth in any case now.
// Set Background Poster in Try-catch in case file cannot be open
try {
// Get/Prepare "InterimPoster" jpeg file to be placeholder image
final ImageView iv = (ImageView) mRootview.findViewById(R.id.movie_poster);
final Bitmap bitmap = BitmapFactory.decodeStream(c.openFileInput("InterimPoster"));
final Drawable lowResPoster = new BitmapDrawable(getResources(), bitmap);
iv.setImageDrawable(lowResPoster);
// Download higher resolution image with 0.8 sec delay to avoid load complete before
// animation finishes (causing some flicker/overflow image problem).
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
// still need placeholder here otherwise will flash of white image
.placeholder(lowResPoster)
.error(lowResPoster)
.fit()
.centerCrop()
.noFade() // without this image replacement will not be smooth
.into(iv);
}
};
handler.postDelayed(runnable, 800);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Try the below code:
First use picasso normally to load placeholder into the imageview
iv.setImageBitmap(bitmap);
Then use SharedElement Listener like this
Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();sharedElementEnterTransition.addListener(new TransitionListenerAdapter() {
#Override
public void onTransitionEnd(android.support.transition.Transition transition) {
super.onTransitionEnd(transition);
Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
.placeholder(new BitmapDrawable(getResources(), bitmap))
.fit()
.centerCrop()
.noFade()
.into(iv);
}
});
To delay some process, you can use Handler().
Example.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// write your codes here.. this will delay 2 seconds...
startActivity(new Intent(this,MySecondActivity.class);
}
},2000);
I'm using Picasso class/library to load an image from an url and display the image to a ImageView. Is it possible for me to set the imageview loaded by the picasso image loader from an url as the background image of a linearlayout programmatically?
I've already found this issue - might be useful for you:
How do i set background image with picasso in code
According to that, Use callback of Picasso
Picasso.with(getActivity()).load(R.drawable.table_background).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})
Read also
Set background resource using Picasso
but there would you find the same solution.
I'm trying to load in part of a tall image into a scrollable imageview (imageview inside a scrollview).
It works with smaller images but if the images is bigger than 2048x2048 the app will crash due to open gl out of memory.
I can load part of the image with this:
private Bitmap decodeBitmapRegion(InputStream in, Rect region) {
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(in, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
region.right = decoder.getWidth();
Bitmap reg = decoder.decodeRegion(region, null);
return reg;
}
Where the rectangle can be something like (0,0,DisplayWidth,800).
That works in most cases since i'm using the imageview to let the user crop a part of the image and save it as a coveriamge.The imageview has the same width as the display and the height of 400 dp.
So the image the user saves will be the same width as the orginal but only 400 dp's tall.
Is there anyway to make it possible for the user to scroll all the way to the bottom of the orginal image?
How does webview load images that are bigger than 2048x2048?
Here's one thought.
Imagine your image broken into rows of bands. Use a ListView to display the bands. A ListView uses an adapter. In adapter.getView(), you use the decoder to load the appropriate band.
The ListView already does "recycling". So, it more-or-less only holds enough rows to fill the screen.
This technique should work well if your images aren't too wide. If they are, and it's not tall enough to occupy more than one screen height, you'll still have the same memory problems. A more complicated solution would be to use some sort of grid view, but then you'll have to do your own view recycling. Not too difficult, but harder than using ListView.
I managed to get the functionality I wanted. But i don't think this is the best solution?
I loaded the image into a webview and then i took a screenshot of the visible part of the webview. After that i can save that bitmap and do whatever i want to it.
But there must be a better way to do this than to use a webview to show the original image?
Anyway here is what I did:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
imgView = (ImageView) findViewById(R.id.imageview);
webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/test.jpg");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgView.setImageBitmap(getBitmapForVisibleRegion(webView));
}
});
}
public static Bitmap getBitmapForVisibleRegion(WebView webview) {
Bitmap returnedBitmap = null;
webview.setDrawingCacheEnabled(true);
returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
return returnedBitmap;
}
I have an app that contains a picture gallery when one of the items in the gallery is pressed, the item clicked views as an imageview fullscreen. What I am then trying to do is allow the user to select whether to download the imageview to the phone or set it as a background...is this possible? How would I go about this? Thanks!
you can use WallpaperManager Service to the the system wallpaper.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourcId);
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}