双缓冲画图:
双缓冲画图 顾名思义 就是在内存中画一张图片,画完后,在把整张图片贴到界面中去,通过双缓冲画图可以有效防止因为多次刷新界面导致界面闪烁的问题
在Android中使用双缓冲画图
1 自定义一个View 主要实现画图功能
package com.example.myview;
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 {
private float preX;
private float preY;
private Path path;
private final int VIEW_WIDTH = 320;
private final int VIEW_HEIGHT = 480;
private Bitmap bitmap = null;
Canvas CacheCanvas = null;
public Paint paint;
public DrawView(Context context)
{
super(context);
initview();
}
public DrawView(Context context,AttributeSet set)
{
super(context,set);//放在最前面
initview();
}
public void initview()
{
bitmap = Bitmap.createBitmap(VIEW_WIDTH,VIEW_HEIGHT,Config.ARGB_8888);
CacheCanvas = new Canvas();
path = new Path();
CacheCanvas.setBitmap(bitmap);
paint = new Paint();
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);//使用双缓冲技术画图
break;
}
invalidate();
return true;//true 表明处理方法已经处理该事件
}
@Override
public void onDraw(Canvas canvas)
{
Paint bmpPaint = new Paint();
canvas.drawBitmap(bitmap, 0, 0,bmpPaint);
canvas.drawPath(path, paint);
}
}
2 定义一个菜单 ,用于选择不同的字体和大小
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:title="@string/color">
<menu>
<group>
<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>
<item android:id="@+id/width_1" android:title="@string/width_1"/>
<item android:id="@+id/width_2" android:title="@string/width_2"/>
<item android:id="@+id/width_3" android:title="@string/width_3"/>
</group>
</menu>
</item>
<item android:id="@+id/blur" android:title="@string/blur"/>
<item android:id="@+id/emboss" android:title= "@string/emboss"/>
</menu>
3 java源代码,实现画图功能
package com.example.handdrawtest;
import com.example.myview.DrawView;
import android.content.res.Resources;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
EmbossMaskFilter emboss;
BlurMaskFilter blur;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflator = new MenuInflater(this);
inflator.inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
DrawView dv = (DrawView)findViewById(R.id.draw);
Resources resources = this.getResources();
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(Integer.parseInt(resources.getText(R.string.width_1).toString()));
item.setChecked(true);
break;
case R.id.width_2:
dv.paint.setStrokeWidth(Integer.parseInt(resources.getText(R.string.width_2).toString()));
break;
case R.id.width_3:
dv.paint.setStrokeWidth(Integer.parseInt(resources.getText(R.string.width_3).toString()));
break;
case R.id.blur:
dv.paint.setMaskFilter(blur);
break;
case R.id.emboss:
dv.paint.setMaskFilter(emboss);
break;
}
return true;
}
}