Android编程实现在Bitmap上涂鸦效果
本文实例讲述了Android编程实现在Bitmap上涂鸦效果。分享给大家供大家参考,具体如下:
布局文件:
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/handwriteview" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal"> <Button android:id="@+id/clear" android:layout_width="200dp" android:layout_height="wrap_content" android:text="清屏"/> </LinearLayout> </LinearLayout>
重写的View文件:
publicclassHandWriteextendsView { privatePaintpaint=null; privateBitmaporiginalBitmap=null; privateBitmapnew1Bitmap=null; privateBitmapnew2Bitmap=null; privatefloatclickX=0,clickY=0; privatefloatstartX=0,startY=0; privatebooleanisMove=true; privatebooleanisClear=false; privateintcolor=Color.GREEN; privatefloatstrokeWidth=2.0f; publicHandWrite(Contextcontext,Bitmapb) { super(context); originalBitmap=Bitmap.createBitmap(b).copy(Bitmap.Config.ARGB_8888,true); new1Bitmap=Bitmap.createBitmap(originalBitmap); } publicvoidclear(){ isClear=true; new2Bitmap=Bitmap.createBitmap(originalBitmap); invalidate(); } publicvoidsetstyle(floatstrokeWidth){ this.strokeWidth=strokeWidth; } @Override protectedvoidonDraw(Canvascanvas) { super.onDraw(canvas); canvas.drawBitmap(HandWriting(new1Bitmap),0,0,null); } publicBitmapHandWriting(BitmaporiginalBitmap) { Canvascanvas=null; if(isClear){ canvas=newCanvas(new2Bitmap); } else{ canvas=newCanvas(originalBitmap); } paint=newPaint(); paint.setStyle(Style.STROKE); paint.setAntiAlias(true); paint.setColor(color); paint.setStrokeWidth(strokeWidth); if(isMove){ canvas.drawLine(startX,startY,clickX,clickY,paint); } startX=clickX; startY=clickY; if(isClear){ returnnew2Bitmap; } returnoriginalBitmap; } @Override publicbooleanonTouchEvent(MotionEventevent) { clickX=event.getX(); clickY=event.getY(); if(event.getAction()==MotionEvent.ACTION_DOWN){ isMove=false; invalidate(); returntrue; } elseif(event.getAction()==MotionEvent.ACTION_MOVE){ isMove=true; invalidate(); returntrue; } returnsuper.onTouchEvent(event); } }
Activity文件:
publicclassHandWritingActivityextendsActivity { /**Calledwhentheactivityisfirstcreated.*/ privateLinearLayouthandWrite=null; privateButtonclear=null; intrequestWidth=116; intrequestHeight=173; intinSampleSize; @Override publicvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hand_writing); handWrite=(LinearLayout)findViewById(R.id.handwriteview); clear=(Button)findViewById(R.id.clear); clear.setOnClickListener(newclearListener()); } privateclassclearListenerimplementsOnClickListener{ publicvoidonClick(Viewv) { //handWrite.clear(); BitmapFactory.Optionsopts=newOptions(); opts.inJustDecodeBounds=true;//让bimapfactory假的解析这个位图,只获取位图的边框信息 BitmapFactory.decodeResource(getResources(),R.drawable.cool,opts); if(opts.outHeight>requestHeight||opts.outWidth>requestWidth){ if(opts.outWidth>opts.outHeight){ inSampleSize=Math.round((float)opts.outHeight /(float)requestHeight); }else{ inSampleSize=Math.round((float)opts.outWidth /(float)requestWidth); } } System.out.println("宽度:"+opts.outWidth); System.out.println("高度:"+opts.outHeight); opts.inSampleSize=inSampleSize; System.out.println(inSampleSize); opts.inJustDecodeBounds=false;//由于已经得到了缩放比例,让位图工厂真正的解析这个位图 //由于前面我们已经解析了这个输入流,需要重新初始化这个输入流 Bitmapb=BitmapFactory.decodeResource(getResources(),R.drawable.cool,opts); HandWritehw=newHandWrite(HandWritingActivity.this,b); System.out.println(b.getWidth()); handWrite.addView(hw); } } }
整合的一个涂鸦工具类:
/** *使用方法: *1.创建TuYaView类实例 *2.调用drawTuya方法 *3.参数1:context *4.参数2:图像的byte[]字节数组 *5.ImageView实例 *6.画笔定义 ***/ importcom.ziipin.lhdc.utils.ToastUtil; importandroid.content.Context; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.graphics.Canvas; importandroid.graphics.Matrix; importandroid.graphics.Paint; importandroid.graphics.BitmapFactory.Options; importandroid.view.MotionEvent; importandroid.view.View; importandroid.view.View.OnTouchListener; importandroid.widget.ImageView; publicclassTuYaView{ //原始图片 privateBitmapmOrignBitmap; privateBitmapmEditBitmap; privateintinSampleSize; privateintrequestWidth=500; privateintrequestHeight=700; /**编辑图片的画布*/ privateCanvasmCanvas; privateImageViewimage; privatePaintmPaint; publicBitmapdrawTuya(Contextcontext,byte[]_data,ImageViewimage, PaintmPaint){ this.image=image; this.mPaint=mPaint; mOrignBitmap=BitmapFactory.decodeByteArray(_data,0,_data.length); returnshowEditBitmap(context,_data,image); } /** *显示编辑的图片 */ privateBitmapshowEditBitmap(Contextcontext,byte[]_data,ImageViewimage){ mOrignBitmap=getScaleBitmap(_data,image); if(mOrignBitmap==null){ ToastUtil.show(context,"编辑出错"); } mEditBitmap=mOrignBitmap.copy(mOrignBitmap.getConfig(),true); mCanvas=newCanvas(mEditBitmap); mCanvas.drawBitmap(mOrignBitmap,newMatrix(),newPaint()); image.setImageBitmap(mEditBitmap); image.setOnTouchListener(mTouchListener); returnmEditBitmap; } /** *获取结果缩放放后的图片 * *@return */ privateBitmapgetScaleBitmap(byte[]_data,ImageViewimage){ BitmapFactory.Optionsopts=newOptions(); opts.inJustDecodeBounds=true;//让bimapfactory假的解析这个位图,只获取位图的边框信息 BitmapFactory.decodeByteArray(_data,0,_data.length,opts); if(opts.outHeight>requestHeight||opts.outWidth>requestWidth){ if(opts.outWidth>opts.outHeight){ inSampleSize=Math.round((float)opts.outHeight /(float)requestHeight); }else{ inSampleSize=Math.round((float)opts.outWidth /(float)requestWidth); } } opts.inSampleSize=inSampleSize; opts.inJustDecodeBounds=false;//由于已经得到了缩放比例,让位图工厂真正的解析这个位图 //由于前面我们已经解析了这个输入流,需要重新初始化这个输入流 Bitmapbmp=BitmapFactory .decodeByteArray(_data,0,_data.length,opts); returnbmp; } //touch事件 privateOnTouchListenermTouchListener=newOnTouchListener(){ intstartx=0; intstarty=0; @Override publicbooleanonTouch(Viewv,MotionEventevent){ switch(event.getAction()){ caseMotionEvent.ACTION_DOWN://手指第一次触摸屏幕 startx=(int)event.getX(); starty=(int)event.getY(); break; caseMotionEvent.ACTION_MOVE://手指在imageview上中移动 intx=(int)event.getX(); inty=(int)event.getY(); mCanvas.drawLine(startx,starty,x,y,mPaint); startx=(int)event.getX(); starty=(int)event.getY(); image.invalidate(); break; } returntrue; } }; }
希望本文所述对大家Android程序设计有所帮助。