網上的畫板代碼收集和整理

修改后的代碼[1]為,少了一個}

package com.example.administrator.myapplication;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;public class MyWallActivity extends Activity
{private SurfaceView mSurfaceView = null;private SurfaceHolder mSurfaceHolder = null;private Button cleanButton = null;private Button colorButton = null;private float oldX = 0f;private float oldY = 0f;private boolean canDraw = false;private Paint mPaint = null;//用來記錄當前是哪一種顏色private int whichColor = 0;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mSurfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);mSurfaceHolder = mSurfaceView.getHolder();mPaint = new Paint();
//畫筆的顏色mPaint.setColor(Color.RED);
//畫筆的粗細mPaint.setStrokeWidth(2.0f);cleanButton = (Button) this.findViewById(R.id.flushbutton);
//按鈕監聽cleanButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {
// TODO Auto-generated method stub
//鎖定整個SurfaceViewCanvas mCanvas = mSurfaceHolder.lockCanvas();mCanvas.drawColor(Color.BLACK);
//繪制完成,提交修改mSurfaceHolder.unlockCanvasAndPost(mCanvas);
//重新鎖一次mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));mSurfaceHolder.unlockCanvasAndPost(mCanvas);}});colorButton = (Button) this.findViewById(R.id.colorbutton);
//按鈕監聽colorButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubDialog mDialog = new AlertDialog.Builder(MyWallActivity.this).setTitle("corlor setting").setSingleChoiceItems(new String[]{"red", "green", "blue"}, whichColor, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubswitch (which) {case 0: {
//畫筆的顏色mPaint.setColor(Color.RED);whichColor = 0;break;}case 1: {
//畫筆的顏色mPaint.setColor(Color.GREEN);whichColor = 1;break;}case 2: {
//畫筆的顏色106mPaint.setColor(Color.BLUE);whichColor = 2;break;}}}}).setPositiveButton("confirm", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubdialog.dismiss();}}).create();mDialog.show();}});}@Overridepublic boolean onTouchEvent(MotionEvent event){
//獲取x坐標float x = event.getX();
//獲取y坐標(不知道為什么要減去一個偏移值才對得準屏幕)float y = event.getY();
//第一次進來先不管if(canDraw){
//獲取觸屏事件switch(event.getAction()){
//如果是拖動事件case MotionEvent.ACTION_MOVE:{
//鎖定整個SurfaceViewCanvas mCanvas = mSurfaceHolder.lockCanvas();mCanvas.drawLine(x, y, oldX, oldY, mPaint);mSurfaceHolder.unlockCanvasAndPost(mCanvas);
//重新鎖一次mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));mSurfaceHolder.unlockCanvasAndPost(mCanvas);break;}}}
//保存目前的x坐標值oldX = x;
//保存目前的y坐標值oldY = y;canDraw = true;return true;}}

[2]里面有個demo可以直接使用,應該是eclipse上的,我轉換為了as的,不過有鋸齒

[3]使用貝塞爾曲線的方法去除了鋸齒

在[4]中可以使用橡皮擦,貌似需要更改一些東西,否則會閃退,可能和用的是as相關

package com.example.administrator.myapplication;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;public class MainActivity extends Activity {private int SCREEN_W;private int SCREEN_H;private int Pen = 1;private int Eraser = 2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LinearLayout layout = new LinearLayout(this);layout.setOrientation(LinearLayout.VERTICAL);LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);Button paint = new Button(this);paint.setText("畫筆");layout.addView(paint, params);Button eraser = new Button(this);eraser.setText("橡皮");layout.addView(eraser, params);final MyView myView = new MyView(this);layout.addView(myView);setContentView(layout);paint.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {myView.setMode(Pen);}});eraser.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {myView.setMode(Eraser);}});}//MyView就是自定義的畫板class MyView extends View {private int mMode = 1;private Bitmap mBitmap;private Canvas mCanvas;private Paint mEraserPaint;private Paint mPaint;private Path mPath;private float mX, mY;private static final float TOUCH_TOLERANCE = 4;public MyView(Context context) {super(context);setFocusable(true);setScreenWH();initPaint();}private void setScreenWH() {DisplayMetrics dm = new DisplayMetrics();dm = this.getResources().getDisplayMetrics();int screenWidth = dm.widthPixels;int screenHeight = dm.heightPixels;SCREEN_W = screenWidth;SCREEN_H = screenHeight;}//設置繪制模式是“畫筆”還是“橡皮擦”public void setMode(int mode){this.mMode = mode;}private void initPaint() {//畫筆mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeCap(Paint.Cap.ROUND);mPaint.setStrokeJoin(Paint.Join.ROUND);mPaint.setColor(Color.BLACK);mPaint.setStrokeWidth(10);//橡皮擦mEraserPaint = new Paint();mEraserPaint.setAlpha(0);//這個屬性是設置paint為橡皮擦重中之重//這是重點//下面這句代碼是橡皮擦設置的重點mEraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//上面這句代碼是橡皮擦設置的重點(重要的事是不是一定要說三遍)mEraserPaint.setAntiAlias(true);mEraserPaint.setDither(true);mEraserPaint.setStyle(Paint.Style.STROKE);mEraserPaint.setStrokeJoin(Paint.Join.ROUND);mEraserPaint.setStrokeWidth(30);mPath = new Path();mBitmap = Bitmap.createBitmap(SCREEN_W, SCREEN_H, Config.ARGB_8888);mCanvas = new Canvas(mBitmap);}@Overrideprotected void onDraw(Canvas canvas) {if (mBitmap != null) {canvas.drawBitmap(mBitmap, 0, 0, mPaint);}super.onDraw(canvas);}private void touch_start(float x, float y) {mPath.reset();mPath.moveTo(x, y);mX = x;mY = y;//如果是“畫筆”模式就用mPaint畫筆進行繪制if (mMode == Pen) {mCanvas.drawPath(mPath, mPaint);}//如果是“橡皮擦”模式就用mEraserPaint畫筆進行繪制if (mMode == Eraser) {mCanvas.drawPath(mPath, mEraserPaint);}}private void touch_move(float x, float y) {float dx = Math.abs(x - mX);float dy = Math.abs(y - mY);if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);mX = x;mY = y;if (mMode == Pen) {mCanvas.drawPath(mPath, mPaint);}if (mMode == Eraser) {mCanvas.drawPath(mPath, mEraserPaint);}}}private void touch_up() {mPath.lineTo(mX, mY);if (mMode == Pen) {mCanvas.drawPath(mPath, mPaint);}if (mMode == Eraser) {mCanvas.drawPath(mPath, mEraserPaint);}}@Overridepublic boolean onTouchEvent(MotionEvent event) {float x = event.getX();float y = event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:touch_start(x, y);invalidate();break;case MotionEvent.ACTION_MOVE:touch_move(x, y);invalidate();break;case MotionEvent.ACTION_UP:touch_up();invalidate();break;}return true;}}
}


