android 加载自定义图片并在图片上绘图

来源:毕设

关键词:Bitmap Canvas

//毕设中需要自定义室内地图,并且在地图上绘制轨迹
//此处是一个测试Demo,实现图片的加载和记录手指在屏幕上的运动轨迹

图片的载入

使用系统提供的内容提供者,要点如下:

1.调用android图库的方式:Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
2.使用startActivityForResult(Intent intent, int requestCode) & onActivityResult(int requestCode, int resultCode, Intent data)返回结果到该activity.
3.权限:
4.Bitmap&Canvas的使用见注释内容

        mLoadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//调用android图库
                startActivityForResult(intent, RESULT);
            }
        });
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode ==RESULT_OK) {
            try {

                Uri imageFileUri = data.getData();
                Display defaultDisplay = getWindowManager().getDefaultDisplay();
                float ddw = defaultDisplay.getWidth();
                float ddh = defaultDisplay.getHeight();

                //BitmapFactory.Options options = new BitmapFactory().Options();//注意别写错了,options是静态内部类,需要直接使用外部类直接饮用
                BitmapFactory.Options options = new BitmapFactory.Options();
                /**
                 * inJustDecodeBounds 如果设置为true,并不会把图像的数据完全解码,亦即decodeXyz()返回值为null,但是Options的outAbc中解出了图像的基本信息。
                 * 先设置inJustDecodeBounds= true,调用decodeFile()得到图像的基本信息,利用图像的宽度(或者高度,或综合)以及目标的宽度,得到inSampleSize值,
                 * 再设置inJustDecodeBounds= false,调用decodeFile()得到完整的图像数据。
                 * 先获取比例,再读入数据,如果欲读入大比例缩小的图,将显著的节约内容资源。有时候还会读入大量的缩略图,这效果就更明显了。
                 */
                options.inJustDecodeBounds = true;
                mBitmap = BitmapFactory.decodeStream(getContentResolver()
                        .openInputStream(imageFileUri), null, options);//需要权限android.permission.READ_EXTERNAL_STORAGE

                //计算缩放因子
                int heightRatio = (int) Math.ceil(options.outHeight/ddh);
                int widthRatio = (int) Math.ceil(options.outWidth/ddw);
                if (heightRatio > widthRatio) {
                    options.inSampleSize = heightRatio;
                } else {
                    options.inSampleSize = widthRatio;
                }

                options.inJustDecodeBounds = false;
                mBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, options);
                mAlterBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());

                mCanvas = new Canvas(mAlterBitmap);
                mPaint = new Paint();
                mPaint.setColor(Color.GREEN);
                mPaint.setStrokeWidth(5);
                Matrix matrix = new Matrix();//矩阵

                mCanvas.drawBitmap(mBitmap, matrix, mPaint);
                mIv.setImageBitmap(mAlterBitmap);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

在图片上绘制

图片被加载到了ImageView控件mIv中,通过设置mIv.setOnTouchListener(),可以实现在图片上的绘制.

                mIv.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_DOWN:
                                downx = event.getX();
                                downy = event.getY();
                                break;
                            case MotionEvent.ACTION_MOVE:
                                upx = event.getX();
                                upy = event.getY();
                                mCanvas.drawLine(downx, downy, upx, upy, mPaint);
                                mIv.invalidate();
                                downx = upx;
                                downy = upy;
                                break;
                            case MotionEvent.ACTION_UP:
                                break;

                            default:
                                break;
                        }

                        return true;
                    }
                });

图片的保存

mSaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存画好的图片
                if(mAlterBitmap!=null){
                    try {
                        Uri imageUri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
                        OutputStream outputStream=getContentResolver().openOutputStream(imageUri);
                        mAlterBitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
                        Toast.makeText(getApplicationContext(), "save!", Toast.LENGTH_SHORT).show();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

效果图

github地址:https://github.com/zhangbz/BitmapTest

原文地址:https://www.cnblogs.com/happyhacking/p/5291442.html