Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android項目 之 記事本(10) ----- 手寫功能之設置畫筆大小和畫筆顏色

android項目 之 記事本(10) ----- 手寫功能之設置畫筆大小和畫筆顏色

編輯:關於Android編程

上一節,實現了畫板的所有功能,包括設置畫筆大小,設置畫筆顏色,橡皮擦等功能,而手寫,也可以添加設置筆跡大小和顏色的功能,這節就順勢實現手寫的調整筆跡大小和調整筆跡的顏色。

先看圖:

\\

其實,手寫和畫板的這些功能都類似,直接復用之前的代碼就行,原理如下:

1. 設置畫筆大小的功能:

1)初始化畫筆。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgICAgICAgICAgICAgIDKjqcno1sO7rbHKtcS089ChzqrL+dGh1PG1xLTz0KGhozwvcD4KPHA+ICAgICAgICAgICAgICAgICAgIDOjqdPD0ru49rHkwb+8x9ehtbHHsLutscq1xLTz0KGjrNPD09rU2r340NDG5Mv7stnX97rzu7mxo7PW1q7HsMno1sO1xLutscq089ChoaM8L3A+CjxwPiAgIDwvcD4KPHA+ICAgICAgPHN0cm9uZz4yLiDJ6NbDu62xytHVyau1xLmmxNyjujwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz4gICAgICAgICAgICAgIDwvc3Ryb25nPiAgICAgMaOps/XKvLuvu62xyqGjPC9wPgo8cD4gICAgICAgICAgICAgICAgICAgMqOpyejWw7utscq1xNHVyavOqsv50aHU8bXE0dXJq6GjPC9wPgo8cD4gICAgICAgICAgICAgICAgICAgM6Op08PSu7j2seTBv7zH16G1scewu62xyrXE0dXJq6Os08PT2tTavfjQ0Mbky/uy2df3uvO7ubGjs9bWrsewyejWw7XEu62xytHVyauhozwvcD4KPHA+ICAgIDwvcD4KPHA+ICAgICDW99KqtPrC66Oo1NpUb3VjaFZpZXfW0MztvNOjqcjnz8KjujwvcD4KPHByZSBjbGFzcz0="brush:java;">private Paint mPaint; private int currentColor = Color.RED; private int currentSize = 5;

   //設置畫筆樣式 
    public void setPaintStyle(){
 	    mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(currentSize);
        mPaint.setColor(currentColor);
    }

設置畫筆大小:

         //設置畫筆的大小 
	public void selectHandWritetSize(int which){
		int size =Integer.parseInt(this.getResources().getStringArray(R.array.paintsize)[which]); 
    	currentSize = size;
    	setPaintStyle();
	}

設置畫筆顏色:

    //設置畫筆顏色
    public void selectHandWriteColor(int which){
    	
    	currentColor = paintColor[which];
    	setPaintStyle();
    }
	


在自定義View中添加這些方法後,接下來就是在Activity中調用這些方法了,同上節(在HandWriteActivity.java中添加如下代碼):

	//畫筆大小
	  case 0:
	  	showPaintSizeDialog(view);
	  	break;
	  //顏色
	  case 1:
	  	showPaintColorDialog(view);
	  	break;

通過點擊這兩個按鈕,彈出對話框,用於選擇畫筆的大小和顏色

showPaintSizeDialog(view),彈出選擇畫筆大小的對話框,如下:

  		//彈出畫筆大小選項對話框
	  	public void showPaintSizeDialog(View parent){
	  		
	  		
	  		
	  		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this,R.style.custom_dialog);
			alertDialogBuilder.setTitle("選擇畫筆大小:");
			
			alertDialogBuilder.setSingleChoiceItems(R.array.paintsize, select_handwrite_size_index, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					select_handwrite_size_index = which;
					touchView.selectHandWritetSize(which);
					dialog.dismiss();
				}
			});
			
			alertDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			alertDialogBuilder.create().show();
	  	}


showPaintColorDialog(view):彈出顏色選擇對話框,如下:

		//彈出畫筆顏色選項對話框
	  	public void showPaintColorDialog(View parent){
	  		
	  		
	  		
	  		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this,R.style.custom_dialog);
			alertDialogBuilder.setTitle("選擇畫筆顏色:");
			
			alertDialogBuilder.setSingleChoiceItems(R.array.paintcolor, select_handwrite_color_index, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					select_handwrite_color_index = which;
					touchView.selectHandWriteColor(which);
					dialog.dismiss();
				}
			});
			
			alertDialogBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			alertDialogBuilder.create().show();
	  	}

裡面同樣也用到了上節提到的arrays.xml

arrays.xml



    
        @string/track_line
        @string/Eraser
    
    
        5
        10
        15
        20
        25
        30
    
 
    
        RED
        BLUE
        BLACK
        GREEN
        YELLOW
        CYAN
        LTGRAY
    
    

至此,手寫的功能,也已基本全部完成,接下來就剩將手寫的字保存成文件,並返回保存文件的路徑存儲到數據庫,後續繼續完善。






  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved