Android图形与图像处理-采用双缓冲实现画图板

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>


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页