android图片适配

个人笔记

服务器提供图片 高:560,宽:750,本地显示尺寸依照这个比例显示

需求一:图片宽度全屏显示,高度不变形自由显示

           由于如果仅仅靠布局文件的scaleType是不行的,这样仅仅会宽度顶着手机两边,高度却是不按照不变形自由显示的,所以还得靠代码来控制高度和宽度

代码:

布局item_wei_hui.xml:

 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/ll_picbookborder"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/iv_picbook"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>


            <ImageView
                android:id="@+id/iv_play"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@mipmap/btn_play_begin"/>
        </RelativeLayout>
java:

//
        llPicbookborder.setLayoutParams(new RelativeLayout.LayoutParams(UiUtils.getScreenWidth(), UiUtils.getPicShowHeight()));//设置图片外层框大小
        bitmapUtils.display(ivPicbook, weiHuiInfo.picbookURL, bigPicDisplayConfig);//加载图片
图片工具类UiUtils.java:

/**
     * 得到屏幕高度
     * @return px
     */
    public static int getScreenHeight() {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);// 取得窗口属性
        return dm.heightPixels;
    }

    /**
     * 图片高度(宽度全屏,得到图片的显示高度)
     * 通过比例公式:screenWidth/x = 750/560 (微绘图片规定尺寸)
     * @return
     */
    public static int getPicShowHeight() {
        int h = Math.round((float) (getScreenWidth()*560) / (float) 750);
        return h;
    }

需求二:宽度固定高度不变形显示

              和需求一一样,只不过宽度为固定的,需求一宽度是屏幕宽度

图片工具类UiUtils.java:

 /**
     * 图片高度(宽度输入,得到图片的显示高度)
     * 通过比例公式:screenWidth/x = 750/560 (微绘图片规定尺寸)
     * @return
     */
    public static int getPicShowHeight(int width) {
        int h = Math.round((float) (width*560) / (float) 750);
        return h;
    }

需求三:待定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Beluga_白鲸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值