Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android簡易畫板

Android簡易畫板

編輯:關於Android編程

\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+tq+7rcbkyrW+zcrHsru2z7XEtffTw1ZpZXfX6bz+tcRvbkRyYXe3vbeoo6zDv7TO1NpWaWV31+m8/snPu+bWxrXEzbzQzrK7zay+zdDOs8nBy87Sw8e/tLW9tcS2r7utoaM8L3A+CjxwPs6qwcvIw1ZpZXfX6bz+yc+75tbGtcTNvNDOt6LJ+rjEseSjrL7N0OjSqrLJ08Ox5MG/vMfXodXi0KnK/b7d17TMrKOsyOe5+9Do0qrTw7unstnX96OsxMfDtL7N0OjSqrS0vajKwrz+vODM/cb3o6zI57n7yse2r7ut19S8urjEseTEx8O0vs3Q6NKqyrnTw7aoyrHG91RpbWVyoaPNqNaqVmlld9bYu+a/ybX308NpbnZhbGlkYXRlKNTaVUnP37PM1tApus1wb3N0SW52YWxpZGF0ZSjU2rfHVUnP37PM1tApoaM8L3A+CjxwPs/W1NrSqtf20ru49rutzbyw5aOsxuTKtc7Sw8e/tLW9tcTK1rvmysfSu9bWvNnP86OsyrW8ysnPysfA+9PDQ2FudmFztcRkcmF3TGluZbe9t6i7rdaxz9+jrMO/zPXWsc/ftrzKx9PJtuC49s3Ptq/Kwrz+way907b4s8m1xKOszai5/VBhdGjA4L/J0tS63Le9sePKtTwvcD4KPHA+z9aho9Do0qrWuLP2tcTKx9XiwO/Q6NKqyrnTw8uru7qz5by8yvWjrLy0z8iw0c28u+bWxtTa0ru49sTatObW0LXEY2FjaGVCaXRtYXDW0KOstci1vcTatObW0LXEQml0bWFwu+bWxrrD1q6689TZ0ru0ztDUvatjYWNoZUJpdG1hcLvm1sbU2lZpZXfJz6GjPC9wPgo8cD48YnI+CjwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">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.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class DrawView extends View { float preX; float preY; private Path path; public Paint paint = null; final int VIEW_WIDTH = 480; //可以根據自己手機的分辨率調整大 final int VIEW_HEIGHT = 800; // 定義一個內存中的圖片,該圖片將作為緩沖區 Bitmap cacheBitmap = null; // 定義cacheBitmap上的Canvas對象 Canvas cacheCanvas = null; public DrawView(Context context, AttributeSet set) { super(context, set); // 創建一個與該View相同大小的緩存區 cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT, Config.ARGB_8888); cacheCanvas = new Canvas(); path = new Path(); // 設置cacheCanvas將會繪制到內存中的cacheBitmap上 cacheCanvas.setBitmap(cacheBitmap); // 設置畫筆的顏色 paint = new Paint(Paint.DITHER_FLAG); paint.setColor(Color.RED); // 設置畫筆風格 paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1); // 反鋸齒 paint.setAntiAlias(true); paint.setDither(true); } @Override public boolean onTouchEvent(MotionEvent event) { // 獲取拖動事件的發生位置 float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //手按下 path.moveTo(x, y); preX = x; preY = y; break; case MotionEvent.ACTION_MOVE: //手移動 path.quadTo(preX, preY, x, y); preX = x; preY = y; break; case MotionEvent.ACTION_UP: //手放開 cacheCanvas.drawPath(path, paint); path.reset(); break; } invalidate(); // 返回true表明處理方法已經處理該事件 return true; } @Override public void onDraw(Canvas canvas) { Paint bmpPaint = new Paint(); // 將cacheBitmap繪制到該View組件上 canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint); // 沿著path繪制 canvas.drawPath(path, paint); } }

import android.app.Activity;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class HandDraw extends Activity
{  /* MaskFilter類可以為Paint分配邊緣效果。對MaskFilter的擴展可以對一個Paint邊緣的alpha通道應用轉換。
   Android包含了下面幾種MaskFilter:
   BlurMaskFilter   指定了一個模糊的樣式和半徑來處理Paint的邊緣。
   EmbossMaskFilter  指定了光源的方向和環境光強度來添加浮雕效果。
   要應用一個MaskFilter,可以使用setMaskFilter方法,並傳遞給它一個MaskFilter對象。
*/
	EmbossMaskFilter emboss;
	BlurMaskFilter blur;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		emboss = new EmbossMaskFilter(new float[] 
			{ 1.5f, 1.5f, 1.5f }, 0.6f,	6, 4.2f);
		blur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
	}

	@Override
	// 負責創建選項菜單
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflator = new MenuInflater(this);
		// 裝載R.menu.my_menu對應的菜單,並添加到menu中
		inflator.inflate(R.menu.my_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	// 菜單項被單擊後的回調方法
	public boolean onOptionsItemSelected(MenuItem mi)
	{
		DrawView dv = (DrawView) findViewById(R.id.draw);
		// 判斷單擊的是哪個菜單項,並針對性的作出響應。
		switch (mi.getItemId())
		{
			case R.id.red:
				dv.paint.setColor(Color.RED);
				mi.setChecked(true);
				break;
			case R.id.green:
				dv.paint.setColor(Color.GREEN);
				mi.setChecked(true);
				break;
			case R.id.blue:
				dv.paint.setColor(Color.BLUE);
				mi.setChecked(true);
				break;
			case R.id.width_1:
				dv.paint.setStrokeWidth(1);
				break;
			case R.id.width_3:
				dv.paint.setStrokeWidth(3);
				break;
			case R.id.width_5:
				dv.paint.setStrokeWidth(5);
				break;
			case R.id.blur:
				dv.paint.setMaskFilter(blur);
				break;
			case R.id.emboss:
				dv.paint.setMaskFilter(emboss);
				break;
		}
		return true;
	}
}



	
		
			
			
				
				
				
				
			
		
	
	
		
			
			
				
				
				
										
			
		
	
		
								



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