[1] 一個簡單涂鴉http://www.jizhuomi.com/android/example/19.html

[2] 安卓實現代碼的多種方式?

[3] 安卓path類的lineTo和quadTo方法的區別?

[4] 帶畫板http://www.mamicode.com/info-detail-1480421.html

轉載于:https://www.cnblogs.com/hellokittyblog/p/9128418.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/455376.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/455376.shtml
英文地址,請注明出處:http://en.pswp.cn/news/455376.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何寫年終總結(轉)

很多人不重視年終總結,覺得是一個非常令人厭煩的任務,往往是應付了事,短短幾百字,對目前工作中存在的問題發現不夠,思考不足,對自己一年的評價和未來一年的定位沒有說明。造成的后果就是公司得不到來自基層…

cad移動時捕捉不到基點_CAD入門必備(一)移動和復制新手必看

cad也瘋狂前言:CAD繪圖之所以能夠取代手工繪圖,很大的一部分原因是因為它可以很方便的修改和重復利用,例如外參可以節省很大部分時間。而我們在使用CAD中,用得最頻繁的功能就是移動和復制了,當然這也是新手必備的其中一…

H.264軟件解碼器在PXA270平臺上的優化

羅 嶸,何 苦 時間:2009年04月24日摘 要: 研究了嵌入式系統中H.264 Baseline軟件解碼器設計和優化的問題,提出了四種有效的優化方法,并在PXA270平臺上進行了測試。測試結果顯示,綜合使用提出的四種方法,H.26…

