开发者

Gallery item width vs gallery width

I have a Gallery with a set of images that was downloaded at run time.

I followed the example here: http://www.anddev.org/a_androidwidgetgallery_-_example-t332.html

but instead of using

i.setImageResource(this.myImageIds[position]);

I used

i.setImageBitmap(bitmaps.get(position));

This doesn't fill the entire width of the screen, only as much as the width specified here:

i.setLayoutParams(new Galler开发者_如何学Cy.LayoutParams(150, 150)); 

When I increase this number, the item scales with it instead of showing more images per the example. I've even tried to scale the images before adding them to the set. Not sure what I'm missing, or where other examples of this might be. Any help would be great, Thanks.


It seems that you can set the layout's width as the image's real size. or compute a scale factor by some logic. such as you can decrease the image's width when the image is far away the gallery center.

public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(this.myContext);

        i.setImageResource(this.myImageIds[position]);
        /* Image should be scaled as width/height are set. */
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        /* Set the Width/Height of the ImageView. */
        int selectedPosition = ((Gallery) parent).getSelectedItemPosition();

        float scale = getScale(position - selected);

            int width = Math.round(IMAGE_WIDTH * scale);

            int height = Math.round(IMAGE_HEIGHT * scale);

            i.setLayoutParams(new Gallery.LayoutParams(width, height));

        return i;
    }


In your layout xml file be sure to set android:layout_width="fill_parent", like this

<Gallery 
            android:id="@+id/gallery"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dip"
                        android:layout_gravity="center_horizontal|bottom"
                        android:padding="1dip"
                        android:background="#AA000000"
            />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