Android - Bitmap用法


前言

代码直接复制使用,修改为自己需要的参数就可以了


一、获取Bitmap位图对象

五种方法获取Bitmap对象
1.构建BitmapDrawable对象生成Bitmap

BitmapDrawable mBitmap = new BitmapDrawable(getResources(), "sdcard/xxx.png");
Bitmap bitmap = mBitmap.getBitmap();

2.通过资源ID创建,需要将对应的资源先放在项目中的drawable文件夹中

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);

3.通过文件创建,需要文件绝对路径

Bitmap bitmap = BitmapFactory.decodeFile("sdcard/xxx.png");

4.通过字节数组创建

byte[] data;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

5.通过输入流创建

InputStream inputStream = new InputStream() {
            @Override
            public int read() throws IOException {
                return 0;
            }
        };
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

二、对Bitmap进行处理

1. 缩放

以下代码将图片调整到与控件大小一样刚好占满控件,前面两个参数为位图的宽度和高度,第三个参数为格式
onWindowFocusChanged这个方法里面才可以获取控件的宽度和高度,oncreate方法里面是无法获取的

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            int[] a = new int[2];
            img = (ImageView) findViewById(R.id.img);
            img.getLocationOnScreen(a);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);
            //缩放
            Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap,img.getWidth(), img.getHeight(),true);
            img.setImageBitmap(bitmap2);
        }
    }

2.抠图

第一个参数需要剪切的位图对象,然后是剪切的起始位置为剪切图左上角的坐标,最后两个参数是剪切图的宽度和高度

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap,100,100,200,200);
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap2);;

3.旋转图片方向

下面的代码将图片旋转90°

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1);
Matrix matrix = new Matrix();
matrix.postRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);

4.将Bitmap位图保存为图片文件

以下代码将Bitmap保存为图片文件,安卓10以下保存在sdcard根目录,安卓10以上保存在Android/data/包名/files目录下

new Thread(new Runnable(){
    @Override
    public void run(){
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.abc1);
        try{
            String filePath=null;
            //如果手机已插入sd卡,且app具有读写sd卡的权限
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                if(Build.VERSION.SDK_INT< 29){
                     //安卓10以下保存在SD卡根目录
                    filePath=Environment.getExternalStorageDirectory().getCanonicalPath()+"/"+System.currentTimeMillis()+".jpg";
                }else{
                    //安卓10以上保存在Android/data/com.example.filetorw/files目录下
                    filePath=MainActivity.this.getExternalFilesDir(null).getAbsolutePath()+"/"+System.currentTimeMillis()+".jpg";
                }
            }
            FileOutputStream outStream=new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,outStream);
            outStream.flush();
            outStream.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}).start();

5.图片上插入其他图片和文字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Paint mPaint = new Paint();;
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(36);
        mPaint.setStrokeWidth(5);
        Canvas mCanvas;
        ImageView img = (ImageView) findViewById(R.id.img);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.abc1, options).copy(Bitmap.Config.ARGB_8888, true);
        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.abc2);

        bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
        mCanvas = new Canvas(bitmap);
        //添加图片
        mCanvas.drawBitmap(bitmap2, 100, 100, null);
        //添加文字
        mCanvas.drawText("测试", 200, 250, mPaint);
        mCanvas.save();
        mCanvas.restore();
        img.setImageBitmap(bitmap);
    }

总结

代码直接复制使用,修改为自己需要的参数就可以了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值