JavaWeb筆記04-解決GET與POST亂碼問題

解決GET與POST亂碼問題: 請求的亂碼問題 GET:tomcat8版本之前,get請求會亂碼 正常文字 --> UTF-8編碼 --> 字節數組 --> ISO-8859-1 編碼 --> 亂碼文字 正常文字 <-- UTF-8編碼 <-- 字節數組 <-- ISO-8859-1 編碼 <-- 亂碼文字解決亂碼的兩種格式: …

EF架構~codeFirst從初始化到數據庫遷移

一些介紹 CodeFirst是EntityFrameworks的一種開發模式&#xff0c;即代碼優先&#xff0c;它以業務代碼為主&#xff0c;通過代碼來生成數據庫&#xff0c;并且加上migration的強大數據表比對功能來生成數據庫版本&#xff0c;讓程序開發人員不用維護數據庫的變更&#xff0c;而…

Ubuntu用戶Steam控制器不工作的解決辦法

Steam 控制器已開始送貨到世界各地游戲玩家手中&#xff0c;不過有朋友遇到 Steam 控制器在 Ubuntu 中無法正常工作&#xff0c;本文我們來介紹一下解決辦法。該解決辦法并非 Ubuntu 官方提出的最佳解決方案&#xff0c;不過還是可以臨時解決 Ubuntu 用戶 Steam 控制器不工作的…

lisp 車位塊自動編號_機械車位做產權登記,真的適合嗎?

為了更好地把握停車市場發展動向&#xff0c;給停車行業從業者提供一個發表觀點、各抒己見的平臺&#xff0c;共同促進停車行業的發展&#xff0c;《城市停車》開設熱點版塊&#xff0c;每期針對1-2個行業熱點&#xff0c;廣泛征集業內人士觀點和看法。HOT TOPIC本期熱點今年兩…

JavaWeb筆記05-解決線程安全問題

線程安全問題: Servlet的service方法,每次被請求是,調用. 這個調用很特殊,是在新的子線程中調用的,當service方法執行完畢,子線程死亡了. 可以簡單的理解為:service方法每次執行都是一個新的線程. package cn.xdl.demo1;import javax.servlet.ServletException; import jav…

Java并發編程實戰 第14章 構建自定義的同步工具

狀態依賴性 定義&#xff1a;只有滿足特定的狀態才能繼續執行某些操作&#xff08;這些操作依賴于固定的狀態&#xff0c;這些狀態需要等待別的線程來滿足&#xff09;。 FutureTask&#xff0c;Semaphroe&#xff0c;BlockingQueue等&#xff0c;都是狀態依賴性的類。 條件隊列…

webserver接口_SpringBoot內置源碼解析WebServer初始化過程

WebServer 初始化過程在上一節中 Spring Boot 初始化了 WebServer 對應的工廠類。同時&#xff0c;我們也知道對應 Web容器的WebServer實現類有:TomcatWebServer、JettyWebServer和UndertowWebServer。這節重點講解這些 WebServer 是如何被初始化&#xff0c;又如何啟動的。Web…

提升應用程序彈性:保障工作負載正常運行

