Android 之 图片变换

本文详细介绍了Android系统中的Bitmap类及其在图像处理中的应用,包括资源加载、文件解码、流解码、图片缩放、旋转、剪裁和保存等功能,并通过实例代码展示了如何使用Bitmap实现图片变换,旨在帮助开发者更高效地处理Android平台上的图像资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说到图片,第一反映就是bitmap,那就先来认识一下bitmap

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件

Bitmap实现在android.graphics包中。但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化。这必然是 某个辅助类提供了创建Bitmap的接口,而这个类的实现通过JNI接口来实例化Bitmap的,这个类就是BitmapFactory

decode

方法比较多,我们暂时只考虑比较常用的几个,其余的看一下源码的注解就知道

  1. decodeResource
  2. decodeFile
  3. decodeStream

解码资源,解码文件,解码流,根据它的名字就知道了所要加载的资源,例如sd卡的文件解码可是使用decodeFile方法,网络上的图片可以使用decodeStream方法,资源文件中的图片可以使用decodeResource方法,当然这个不是固定唯一的,因为前面两个方法都是通过对第三个方法的包装实现的

为了防止图片OOM,它还提供了Options这个参数

  1. inJustDecodeBounds
  2. inSampleSize
  3. outWidth
  4. outHeight
  5. inPreferredConfig
  6. outMimeType
  7. 如果inJustDecodeBounds设置为true,允许查询位图,但是不分配内存返回值为null,但是可以读取图片的尺寸和类型信息。
    inSampleSize的值在解析图片为Bitmap时在长宽两个方向上像素缩小的倍数,默认值和最小值为1(当小于1时,按照1处理),且在大于1时,该值只能为2的幂(当不为2的幂时,解码器会取与该值最接近的2的幂)
    inPreferredConfig默认为ARGB_8888,当然也可以赋其他值,例如:ALPHA_8, RGB_565, ARGB_4444,四种像素类型,每个像素占用所占的字节数不同

下面来实现图片的变换

缩小:

public void scalePic(int reqWidth,int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.mipmap.demo, options);
        options.inSampleSize = PhotoUtil.calculateInSampleSize(options, reqWidth,reqHeight);
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.demo, options);

        postInvalidate();
    }
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

主要使用通过属性inSampleSize实现图片缩小,当然这个方法不是为了实现缩放的,我们这里只是说明一下,它的主要目的还是预防OOM,当加载一张图片时,当对于图片的大小不确定时,要做一下限制,以防出现OOM,下面给出真正放大,缩小的代码

public void scalePicByMatrix(float scaleWidth, float scaleHeight){
        Matrix matrix = new Matrix();
        matrix.setScale(scaleWidth,scaleHeight);
        bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false);
        postInvalidate();
    }

旋转:

public void rotatePic(int angle) {
        Matrix matrix = new Matrix();
        matrix.setRotate(angle);
        bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false);
        postInvalidate();
    }

剪裁:

public void cutPic(int reqWidth, int reqHeight) {
        if (bitmap.getWidth() > reqWidth && bitmap.getHeight() > reqHeight) {
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, reqWidth, reqHeight);
        }else {
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight());
        }
        postInvalidate();
    }

图片保存:

public void savePic(String path) {
        File file = new File(path);
        FileOutputStream fileOutputStream = null;
        try {
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

以上给出的只是其中的一种方法,具体操作看应用场景

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值