美文网首页androidAndroid知识
Android 之 图片变换

Android 之 图片变换

作者: 子墨_guo | 来源:发表于2016-01-26 16:29 被阅读97次

说到图片,第一反映就是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();        
        }    
    }
}

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

相关文章

网友评论

  • 5f4cd7e51fb8:裁剪的代码里头,bitmap宽高判断条件是不是写错了?应该是 或 运算吧?

本文标题:Android 之 图片变换

本文链接:https://www.haomeiwen.com/subject/byedkttx.html