Thumbnail does not fill_parent as expected

谁说胖子不能爱 提交于 2019-12-02 08:25:37

I think the image is now displayed at 100%. If you want to stretch it to the full width, add the following line to the urlImageView xml:

android:scaleType="fitCenter"

CenterInside does the following: Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

Check the documentation for more info on scaling.

Tenfour04

In your getView method, you either need to manually apply LayoutParams to the inflated view, or else use inflate(myResource, parent, false) instead of inflate(myResource, null) to prevent your xml layout params from being overwritten. In your case, myResource is R.layout.list_item_user_video.

Then you can use

if (convertView==null)
    convertView = 
        mInflater.inflate(R.layout.list_item_user_video, parent, false);

EDIT:

OK, it looks like ImageViews cannot in XML be automatically scaled to fit a certain width without distortion: https://stackoverflow.com/a/13925725/506796

Therefore, you must scale them after inflating the view. First you need to know the aspect ratio of the image (width/height). Since you seem to have a constant aspect ratio, you could just make this a static final float variable which will be much easier.

Then you need to know how wide the listview is in pixels. Again, you're in luck since your listview fills the width of the screen. (This is easier, because it would be tricky to measure the listview's width--you would have to do it after layout completes its first pass.) You can get the screen width in the adapter constructor with

screenWidth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getWidth()

You can also create a single set of layout params to apply to all of your image views. In your adapter constructor:

LinearLayout.LayoutParams imageParams = new LayoutParams(screenWidth, (int)(screenWidth/aspectRaio));

You can apply these params to the image view in getView.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!