Android 图片绘制裁剪

2018-12-29  本文已影响9人  风___________
 // 将图片放大至屏幕,然后裁剪制定高度的图片
    public static Bitmap DrawSmailImage(Context context, int resourceID, int drawHeight) {
        Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(), resourceID);
        Bitmap drawBitmap = null;
        // 图片放大到屏幕大小
        if (imageBitmap != null) {
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            int screenWidth = manager.getDefaultDisplay().getWidth();
            int screenHeight = manager.getDefaultDisplay().getHeight();
            int imageWidth = imageBitmap.getWidth();
            int imageHeight = imageBitmap.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) imageWidth) /  screenWidth;
            float scaleHeight = ((float) imageHeight ) / screenHeight;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            drawBitmap = Bitmap.createBitmap(imageBitmap, 0, 0, screenWidth, drawHeight, matrix, true);
        }
        return drawBitmap;
    }

    // 将图片放大至屏幕
    public static Bitmap DrawScreenImage(Context context, int resourceID) {
        Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(), resourceID);
        Bitmap drawBitmap = null;
        // 图片放大到屏幕大小
        if (imageBitmap != null) {
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            int screenWidth = manager.getDefaultDisplay().getWidth();
            int screenHeight = manager.getDefaultDisplay().getHeight();
            int imageWidth = imageBitmap.getWidth();
            int imageHeight = imageBitmap.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) imageWidth) /  screenWidth;
            float scaleHeight = ((float) imageHeight ) / screenHeight;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            drawBitmap = Bitmap.createBitmap(imageBitmap, 0, 0, screenWidth, screenHeight, matrix, true);
        }
        return drawBitmap;
    }
上一篇 下一篇

猜你喜欢

热点阅读