自定义适配屏幕的ImageView

实现了按UI需要的产品图片适配。

RatioImageView.java

/**
 * 类说明:一个能保持比例的 ImageView
 *         暂时只支持维持宽度适应高度
 * Author: gaobaiq
 * Date: 2016/8/12 9:17
 */
public class RatioImageView extends ImageView {

    private float mRatio = 0f;
    private int mWidth;

    private static final String TARGET_SCREEN_WIDTH = "targer_screen_width";

    private Context context;

    public RatioImageView(Context context) {
        super(context);
        this.context = context;
    }

    public RatioImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getAttrs(context, attrs);
    }

    public RatioImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttrs(context, attrs);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioImageView);
            mRatio = mTypedArray.getFloat(R.styleable.RatioImageView_ratio, 0f);
            int width = mTypedArray.getInteger(R.styleable.RatioImageView_width, 0);
            calculateWidth(width);
            mTypedArray.recycle();
        }
    }

    public void setRatio(float ratio) {
        mRatio = ratio;
        invalidate();
    }

    public float getRatio() {
        return mRatio;
    }

    private void calculateWidth(int width) {
        int targetScreenWidth = 0;
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            targetScreenWidth = info.metaData.getInt(TARGET_SCREEN_WIDTH);
        } catch (Exception e) {
        }

        if (width > 0) {
            if (targetScreenWidth > 0) {
                mWidth = width * ScreenUtils.getScreenWidth(context) / targetScreenWidth;
            } else {
                mWidth = width;
            }
        }
    }

    public void setWidth(int width) {
        calculateWidth(width);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mRatio > 0 || mWidth > 0) {

            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);

            if (mWidth > 0) {
                width = mWidth;
            }

            // 现在只支持固定宽度
            if (width > 0 && mRatio > 0) {
                height = (int) ((float) width * mRatio);
            }
            setMeasuredDimension(width, height);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

 

attr.xml

<!-- 自定义ImageView 固定宽高比 -->
    <declare-styleable name="RatioImageView">
        <!--比例-->
        <attr name="ratio" format="float" />
        <attr name="width" format="integer" />
    </declare-styleable>

在manifest文件的application节点上添加图片的宽度(px)

<meta-data
    android:name="targer_screen_width"
    android:value="720"/>

 

布局文件

<com.app.gaobaiq.customview.RatioImageView
             xmlns:myapp="http://schemas.android.com/apk/res-auto"
             android:id="@+id/img_user"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:src="@mipmap/ic_launcher"
             myapp:ratio="1.0"
             myapp:width="100"/>  <!-- 图片上得到的尺寸px -->             

 

转载于:https://www.cnblogs.com/gaobaiq/p/5763746.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值