android图片尺寸大小设置

贴上代码,以后直接复制粘贴使用,不用再计算,再百度了!!

方法一:(按照图片尺寸设置、方法中viewRootBanner为图片或者装载图片的控件banner)

例如:750*286尺寸 的图片

//设置图片宽高比
float scale = (float) 750 / (float) 286;
int screenWidth;
//获取屏幕的宽度
WindowManager wm = (WindowManager) getBaseActivity().getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getSize(size);
screenWidth = size.x;
//计算BGABanner的应有高度
int viewHeight = Math.round(screenWidth / scale);
//设置BGABanner的宽高属性
ViewGroup.LayoutParams banner_params = viewRootBanner.getLayoutParams();
banner_params.width = screenWidth;
banner_params.height = viewHeight;
viewRootBanner.setLayoutParams(banner_params);

方法二:(按照图片比例设置,方法中viewRootBanner为图片或者装载图片的控件banner,Util为工具类,方法在下面)

例如:1.87:1 的图片比例

int width = Util.getPixbyPercent(1, getBaseActivity(), Util.Horizontal);
int heigh = (int) (width / 1.87);       //banner图片宽高比例1.87:1
ViewGroup.LayoutParams layoutParams = viewRootBanner.getLayoutParams();
layoutParams.width = width;
layoutParams.height = heigh;
viewRootBanner.setLayoutParams(layoutParams);

方法三:(imageView为图片,Util为工具类,方法在下面)

imageView = (ImageView) itemView.findViewById(R.id.class_list_image);
int fullwidth = Util.getPixbyPercent(1, (BaseActivity)mContext, Util.Horizontal);
int width = fullwidth - Util.dip2px(mContext, 32);
int height = 0;
height = (int) (width/2 * 1.47);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
imageView.setLayoutParams(params);

方法二、方法三中用到的方法:(里面的生成二维码方法需要依赖,依赖见下面第一行,如果有其他错误,用不到的可直接删掉,我这里就不删了)

implementation 'com.google.zxing:core:3.3.0'
public class Util {

    public static final int Horizontal = 0;
    public static final int Vertical = 1;
    private static DisplayMetrics dm = null;

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    private static DisplayMetrics getDisplayMetrics(Activity activity) {
        if (dm == null) {
            dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        }
        return dm;
    }

    /**
     * @param percent     百分比
     * @param activity    用于获取本机分辨率
     * @param orientation ZichanjiaUtil.Horizontal 返回横向百分比, 其他则 返回纵向百分比,
     * @return
     */
    public static int getPixbyPercent(double percent, Activity activity,
                                      int orientation) {
        DisplayMetrics dm = getDisplayMetrics(activity);
        int screenW = dm.widthPixels; // 获取分辨率宽度
        int screenH = dm.heightPixels; // 获取分辨率高度
        return Horizontal == orientation ? (int) (screenW * percent)
                : (int) (screenH * percent);
    }

    /**
     * 隐藏输入法
     *
     * @param context
     */
    public static void hideInputMethod(Context context)
    {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive())
        {
            imm.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_SHOWN, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 显示输入法
     *
     * @param context
     * @param view
     * @param requestFocus
     */
    public static void showInputMethod(Context context, View view, boolean requestFocus)
    {
        if (requestFocus)
        {
            view.requestFocus();
        }
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    public static int getIntFromString(String content, int defaultValue)
    {
        if (!TextUtils.isEmpty(content))
        {
            try
            {
                return Integer.parseInt(content);
            } catch (Exception e)
            {
                return defaultValue;
            }
        } else
        {
            return defaultValue;
        }
    }

    /**
     * 将毫秒转化成 yyyy-MM-dd HH:mm:ss的字符串
     * @param milSec 毫秒
     * @return
     */
    public static String milToStringlong(Long milSec)
    {
        return milToStringlong(milSec, "yyyy-MM-dd HH:mm:ss");
    }

    public static String milToStringlong(Long milSec, String pattern)
    {
        Date dateNow = new Date(milSec);
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        String dateStrLong = formatter.format(dateNow);
        return dateStrLong;
    }

    /**
     * 将字符串的yyyy-MM-dd HH:mm:ss 转化为毫秒
     * @param dateStrlong yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long stringLongToMil(String dateStrlong)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date date = formatter.parse(dateStrlong);

            long milSec = date.getTime();
            return milSec;
        } catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 计算收益
     * @param bidMoney
     * @param investModel
     * @return
     */
    public static double cacleEarningsMoney(double bidMoney, InvestModel investModel){
        double earningsMoney = 0;
        earningsMoney = bidMoney * Double.parseDouble(investModel.getRate()) / 100 / 360 * Double.parseDouble(investModel.getRepay_time());
        return earningsMoney;
    }

    public static double cacleEarningsMoney(double bidMoney, double rate, double repayTime){
        double earningsMoney = 0;
        earningsMoney = bidMoney * rate / 100 / 360 * repayTime;
        return earningsMoney;
    }

    /**
     * 获取版本号
     * @return 当前应用的版本号
     */
    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "未知版本号";
        }
    }

    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 生成二维码
     * @param text
     * @param size
     * @return
     */
    public static Bitmap createQRCode(String text, int size) {
        try {
            Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            hints.put(EncodeHintType.MARGIN, 0);
            BitMatrix bitMatrix = new QRCodeWriter().encode(text,
                    BarcodeFormat.QR_CODE, size, size, hints);
            int[] pixels = new int[size * size];
            for (int y = 0; y < size; y++) {
                for (int x = 0; x < size; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * size + x] = 0xff000000;
                    } else {
                        pixels[y * size + x] = 0xffffffff;
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(size, size,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
            return null;
        }
    }
}

 

 

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值