Android手機圖片適配問題

來源:互聯網
上載者:User

標籤:this   ges   public   padding   http   type   todo   問題   技術分享   

需求:今天在做ListView的時候遇到一個問題,就是ListView中載入圖片的時候。有些圖片的大小比較大,所以會出現圖片顯示不充分的問題。

首先,再不做任何處理的情況下,大小是這樣的。寬度是WrapContent。

 

 

那麼怎麼解決呢??

1、首先FIX_XY,但是這樣會引起失真。

2、於是需要換個解決方案,那就是自訂View,重寫onMeasure方法。

自訂一個屬性:長寬高比。通過自己重寫onMeasure方法來解決。

具體解決代碼如下:

package com.itheima.googleplay_8.views;import com.itheima.googleplay_8.R;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.widget.FrameLayout;/** * @author  Administrator * @time     2015-7-18 下午2:10:54 * @des    TODO * * @version $Rev: 33 $ * @updateAuthor $Author: admin $ * @updateDate $Date: 2015-07-18 15:13:26 +0800 (星期六, 18 七月 2015) $ * @updateDes TODO */public class RatioLayout extends FrameLayout {    private float                mPicRatio        = 0f;                // 圖片的寬高比 2.43    private static final int    RELATIVE_WIDTH    = 0;                // 控制項寬度固定,已知圖片的寬高比,求控制項的高度    private static final int    RELATIVE_HEIGHT    = 1;                // 控制項高度固定,已知圖片的寬高比,求控制項的寬度    private int                    mRelative        = RELATIVE_WIDTH;    public RatioLayout(Context context) {        this(context, null);    }    public RatioLayout(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout);        mPicRatio = typedArray.getFloat(R.styleable.RatioLayout_picRatio, 0);        mRelative = typedArray.getInt(R.styleable.RatioLayout_relative, RELATIVE_WIDTH);        typedArray.recycle();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        // 控制項寬度固定,已知圖片的寬高比,求控制項的高度        int parentWidthMode = MeasureSpec.getMode(widthMeasureSpec);        // 控制項高度固定,已知圖片的寬高比,求控制項的寬度        int parentHeightMode = MeasureSpec.getMode(heightMeasureSpec);        if (parentWidthMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_WIDTH) {// 控制項寬度固定,已知圖片的寬高比,求控制項的高度            // 得到父容器的寬度            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);            // 得到孩子的寬度            int childWidth = parentWidth - getPaddingLeft() - getPaddingRight();            // 控制項的寬度/控制項的高度 = mPicRatio;            // 計算孩子的高度            int childHeight = (int) (childWidth / mPicRatio + .5f);            // 計算父容器的高度            int parentHeight = childHeight + getPaddingBottom() + getPaddingTop();            // 主動測繪孩子.固定孩子的大小            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);            measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);            // 設定自己的測試結果            setMeasuredDimension(parentWidth, parentHeight);        } else if (parentHeightMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_HEIGHT) {            // 控制項高度固定,已知圖片的寬高比,求控制項的寬度            // 得到父親的高度            int parentHeight = MeasureSpec.getSize(heightMeasureSpec);            // 得到孩子的高度            int childHeight = parentHeight - getPaddingBottom() - getPaddingTop();            // 控制項的寬度/控制項的高度 = mPicRatio;            // 計算控制項寬度            int childWidth = (int) (childHeight * mPicRatio + .5f);            // 得到父親的寬度            int parentWidth = childWidth + getPaddingRight() + getPaddingLeft();            // 主動測繪孩子.固定孩子的大小            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);            measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);            // 設定自己的測試結果            setMeasuredDimension(parentWidth, parentHeight);        } else {            super.onMeasure(widthMeasureSpec, heightMeasureSpec);        }    }}

 

Android手機圖片適配問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.