通過集群化、復制、快照、微服務和應用程序設計來提高企業工作負載的應用程序彈性和可用性。 應用程序的彈性和可用性是現代企業工作負載的關鍵屬性。應用程序需要在硬件故障發生后&#xff0c;扛過服務故障(例如負載平衡器和域名系統錯誤)保持工作狀態&#xff0c;并且可以忍受…

JDBC筆記01-JDBC,Connection,Statement,ResultSet,PreparedStatement,Properties

學習目標 理解JDBC原理 掌握Connection接口的使用 掌握Statement接口的使用 掌握ResultSet接口的使用 掌握PreparedStatement接口的使用 掌握Properties類與配置文件的使用 JDBC 概念 JDBC (Java DataBase Connectivity) Java數據庫連接技術的簡稱&#xff0c;提供連接各種常…

NVDKC6416平臺H.264算法優化

本文轉載自&#xff1a;http://blog.csdn.net/embedesign/archive/2009/09/15/4556486.aspx&#xff0c;版權歸原作者&#xff0c;編輯&#xff1a;小乙哥 多媒體通信終端設備具有廣泛的應用前景&#xff0c;可以應用于視頻會議、可視電話、PDA、數字電視等各個領域&#xff0…

攔截器及 Spring MVC 整合

一、實驗介紹 1.1 實驗內容 本節課程主要利用 Spring MVC 框架實現攔截器以及 Spring MVC 框架的整合。 1.2 實驗知識點 Spring MVC 框架攔截器1.3 實驗環境 JDK1.8Eclipse JavaEE二、實驗步驟 2.1 攔截器實現 在項目 hrms 的目錄 src/main/java 下新建包 com.shiyanlou.interc…

高德地圖軌跡回放_高德地圖上線了一個新功能….

文、路人甲TM德地圖這兩天剛上線了一個叫做「家人地圖」的功能&#xff0c;所謂家人地圖顧名思義&#xff0c;就是你可以通過高德地圖組建一個家人圈&#xff0c;在這個圈子里面你可以看到你的家人在什么位置&#xff0c;當你的家人到達什么位置的時候自動發送通知或者警告&…

You have new mail in /var/spool/mail/root消除提示的方法

有時在進入系統的時候經常提示You have new mail in /var/spool/mail/root 你覺得煩人---解決方法&#xff1a; 修改系統配置文件/etc/profile&#xff0c;告訴系統不要去檢查郵箱. 具體操作&#xff1a;命令行輸入&#xff1a;echo "unset MAILCHECK" >> /etc…

c3p0-config.xml文件簡單說明與備忘

<?xml version"1.0" encoding"UTF-8"?> <c3p0-config><named-config name"mysql"><!-- 配置數據庫用戶名 --><property name"user">root</property><!-- 配置數據庫密碼 --><property…

python 消息隊列 get是從隊首還是隊尾取東西_從零開始Python對redis作為消息隊列的使用...

一、Redis 服務1、安裝yum install redis2、 python安裝支持模塊/opt/python2.7.13/bin/pip install redis3、 和redis的簡單直接交互In [1]: import redisIn [2]: rc redis.Redis(host192.168.8.237,port6379,decode_responsesTrue)In [5]: rc.set(imoocc,jeson)Out[5]: True…

‘’和“”

單引號引的數據是char類型的 雙引號引的數據是String類型的 單引號只能引一個字符 而雙引號可以引0個及以上 字符&#xff08;Character&#xff09;是指人類語言的最小的表義符號&#xff0c;字符是指計算機中使用的字母、數字和符號&#xff0c;包括1、2、3、A、B、C、#、&am…

Spring整合Quartz定時任務 在集群、分布式系統中的應用(Mysql數據庫環境)

轉載&#xff1a;http://www.cnblogs.com/jiafuwei/p/6145280.html 單個Quartz實例能給予你很好的Job調度能力&#xff0c;但它不能滿足典型的企業需求&#xff0c;如可伸縮性、高可靠性滿足。假如你需要故障轉移的能力并能運行日益增多的 Job&#xff0c;Quartz集群勢必成為你…