自适应网络加载的图片(按比例缩小)

网络下载的图片如果加载到屏幕上 由于其 尺寸不定  所以要对其进行统一管理,
一般是解析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;
	}


}



你可能感兴趣的:(android)