自适应网络加载的图片(按比例缩小)
来源:互联网 发布:西安vb舞蹈工作室官网 编辑:程序博客网 时间:2024/04/30 15:55
网络下载的图片如果加载到屏幕上 由于其 尺寸不定 所以要对其进行统一管理,
一般是解析json先得到图片路径(附带图片的width,height)
对图片统一管理方案
float width
float height
width = 屏幕w
height = 图片h/(图片w/屏幕w)
imageView.setLayoutParams(new LinearLayout.LayoutParams(width, height));
实例
一般是解析json先得到图片路径(附带图片的width,height)
对图片统一管理方案
float width
float height
width = 屏幕w
height = 图片h/(图片w/屏幕w)
imageView.setLayoutParams(new LinearLayout.LayoutParams(width, height));
实例
private int maxWidth; private int maxHeight; maxWidth = ToolsUtil.getWidthInPx(context)-20; maxHeight = ToolsUtil.getHeightInPx(context) - ToolsUtil.getStatusHeight(context) - ToolsUtil.dip2px(context, 96);//屏幕高度-状态栏 float scale = (float)imageBean.getWidth()/(float)maxWidth; int height = (int)(imageBean.getHeight()/scale); if(height>maxHeight){ height = maxHeight; } imageView.setLayoutParams(new LinearLayout.LayoutParams(maxWidth, height));
ToolsUtil类
package com.jsu.whr;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;public class ToolsUtil {public static final int getHeightInPx(Context context) {final int height = context.getResources().getDisplayMetrics().heightPixels;return height;}public static final int getWidthInPx(Context context) {final int width = context.getResources().getDisplayMetrics().widthPixels;return width;}public static final int getHeightInDp(Context context) {final float height = context.getResources().getDisplayMetrics().heightPixels;int heightInDp = px2dip(context, height);return heightInDp;}public static final int getWidthInDp(Context context) {final float width = context.getResources().getDisplayMetrics().widthPixels;int widthInDp = px2dip(context, width);return widthInDp;}public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}public static int px2dip(Context context, float pxValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (pxValue / scale + 0.5f);}public static int px2sp(Context context, float pxValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (pxValue / scale + 0.5f);}public static int sp2px(Context context, float spValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (spValue * scale + 0.5f);}/** * 获得状态栏的高度 * * @param context * @return */public static int getStatusHeight(Context context) {int statusHeight = -1;try {Class<?> clazz = Class.forName("com.android.internal.R$dimen");Object object = clazz.newInstance();int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());statusHeight = context.getResources().getDimensionPixelSize(height);} catch (Exception e) {e.printStackTrace();}return statusHeight;}/** * 判断网络是否可用 * @param context * @return */public static boolean isNetworkAvailable(Context context) {ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (cm != null) {//如果仅仅是用来判断网络连接//则可以使用 cm.getActiveNetworkInfo().isAvailable();NetworkInfo[] info = cm.getAllNetworkInfo();if (info != null) {for (int i = 0; i < info.length; i++) {if (info[i].getState() == NetworkInfo.State.CONNECTED) {return true;}}}}return false;}}
0 0
- 自适应网络加载的图片(按比例缩小)
- 图片的按比例缩小.
- 按比例缩小图片类(无组件)
- 按比例缩小图片
- js按比例缩小图片
- 按比例缩小图片
- IOS 图片按比例缩小
- js技术图片加载并按比例缩小到合适大小
- 利用CSS按比例缩小图片
- 利用CSS按比例缩小图片
- css控制图片按比例缩小
- 图片不完全按比例自动缩小
- 将图片按比例缩小显示
- JS 图片按比例缩小
- js按比例放大缩小图片
- 指定区域的图片自动按比例缩小的js代码(防止页面被图片撑破)
- 关于图片按比例自适应缩放
- JS图片按比例加载
- WEB应用中的SESSION知多少?
- Android LayoutInflater详解
- java基础学习
- python pexpect spwan 方法
- 比较全的mysql错误代码和消息对照表
- 自适应网络加载的图片(按比例缩小)
- poj1284 Primitive Roots
- Glide深入浅出(一)——Glide vs Picasso
- 《算法入门竞赛经典》(第八章)8.1
- 导入项目运行报错:某些输入文件使用或覆盖了已过时的 API……
- android 图片压缩的三种方法,其实还有第四种就是2.2以后提供的压缩类
- 3DES 和 ECB CBC 加密方式
- C++如何通过栈实现超大整型数据的相加
- PHP[杂项函数] - define()