安卓学习 之 bitmap用法

1. 获取资源

从资源文件得到图片

Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha);

读取sd卡(通过文件)

String SDCarePath=Environment.getExternalStorageDirectory().toString();   
String filePath=SDCarePath+"/"+"haha.jpg";   
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);   

读取sd卡(通过输入流)

InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg");   
Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream);
//读取SD卡下的图片   
    private InputStream getBitmapInputStreamFromSDCard(String fileName){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String SDCarePath=Environment.getExternalStorageDirectory().toString();
            String filePath=SDCarePath+File.separator+fileName;
            File file=new File(filePath);
            try {
                FileInputStream fileInputStream=new FileInputStream(file);
                return fileInputStream;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }

2. 设置圆角

 public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
        Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(roundCornerBitmap);
        int color = 0xff424242;//int color = 0xff424242;
        Paint paint = new Paint();
        paint.setColor(color);
        //防止锯齿
        paint.setAntiAlias(true);
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(rect);
        float roundPx = pixels;
        //相当于清屏
        canvas.drawARGB(0, 0, 0, 0);
        //先画了一个带圆角的矩形
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //再把原来的bitmap画到现在的bitmap!!!注意这个理解
        canvas.drawBitmap(bitmap, rect, rectF, paint);
        return roundCornerBitmap;
    }  
}   

/*
 * bitmap 绘制的bitmap对象
 * src  Bitmap对象的矩形区域
 * dst  bitmap绘制在屏幕的什么地方
 * paint Paint对象
 */
public void drawBitmap(Bitmap bitmap, Rect src, RectF dst,Paint paint)

3. 压缩图片(通过矩阵)

// 得到图片原始的高宽   
int rawHeight = rawBitmap.getHeight();   
int rawWidth = rawBitmap.getWidth();   

// 设定图片新的高宽   
int newHeight = 500;   
int newWidth = 500;   

// 计算缩放因子   
float heightScale = ((float) newHeight) / rawHeight;   
float widthScale = ((float) newWidth) / rawWidth;   
// 新建立矩阵   
Matrix matrix = new Matrix();   
matrix.postScale(heightScale, widthScale);   

// 设置图片的旋转角度   
//matrix.postRotate(-30);   
// 设置图片的倾斜   
//matrix.postSkew(0.1f, 0.1f);   

//将图片大小压缩,压缩后图片的宽和高以及kb大小均会变化     
Bitmap newBitmap = Bitmap.createBitmap(rawBitmap, 0, 0, rawWidth,rawWidth, matrix, true);   

// 将Bitmap转换为Drawable   
Drawable newBitmapDrawable = new BitmapDrawable(newBitmap);   
imageView.setImageDrawable(newBitmapDrawable);   

传入原来的bitmap,调用compressAndSaveBitmapToSDCard使得宽高不变,大小缩小,80代表压缩的质量

this.compressAndSaveBitmapToSDCard(copyRawBitmap1(原bitmap),"0011fa.jpg",80);   
//压缩且保存图片到SDCard
    private void compressAndSaveBitmapToSDCard(Bitmap rawBitmap, String fileName, int quality) {
        String saveFilePaht = this.getSDCardPath() + File.separator + fileName;
        File saveFile = new File(saveFilePaht);
        if (!saveFile.exists()) {
            try {
                saveFile.createNewFile();
                FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
                if (fileOutputStream != null) {
                    //imageBitmap.compress(format, quality, stream);
                    //把位图的压缩信息写入到一个指定的输出流中
                    //第一个参数format为压缩的格式
                    //第二个参数quality为图像压缩比的值,0-100.0 意味着小尺寸压缩,100意味着高质量压缩
                    //第三个参数stream为输出流
                    rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
                }
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }

4. 获取缩略图

String SDCarePath2=Environment.getExternalStorageDirectory().toString();   
String filePath2=SDCarePath2+"/"+"haha.jpg";   
Bitmap tempBitmap=BitmapFactory.decodeFile(filePath2);   
Bitmap bitmapThumbnail2=ThumbnailUtils.extractThumbnail(tempBitmap, 100, 100);   
imageView.setImageBitmap(bitmapThumbnail2);   

其中ThumbnailUtils 是安卓提供的工具类

你可能感兴趣的:(安卓学习)