emilsjolander / android-FlipView

A small, easy to use android library for implementing flipping between views as seen in the popular Flipboard application
Apache License 2.0
924 stars 273 forks source link

Center Cropped ImageView is Expanding on Page Turn #14

Closed btate closed 11 years ago

btate commented 11 years ago

I have a bunch of image views on my flipview page that are center cropped. As I'm turning the page the image view is expanding. I have a horizontal page turn and the image is expanding upward. The image being exposed while it is expanding is the part of the image being cropped out.

This doesn't happen with the squares that contain web views or the square that contains image views with bitmaps not larger than their container.

Any thoughts on that? Library is excellent btw. Only issue I've had.

JackDanyels commented 11 years ago

you have to set the scale type before loading the image into the imageview. Try out the UniversalimageLoader and the Imageloadinglistener.

btate commented 11 years ago

I ended up creating a self cropping image view that crops itself on layout so there is no extra content there.

public class CroppingImageView extends ImageView {

    public CroppingImageView(Context context){
        super(context);
    }

    protected void onLayout (boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right, bottom);

        crop();
    }

    public void crop(){

        boolean cacheEnabled = isDrawingCacheEnabled();
        setDrawingCacheEnabled(true);

        buildDrawingCache(false);
        Bitmap cached = getDrawingCache();
        Bitmap croppedBitmap = Bitmap.createBitmap(cached);
        destroyDrawingCache();
        cached.recycle();
        cached = null;
        setDrawingCacheEnabled(cacheEnabled);

        setImageBitmap(null);
        setImageBitmap(croppedBitmap);

    }
}
emilsjolander commented 11 years ago

i noticed the exact same thing yesterday. Turning on show layout bounds in develoepr settings reveals that the image is being drawn outside of the view bounds. So my guess is that this is a problem with ImageView+centerCrop being drawn on rotated canvases (centerCrop does not actually crop the underlying bitmap, just draws in in a cropped way). I ended up cropping the bitmap manually instead.