创新实训——第八周1

    本次的工作主要工作是自定义了两个类:SystemUtils和ImageUtils,两个工具类分别用于处理页面信息(屏幕分辨率、高度等)以及处理图片。这两个类会在其他Java文件的功能实现中得以调用。

获取屏幕分辨率、状态栏高度、app显示高度、键盘高度:

/**
     * 获取屏幕分辨率高度
     * @param context 上下文
     * @return 屏幕高,与手机分辨率相关
     */
    public static int getScreenHeight(Context context){
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 或者状态栏高度
     * @param context 上下文
     * @return 状态栏高度
     */
    public static int getStatusHeight(Context context){
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height",
                "dimen","android");
        return resources.getDimensionPixelSize(resourceId);
    }

    /**
     * 获取 APP 显示高度
     * @param activity 当前活动状态的 Activity
     * @return AppHeight(include ActionBar) = Screen Height - StatusHeight
     */
    public static int getAppHeight(Activity activity){
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        return rect.height();
    }

    /**
     * 获取软键盘显示高度
     * @param activity 当前活动状态的 Activity
     * @return 软键盘高度 = 分辨率高 - 状态栏高 - 应用可视高,第一次获取,该值为787
     */
    public static int getKeyBoardHeight(Activity activity){
        return getScreenHeight(activity) - getStatusHeight(activity) - getAppHeight(activity);
    }

从文件获取bitmap并根据屏幕进行缩放、旋转得到的图片(90°、180°、270°)、将bitmap保存到本地等

/**
     * 从文件获取 bitmap ,并根据给定的显示宽高对 bitmap 进行缩放
     *
     * @param filePath 文件路径
     * @param height   需要显示的高度
     * @param width    需要显示的宽度
     * @return 缩放后的 bitmap,若获取失败,返回 null
     */
    public static Bitmap getBitmapFromFile(String filePath, int height, int width) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        float srcWidth = options.outWidth;
        float srcHeight = options.outHeight;
        if ((srcWidth == -1) || (srcHeight == -1))
            return null;
        int inSampleSize = 1;
        if (srcHeight > height || srcWidth > width) {
            if (srcWidth > srcHeight) {
                inSampleSize = Math.round(srcHeight / height);
            } else {
                inSampleSize = Math.round(srcWidth / width);
            }
        }

        options.inJustDecodeBounds = false;
        options.inSampleSize = inSampleSize;

        return BitmapFactory.decodeFile(filePath, options);
    }



    /**
     * 获取图片旋转角度
     *
     * @param path 图片路径
     * @return 旋转角度
     */
    public static int getBitmapDegree(String path) {
        int degree = 0;
        try {
          /*
            TAG_DATETIME时间日期
            TAG_FLASH闪光灯
            TAG_GPS_LATITUDE纬度
            TAG_GPS_LATITUDE_REF纬度参考
            TAG_GPS_LONGITUDE经度
            TAG_GPS_LONGITUDE_REF经度参考
            TAG_IMAGE_LENGTH图片长
            TAG_IMAGE_WIDTH图片宽
            TAG_MAKE设备制造商
            TAG_MODEL设备型号
            TAG_ORIENTATION方向
            TAG_WHITE_BALANCE白平衡 */

            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /**
     * 根据给定的角度,对 bitmap 进行旋转
     *
     * @param bitmap 原始 bitmap
     * @param degree 旋转角度
     * @return 旋转之后的 bitmap
     */
    public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {
        Bitmap returnBm;
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        try {
            returnBm = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (OutOfMemoryError e) {
            Log.e("ImageUtils", e.getMessage());
            return bitmap;
        }
        if (returnBm == null) {
            returnBm = bitmap;
        }
        if (bitmap != returnBm) {
            bitmap.recycle();
        }
        return returnBm;
    }




    public static String getFilePathFromUri(Context context,Uri uri) {
        String filePath = "";
        String[] filePathColumn = {MediaStore.MediaColumns.DATA};
        Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
        if (cursor != null){
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            filePath = cursor.getString(columnIndex);
            cursor.close();
        }
        return filePath;
    }

    /**
     * 将 bitmap 保存到本地 jpeg
     *
     * @param bitmap 图片 bitmap
     * @param path   保存路径(全路径)
     * @throws IOException
     */
    public static void saveBitmap2Jpg(Bitmap bitmap, String path) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        OutputStream outputStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    }

    /**
     * load bitmap by url into imageView with diskCache
     */
    public static void setImageByUrl(Context context, ImageView imageView, String url,int default_img) {
        setImageByString(context, imageView, url, default_img);
    }

    /**
     * load bitmap by file path into imageView with diskCache
     */
    public static void setImageByFile(Context context, ImageView imageView, String filePath,int default_img) {
        setImageByString(context, imageView, filePath, default_img);
    }



    private static void setImageByString(Context context, ImageView imageView, String path,int default_img) {
        Glide.with(context)
                .load(path)
                .asBitmap()
                .error(default_img)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值