Android Bitmap & Paint & Canvas & Matrix 位图/图片/画板

来源:互联网 发布:优化网站排名 编辑:程序博客网 时间:2024/04/30 10:51

Bitmap & Paint & Canvas & Matrix 位图/图片/画板

安卓中最占资源,内存,,最容易导致内存溢出的




加载图片

//创建一个位图工厂的配置文件BitmapFactory.Options options = new BitmapFactory.Options();//配置解码器不去真正解析图片,去获取图片的高宽信息options.inJustDecodeBounds = true;BitmapFactory.decodeFile("/mnt/sdcard/pic/jpg",options);//获取图片的高宽信息int imgWidth = options.outWidth;int imgHeight = options.outHeight;//计算缩放比例int scale = 1;int scaleX = imgWidth/witdh; //witdh屏幕或界面宽度int scaleY = imgHeight/height;if(scaleX>=scaleY && scaleX>scale) scale = scaleX;if(scaleY>scaleX && scaleY>scale) scale = scaleY;//配置缩放比, 取消之前的不解析图片 并 按照缩放比加载图片options.inSampleSize = scale;options.inJustDecodeBounds = false;Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/pic/jpg", options);//把图片装载控件img0.setImageBitmap(bitmap);







Paint 画笔

Paint paint1 = new Paint();paint1.setColor(Color.WHITE);        // 画笔颜色paint1.setAntiAlias(true);           // 启用抗锯齿 会变慢paint1.setDither(true);              // 启用抖动处理 更美观paint1.setStyle(Paint.Style.STROKE); // 设置画笔为空心paint2.setAlpha(50);                 // 透明度paint1.setStrokeWidth((float) 2);    // 设置线宽paint4.setTextSize(this.Scale / 3);  //画的文本大小




Canvas 画板

canvas.drawColor(Color.BLACK); // 设置画布背景颜色canvas.drawLine(GoX, GoY, ToX, ToY, paint); //划线canvas.drawText("我爱你", startX, startY, paint); //写字 startX/Y左上角坐标




画画

//通过位图工厂获取图片final Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic00);//获取原图的副本 相同尺寸的空白纸final Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth(),srcBitmap.getHeight(),srcBitmap.getConfig());final Paint paint = new Paint(); //创建画笔final Canvas canvas = new Canvas(copyBitmap); //创建画板canvas.drawBitmap(srcBitmap, new Matrix(), paint); //把原图画上去paint.setColor(Color.RED); // 画笔颜色paint.setStrokeWidth((float) 50); // 设置线宽myImg.setImageBitmap(copyBitmap); //设置图片myImg.setOnTouchListener(new OnTouchListener() { //设置触摸事件    float startX = 0;    float startY = 0;    float stopX = 0;    float stopY = 0;    @Override    public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN: //按下                Log.e("***************", event.getX()+"-"+event.getY());                //获取开始坐标                startX = event.getX();                startY = event.getY();                break;            case MotionEvent.ACTION_MOVE: //移动                //获取结束坐标                stopX = event.getX();                stopY = event.getY();                //画线                canvas.drawLine(startX*2, startY, stopX*2, stopY, paint);                //重新设置开始坐标                startX = stopX;                startY = stopY;                //重新加载图片                myImg.setImageBitmap(copyBitmap);                break;            case MotionEvent.ACTION_UP: //抬起                break;            default:                break;        }        return true;    }});

重置操作

canvas.drawBitmap(srcBitmap, new Matrix(), paint); //把原图画上去myImg.setImageBitmap(copyBitmap);




// 画Y虚线

private void drawYTable(Canvas canvas, Paint paint) {    Path path = new Path();    PathEffect pathEffect = new DashPathEffect(new float[] { 5, 2 }, 1);    paint.setPathEffect(pathEffect);    // Y向线    // (*/2:因为间距是20,我要以10画一条线)    for (int i = 1; i < yLabel.length * 2 - 1; i++) {        float startY = yPoint - i * Scale / 2;        float stopX = xPoint + (this.xLabel.length ) * Scale - Scale/2;          path.moveTo(xPoint, startY);        path.lineTo(stopX, startY);        canvas.drawPath(path, paint);    }}




操作图片

Matrix matrix = new Matrix();//每次操作都会覆盖原先的操作matrix.setRotate(20, srcBitmap.getWidth()/2, srcBitmap.getHeight()/2); //旋转matrix.setScale(0.5f, 0.5f); //切换大小matrix.setTranslate(30, 0); //平移//镜面效果matrix.setScale(-1.0f, 1);matrix.postTranslate(srcBitmap.getWidth(), 0); //post 在上次修改的基础上再进行修改//倒影效果matrix.setScale(1.0f, -1);matrix.postTranslate(0, srcBitmap.getWidth());//应用修改canvas.drawBitmap(srcBitmap, matrix, paint); //把原图画上去




保存图片

try {    File file = new File(Environment.getExternalStorageDirectory().getPath(),SystemClock.uptimeMillis()+".jpg"); //开机运行了多久时间    FileOutputStream fos = new FileOutputStream(file);    copyBitmap.compress(CompressFormat.JPEG, 100, fos); //保存的类型,质量(0~100),流    //发送一条广播 SD卡重新挂载 更新图库    Intent intent = new Intent();    intent.setAction(Intent.ACTION_MEDIA_MOUNTED);    intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));    sendBroadcast(intent);    Toast.makeText(getApplicationContext(), "保存成功", 0).show();} catch (Exception e) {    e.printStackTrace();}




撕衣服

原理:: 两张图片。有衣服的在上,没衣服在下,把在上面的图片画为透明#00000000/Color.TRANSPARENT

img.setOnTouchListener(new OnTouchListener() {    @Override    public boolean onTouch(View v, MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                break;            case MotionEvent.ACTION_MOVE:                try {                    for(int x=-20; x<20; ++x)                    for(int y=-20; y<20; ++y){                        if(Math.sqrt(x*x+y*y)<20) //改为圆                        copyBitmap.setPixel((int)event.getX()+x,(int)event.getY()+y,Color.TRANSPARENT); //Color.TRANSPARENT透明                    }                    img.setImageBitmap(copyBitmap); //设置图片                } catch (Exception e) {                    e.printStackTrace();                }                break;            default:                break;        }        return true;    }});




0 0