Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have an answer for you!</p> <p>When any of the <code>setImage...</code> methods are called on <code>ImageView</code> in internally a layout pass is requested, for example, <code>setImageBitmap()</code> as above is defined as such</p> <pre><code>public void setImageBitmap(Bitmap bm) { setImageDrawable(new BitmapDrawable(mContext.getResources(), bm)); } </code></pre> <p>which calls</p> <pre><code>public void setImageDrawable(Drawable drawable) { if (mDrawable != drawable) { mResource = 0; mUri = null; updateDrawable(drawable); requestLayout(); //layout requested here! invalidate(); } } </code></pre> <p>which has the effect of the gallery 'snapping' to the center of the image thats currently closest to the center of the gallery.</p> <p>What I have done to prevent this is have the View thats loading into the Gallery have a explicit height and width (in <code>dip</code>s) and using an <code>ImageView</code> subclass that ignores layout requests. This works as the gallery still has a layout pass initially but does not bother doing this every time an image in the gallery changes, which I imagine would only need to happen if the gallery views had their width and height set to <code>WRAP_CONTENT</code>, which we dont. Note that as <code>invalidate()</code> is still called in <code>setImageDrawable()</code> the image will still be drawn when set.</p> <p>My very simple <code>ImageView</code> subclass below!</p> <pre><code>/** * This class is useful when loading images (say via a url or file cache) into * ImageView that are contained in dynamic views (Gallerys and ListViews for * example) The width and height should be set explicitly instead of using * wrap_content as any wrapping of content will not be triggered by the image * drawable or bitmap being set (which is normal behaviour for an ImageView) * */ public class ImageViewNoLayoutRefresh extends ImageView { public ImageViewNoLayoutRefresh(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ImageViewNoLayoutRefresh(Context context, AttributeSet attrs) { super(context, attrs); } public ImageViewNoLayoutRefresh(Context context) { super(context); } @Override public void requestLayout() { // do nothing - for this to work well this image view should have its dims // set explicitly } } </code></pre> <p>edit: i should mention that the onItemSelected approaches can also work, but as I needed to hook into that while flinging was taking place I came up with the above, which I think is more flexible approach</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload