Android SurfaceView畫(huà)板操作
本文實(shí)例為大家分享了Android SurfaceView畫(huà)板操作的具體代碼,供大家參考,具體內(nèi)容如下
畫(huà)板——畫(huà)路徑
package com.example.review.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * 畫(huà)板畫(huà)路徑 */ public class HuabanView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder surfaceHolder; private Path path = new Path(); public HuabanView(Context context) { super(context); } public HuabanView(Context context, AttributeSet attrs) { super(context, attrs); surfaceHolder = getHolder(); surfaceHolder.addCallback(this);//獲得surfaceview的生命周期 } public HuabanView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public HuabanView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void surfaceCreated(SurfaceHolder holder) { new HuabanThread().start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) {//按下 path.moveTo(x, y); } else if (action == MotionEvent.ACTION_MOVE) {//移動(dòng) path.lineTo(x, y); } return true; } class HuabanThread extends Thread { @Override public void run() { super.run(); //TODO:畫(huà)筆 Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); //TODO:畫(huà)布 while (true) { Canvas canvas = surfaceHolder.lockCanvas(); //避免空指針 if (canvas == null){ return; } canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR); canvas.drawColor(Color.WHITE); canvas.drawPath(path,paint); surfaceHolder.unlockCanvasAndPost(canvas); } } } public void close(){ path.reset(); } }
畫(huà)板——畫(huà)動(dòng)態(tài)直線
package com.example.review.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; /** * 畫(huà)板畫(huà)路徑 * 畫(huà)動(dòng)態(tài)直線 */ public class LineView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder surfaceHolder; private Path path = new Path(); private int x = 0; public LineView(Context context) { super(context); } public LineView(Context context, AttributeSet attrs) { super(context, attrs); surfaceHolder = getHolder(); surfaceHolder.addCallback(this);//獲得surfaceview的生命周期 } public LineView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public LineView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void surfaceCreated(SurfaceHolder holder) { new HuabanThread().start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } class HuabanThread extends Thread { @Override public void run() { super.run(); //TODO:畫(huà)筆 Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setStrokeWidth(20); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); //TODO:畫(huà)布 while (true) { Canvas canvas = surfaceHolder.lockCanvas(); //避免空指針 if (canvas == null){ return; } canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR); canvas.drawColor(Color.WHITE); canvas.drawLine(0,100,x++,100,paint); surfaceHolder.unlockCanvasAndPost(canvas); } } } public void close(){ path.reset(); } }
基本圖形
//圓 canvas.drawOval(50,100,150,200,paint); //半圓 canvas.drawArc(500,500,700,700,20,180,true,paint); //矩形 canvas.drawRect(100,300,250,400,paint); //三角形 canvas.drawLine(100,450,0,600,paint); canvas.drawLine(0,600,400,600,paint); canvas.drawLine(100,450,400,600,paint); //梯形 canvas.drawLine(100,700,200,700,paint); canvas.drawLine(100,700,0,900,paint); canvas.drawLine(0,900,400,900,paint); canvas.drawLine(200,700,400,900,paint); //文字 canvas.drawText("截圖",100,1000,paint);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)畫(huà)板功能(二)
- Android實(shí)現(xiàn)畫(huà)板功能(一)
- Android實(shí)現(xiàn)簡(jiǎn)單畫(huà)圖畫(huà)板
- Android studio實(shí)現(xiàn)畫(huà)板功能
- Android實(shí)現(xiàn)畫(huà)畫(huà)板案例
- Android畫(huà)板開(kāi)發(fā)之添加文本文字
- Android畫(huà)板開(kāi)發(fā)之添加背景和保存畫(huà)板內(nèi)容為圖片
- Android畫(huà)板開(kāi)發(fā)之撤銷反撤銷功能
- Android畫(huà)板開(kāi)發(fā)之基本畫(huà)筆功能
- Android畫(huà)板開(kāi)發(fā)之橡皮擦功能
- Android曲線更圓滑的簽名畫(huà)板
- Android實(shí)現(xiàn)繪畫(huà)板功能
相關(guān)文章
Android仿天貓橫向滑動(dòng)指示器功能的實(shí)現(xiàn)
這篇文章主要介紹了Android仿天貓橫向滑動(dòng)指示器,Android開(kāi)發(fā)中會(huì)有很多很新奇的交互,比如天貓商城的首頁(yè)頭部的分類,使用的是GridLayoutManager+橫向指示器實(shí)現(xiàn)的,需要的朋友可以參考下2022-08-08基于android studio的layout的xml文件的創(chuàng)建方式
這篇文章主要介紹了基于android studio的layout的xml文件的創(chuàng)建方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android Dialog對(duì)話框?qū)嵗a講解
本文通過(guò)實(shí)例代碼給大家介紹了Android Dialog對(duì)話框的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Android實(shí)現(xiàn)屏幕手寫(xiě)簽名
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)屏幕手寫(xiě)簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Android WebView 內(nèi)處理302重定向不跳轉(zhuǎn)的解決
這篇文章主要介紹了Android WebView 內(nèi)處理302重定向不跳轉(zhuǎn)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03淺析Android手機(jī)衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作
通過(guò)廣播接收者,接收到短信,對(duì)短信內(nèi)容進(jìn)行判斷,如果為我們指定的值就執(zhí)行相應(yīng)的操作。本文給大家介紹Android手機(jī)衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作,感興趣的朋友參考下吧2016-04-04android實(shí)現(xiàn)通過(guò)NFC讀取卡號(hào)
這篇文章主要介紹了android實(shí)現(xiàn)通過(guò)NFC讀取卡號(hào),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼
這篇文章主要介紹了Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10Android studio 3.0 查看手機(jī)文件系統(tǒng)的方法(超簡(jiǎn)單)
本文給大家分享Android studio更新到3.0版本之后,查看手機(jī)文件系統(tǒng)的方法,需要的朋友參考下吧2017-11-11