该实例可以在屏幕上绘制任意的图形,同时还可以通过菜单项调整画笔的颜色和笔触的大小
DrawView.java
package com.example.doublebuffering;
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{
Paint paint;
Path path;
//定义一个内存中的图片,该图片将作为缓冲区
Bitmap cacheBitmap=null;
//定义图片的宽度和高度
final int VIEW_WIDTH=480;
final int VIEW_HEIGHT=800;
//前一个点的x,y的值
float preX;
float preY;
//定义cacheBitmap上的canvas对象
Canvas cacheCanvas=null;
public DrawView(Context context,AttributeSet attrs) {
super(context,attrs);
paint=new Paint();
path=new Path();
//创建一个与该View相同大小的缓冲区
cacheBitmap=Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT, Config.ARGB_8888);
cacheCanvas=new Canvas();
//将cacheCanvas绘制到cacheBitmap上
cacheCanvas.setBitmap(cacheBitmap);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
paint.setColor(Color.RED);
//抗锯齿
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
protected void onDraw(Canvas canvas) {
Paint bmpPaint=new Paint();
canvas.drawBitmap(cacheBitmap, 0,0,bmpPaint);
//沿着path绘制
canvas.drawPath(path, paint);
}
}
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/balck" android:title="@string/color_black"/>
<item android:id="@+id/blue" android:title="@string/color_blue"/>
<item android:id="@+id/green" android:title="@string/color_green"/>
</group>
</menu>
</item>
<item android:title="@string/paitnt_width">
<menu>
<!-- 定义一组菜单项 -->
<group>
<item android:id="@+id/width_1" android:title="@string/width_1"></item>
<item android:id="@+id/width_2" android:title="@string/width_2"></item>
<item android:id="@+id/width_3" android:title="@string/width_3"></item>
</group>
</menu>
</item>
</menu>
values/stringx.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">DoubleBuffering</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="color">颜色</string>
<string name="color_black">黑色</string>
<string name="color_green">绿色</string>
<string name="color_blue">蓝色</string>
<string name="paitnt_width">笔触大小</string>
<string name="width_1">1</string>
<string name="width_2">2</string>
<string name="width_3">3</string>
</resources>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<com.example.doublebuffering.DrawView
android:id="@+id/my_draw_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
MainActivity.java
package com.example.doublebuffering;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//负责创建菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=new MenuInflater(MainActivity.this);
//装载菜单资源
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//当菜单项被单击后的回调方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
DrawView view=(DrawView) super.findViewById(R.id.my_draw_view);
//判断点击的是哪个菜单项
switch(item.getItemId()){
case R.id.balck:
view.paint.setColor(Color.BLACK);
item.setChecked(true);
break;
case R.id.blue:
view.paint.setColor(Color.BLUE);
item.setChecked(true);
break;
case R.id.green:
view.paint.setColor(Color.GREEN);
item.setChecked(true);
break;
case R.id.width_1:
view.paint.setStrokeWidth(1);
break;
case R.id.width_2:
view.paint.setStrokeWidth(2);
break;
case R.id.width_3:
view.paint.setStrokeWidth(3);
break;
}
return true;
}
}