我是靠谱客的博主 爱笑啤酒,这篇文章主要介绍根据屏幕的尺寸决定图片的显示大小,现在分享给大家,希望可以做个参考。

在app中我们一般都会下载图片,那么我们如何决定图片的大小呢,给图片固定一样的高度、宽度是不合理的,那么我们需要根据屏幕的尺寸来决定图片的大小。

首先,我们需要计算屏幕尺寸,我们可以写一个工具类,用的时候直接调用。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class ImageSizeUtil { public static SharePreferenceUtil spUtil; public static void getScreenSize(Context context) { WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); int width = windowManager.getDefaultDisplay().getWidth(); int height = windowManager.getDefaultDisplay().getHeight(); if (spUtil == null) { spUtil = new SharePreferenceUtil(context); } spUtil.put(ConfigConts.ScreenWidth, width); spUtil.put(ConfigConts.ScreenHight, height); } /** * @param context * @return 图片的宽是屏幕宽的三分之一 */ public static int setImageWidth(Context context, int i) { if (spUtil == null) { getScreenSize(context); } int imageWidth = spUtil.getInt(ConfigConts.ScreenWidth, 0); return imageWidth / i; } /** * @param * @return 根据图片的高是图片的宽的2/3 */ public static int setImageHight(int width) { return (width * 2) / 3; } /** * @param * @return 根据图片的宽和高一致 */ public static int setImageHightSampleWidth(int width) { return width; } }

在adapter中或者需要的地方调用

复制代码
1
2
3
4
5
6
7
8
9
public MainGrideAdapter(Context context, List<GoodsBean> mData) { this.mData = mData; this.context = context; //设置图片的大小 itemWidth = ImageSizeUtil.setImageWidth(context, 2); // imgParams = new RelativeLayout.LayoutParams(imageWidth, ImageSizeUtil.setImageHight(imageWidth)); } holder.mFirmOrderItemDestImg.setLayoutParams(new RelativeLayout.LayoutParams(imgWidth, imgWidth));

这样就可以根据屏幕尺寸设置图片大小了!

图片工具类
SharePreferenceUtil

大家有需要的可以自己下载!

最后

以上就是爱笑啤酒最近收集整理的关于根据屏幕的尺寸决定图片的显示大小的全部内容,更多相关根据屏幕内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部