副本的创建以及画板DIY

来源:互联网 发布:js实现分页功能 编辑:程序博客网 时间:2024/06/05 15:36

1:创建副本的步骤

[1]创建一个BItmap对象,获取图片的来源

[2]使用Bitmap的createBitmap创建一个副本

2:绘画

使用canvas(画布)来画画,创建一个canvas对象,参数为要操作的资源,使用draw...()方法,paint为画笔,Mitrix为矩阵.

3:设置触摸事件

iv_src = (ImageView) findViewById(R.id.iv_src);iv_copy = (ImageView) findViewById(R.id.iv_copy);Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tomcat);iv_src.setImageBitmap(srcBitmap);copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());paint = new Paint();canvas = new Canvas(copyBitmap);Matrix matrix = new Matrix();/*//旋转matrix.setRotate(20);*//*//平移matrix.setTranslate(30, 0);*//*//镜像matrix.setScale(-1, 1);matrix.postTranslate(copyBitmap.getWidth(), 0);*//*//倒影matrix.setScale(1, -1);matrix.postTranslate(0, copyBitmap.getHeight());*/canvas.drawBitmap(srcBitmap, matrix, paint);iv_copy.setImageBitmap(copyBitmap);iv_copy.setOnTouchListener(new OnTouchListener() {int startX = 0;int startY = 0;@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stub//获取该动作的名字int action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN://System.out.println("down");startX = (int) event.getX();startY = (int) event.getY();break;case MotionEvent.ACTION_MOVE://System.out.println("move");int stopX = (int) event.getX();int stopY = (int) event.getY();canvas.drawLine(startX, startY, stopX, stopY, paint);iv_copy.setImageBitmap(copyBitmap);startX = stopX;startY = stopY;break;case MotionEvent.ACTION_UP://System.out.println("up");break;}//要是turereturn true;}});

4:保存

try {File file = new File(Environment.getExternalStorageDirectory().getPath(), SystemClock.uptimeMillis() + ".png");FileOutputStream fos = new FileOutputStream(file);copyBitmap.compress(CompressFormat.PNG, 100, fos);//同步到相册中 相册在存储卡重新挂载的时候更新 这时可以发送一条广播,广播为存储卡重新挂载.Intent intent = new Intent();//当广播为存储卡挂载或应用安装卸载时要写两个方法setAction  setDataintent.setAction(Intent.ACTION_MEDIA_MOUNTED);intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));sendBroadcast(intent);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}

使用Bitmap对象 的compress(格式,精度(100),输出流)方法保存,发送广播事件,更新相册.

原创粉丝点击