当前位置:   article > 正文

Android - Bitmap用法_android bitmap 复制

android bitmap 复制


前言

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


一、获取Bitmap位图对象

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

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

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

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

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

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

4.通过字节数组创建

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

5.通过输入流创建

InputStream inputStream = new InputStream() {
            @Override
            public int read() throws IOException {
                return 0;
            }
        };
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、对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);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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);;
  • 1
  • 2
  • 3
  • 4

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

总结

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/303784
推荐阅读
相关标签
  

闽ICP备14008679号