1. 程式人生 > >android圖片尺寸大小設定

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;
        }
    }
}