个人笔记
服务器提供图片 高: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;
}
需求三:待定