开发者

Creating ImageViews for a Gallery that fit the Gallery's height in Android

I want to create a Gallery that displays a very large number of images. To do this I have created an adapter class with an ImageView[3] and I making getView pass out ImageView[position%3]. I am also setting the adapter to populate the ImageView with large bitmaps that 开发者_开发知识库are loaded in asynchronously.

Since a fling may scroll faster than the device can load in images, I also have a much longer array of thumbnails (which I will also be eventually using for a thumbnail Gallery). If the large bitmap is unavailable, the ImageView is populated with the thumbnail, then repopulated with the large bitmap once it has loaded.

My problem is therefore depressingly trivial: the ImageView is not scaling the way I want it to. The desirable behaviour is for the ImageView to be the same height as its parent, but have the same aspect ratio as its content. However, if I populate the ImageView[] using this code:

    private void initializeStaticArrays() {
        //initialize envelope arrays
        for(int i=0; i<ENVELOPE_ARRAY_SIZE; ++i)
        {
            mEnvelopeViews[i] = new ImageView(mContext);
//make the views as wide as their content and as tall as their parent
            mEnvelopeViews[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
//then make them scale the contained image
            mEnvelopeViews[i].setScaleType(ImageView.ScaleType.FIT_CENTER);
            mEnvelopeIds[i] = Gallery.INVALID_POSITION;
            mBundles[i] = new BitmapFilePathBundle();
        }
        for(int i=0; i<THUMBNAIL_ARRAY_SIZE; ++i)
        {
            mThumbnailViews[i] = new ImageView(mContext);
            if(mThumbnailParams!=null) mThumbnailViews[i].setLayoutParams(new Gallery.LayoutParams(mThumbnailParams));
            mThumbnailViews[i].setScaleType(ImageView.ScaleType.FIT_CENTER);
            mThumbnailIds[i] = Gallery.INVALID_POSITION;
        }
    }

as soon as the Gallery calls getView() it crashes (please note I have called setImageBitmap elsewhere in code and that has definitely happened before getView() is called). I would imagine this is an arithmetic loop, something like trying to make the bitmap stretch to fit the view, which is trying to contain the bitmap, but I can't work out what combination of layout parameters would give the result I want.

So: is there a way to make an ImageView as tall as its parent, with its content zoomed to fit it with the correct aspect ratio, and with the width of the ImageView exactly matching the width of the content?

Edit: the crash was caused by a CastException from LayoutParams to Gallery.LayoutParams. Changing the appropriate line to this:

//make the views as wide as their content and as tall as their parent
            mEnvelopeViews[i].setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));

fixes the crash but still does not fix the zoom problem.


Unfortunately, in the absence of any automatic scaling solutions to this problem, I ended up manually sizing the views to specific dimensions, and then using Matrix objects to determine how each image fit those dimensions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