SurfaceView使用
首先創建一個SurfaceViewSign類,繼承SurfaceView類,繼承 SurfaceHolder.Callback和Runnable接口,代碼如下:
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; public class SurfaceViewSign extends SurfaceView implements SurfaceHolder.Callback,Runnable { //SurfaceHolder private SurfaceHolder holder; //用於繪圖的Canvas private Canvas canvas; //子線程標誌位 private boolean isDrawing; //畫筆 private Paint paint; //路徑 private Path path; private Bitmap bitmap; private Canvas getCanvas; /** * 獲取mCanvas裏的bitmap * */ public Bitmap getBitmap() { getCanvas.drawColor(Color.WHITE);//畫布背景色 getCanvas.drawPath(path, paint); getCanvas.save(); getCanvas.restore(); return bitmap; //region bitmap壓縮到文件 // File file = new File(Environment.getExternalStorageDirectory().getPath() + "/share_pic.png");// 保存到sdcard根目錄下,文件名爲share_pic.png // FileOutputStream fos = null; // try { // fos = new FileOutputStream(file); // bitmap.compress(Bitmap.CompressFormat.PNG, 50, fos); // // } catch (FileNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // try { // fos.close(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } //endregion } public SurfaceViewSign(Context context) { super(context); initView(); } public SurfaceViewSign(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public SurfaceViewSign(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } private void initView() { bitmap = Bitmap.createBitmap(1000,1000, Bitmap.Config.ARGB_8888); getCanvas = new Canvas(bitmap); holder = getHolder(); //添加回調 holder.addCallback(this); path =new Path(); //初始化畫筆 paint =new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(6); paint.setAntiAlias(true); paint.setColor(Color.RED);//畫筆顏色 setFocusable(true); setFocusableInTouchMode(true); this.setKeepScreenOn(true); } //Surface的生命週期 @Override public void surfaceCreated(SurfaceHolder holder) { isDrawing =true; new Thread(this).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { isDrawing =false; } @Override public void run() { long start =System.currentTimeMillis(); while(isDrawing){ draw(); long end = System.currentTimeMillis(); if(end-start<100){ try{ Thread.sleep(100-end+start); } catch (InterruptedException e) { e.printStackTrace(); } } } } private void draw() { try{ //鎖定畫布並返回畫布對象 canvas = holder.lockCanvas(); canvas.drawColor(Color.WHITE);//設置畫布背景色 canvas.drawPath(path, paint); //畫線 }catch (Exception e){ }finally { if(canvas !=null) holder.unlockCanvasAndPost(canvas);//解鎖 } } /** * 繪製觸摸滑動路徑 * @param event MotionEvent * @return true */ @Override public boolean onTouchEvent(MotionEvent event) { int x=(int) event.getX(); int y= (int) event.getY(); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: path.moveTo(x,y); break; case MotionEvent.ACTION_MOVE: path.lineTo(x,y); break; case MotionEvent.ACTION_UP: break; } return true; } /** * 清屏 * @return true */ public boolean reDraw(){ path.reset(); return true; } }
然後創建一個測試Activity,然後編寫他的頁面代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" android:background="@color/white" tools:ignore="MissingClass"> <com.kiba.test.control.surfaceview.SurfaceViewSign android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/red"></View> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" ></ImageView> </LinearLayout>
然後在activity裏編寫點擊事件如下:
@SingleClick @OnClick(R.id.imageView) public void ImageClick() { Bitmap bitmap =sv.getBitmap(); imageView.setImageBitmap(bitmap); imageView2.setImageBitmap(bitmap); }
這樣就實現了簡單的簽名,並且獲取到了簽名的圖片,類型是bitmap。
效果圖如下:
----------------------------------------------------------------------------------------------------
注:此文章爲原創,任何形式的轉載都請聯繫作者獲得授權並註明出處!
若您覺得這篇文章還不錯,請點擊下方的【推薦】,非常感謝!