Android圖形與圖像處理-采用雙緩沖實現畫圖板
創建項目:HandDraw
運行項目效果:
DrawView.java
- package wwj.handdraw;
-
- 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 = 320;
- final int VIEW_HEIGHT = 480;
- //定義一個內存中的圖片,該圖片將作為緩沖區
- Bitmap cacheBitmap = null;
- //定義cacheBitmap上的Canvas對象
- Canvas cacheCanvas = null;
- public DrawView(Context context, AttributeSet attrs) {
- super(context, attrs);
- //創建一個與該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) {
- // TODO Auto-generated method stub
- //獲取拖動事件的發生位置
- 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
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- Paint bmpPaint = new Paint();
- //將cacheBitmap繪制到該View組件
- canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
- //沿著Path繪制
- canvas.drawPath(path, bmpPaint);
- }
-
- }
menu文件:my_menu.xml
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:title="@string/color">
- <menu>
- <!-- 定義一組單選菜單項 -->
- <group android:checkableBehavior="single">
- <!-- 定義多個菜單項 -->
- <item
- android:id="@+id/red" android:title="@string/color_red"/>
- <item
- android:id="@+id/green" android:title="@string/color_green"/>
- <item
- android:id="@+id/blue" android:title="@string/color_blue"/>
- </group>
- </menu>
- </item>
- <item android:title="@string/width">
- <menu>
- <!-- 定義一組菜單項 -->
- <group>
- <!-- 定義3個菜單項 -->
- <item
- android:id="@+id/width_1" android:title="@string/width_1"/>
- <item
- android:id="@+id/width_3" android:title="@string/width_3"/>
- <item
- android:id="@+id/width_5" android:title="@string/width_5"/>
- </group>
- </menu>
- </item>
- <item
- android:id="@+id/blur" android:title="@string/blur"/>
- <item
- android:id="@+id/emboss" android:title="@string/emboss"/>
- </menu>
HandDraw.java
- package wwj.handdraw;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.BlurMaskFilter;
- import android.graphics.Color;
- import android.graphics.BlurMaskFilter.Blur;
- import android.graphics.EmbossMaskFilter;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.support.v4.app.NavUtils;
-
- public class HandDraw extends Activity {
-
- 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, Blur.NORMAL);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflator = new MenuInflater(this);
- //狀態R.menu.context對應的菜單,並添加到menu中
- inflator.inflate(R.menu.my_menu, menu);
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- DrawView dv = (DrawView)findViewById(R.id.draw);
- //判斷單擊的是哪個菜單項,並有針對性地做出響應
- switch(item.getItemId()){
- case R.id.red:
- dv.paint.setColor(Color.RED);
- item.setChecked(true);
- break;
- case R.id.green:
- dv.paint.setColor(Color.GREEN);
- item.setChecked(true);
- break;
- case R.id.blue:
- dv.paint.setColor(Color.BLUE);
- item.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;
- }
- }
布局文件:main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <wwj.handdraw.DrawView
- android:id="@+id/draw"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="320px"
- android:height="480px"
- />
- </LinearLayout>
字符串文件:string.xml
- <resources>
-
- <string name="app_name">手繪</string>
- <string name="hello_world">Hello world!</string>
- <string name="menu_settings">Settings</string>
- <string name="title_activity_hand_draw">HandDraw</string>
- <string name="width_1">1像素</string>
- <string name="width_3">3像素</string>
- <string name="width_5">5像素</string>
- <string name="color_red">紅色</string>
- <string name="color_green">綠色</string>
- <string name="color_blue">藍色</string>
- <string name="color">畫筆顏色</string>
- <string name="width">畫筆寬度</string>
- <string name="blur">模糊效果</string>
- <string name="emboss">浮雕效果</string>
- </resources>