Android采用雙緩沖技術實現(xiàn)畫板
本文實例為大家分享了Android實現(xiàn)畫板的具體代碼,采用的技術是雙緩沖技術,供大家參考,具體內(nèi)容如下
1.雙緩沖技術的概念
所謂的雙緩沖技術其實很簡單,當程序需要在指定的View上進行繪制時,程序并不需要直接繪制到該View組件,而是先繪制到一個內(nèi)存中的Bitmap圖片上(就是緩沖),等內(nèi)存中的Bitmap繪制好之后,再一次性將Bitmap繪制到View組件上。
2.Android采用雙緩沖實現(xiàn)畫板
實現(xiàn)的思路:
1).定義一個內(nèi)存中圖片,將他作為緩沖區(qū)Bitmap cacheBitmap = null;
2).定義緩沖區(qū)Cache的Canvas對象 Canvas cacheCanvas = null;
3).設置cacheCanvas將會繪制到內(nèi)存的bitmap上。
cacheCanvas.setBitmap(cacheBitmap);
4). 將cacheBitmap繪制到該View上.
canvas.drawBitmap(cacheBitmap,0,0,p);
3.代碼實現(xiàn)
package com.lidong.android_ibrary.view;
import android.content.Context;
import android.graphics.Bitmap;
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;
/**
*@類名 : DrawView
*@描述 : 使用雙緩存技術實現(xiàn)繪制
*@時間 : 2016/4/26 9:18
*@作者: 李東
*@郵箱 : lidong@chni.com.cn
*@company: chni
*/
public class DrawView extends View {
float preX;
float preY;
private Path path;
private Paint paint = null;
private int VIEW_WIDTH = 800;
private int VIEW_HEIGHT = 600;
//定義一個內(nèi)存中圖片,將他作為緩沖區(qū)
Bitmap cacheBitmap = null;
//定義緩沖區(qū)Cache的Canvas對象
Canvas cacheCanvas = null;
public DrawView(Context context) {
this(context,null);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
//創(chuàng)建一個與該VIew相同大小的緩沖區(qū)
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH,VIEW_HEIGHT,Bitmap.Config.ARGB_8888);
//創(chuàng)建緩沖區(qū)Cache的Canvas對象
cacheCanvas = new Canvas();
path = new Path();
//設置cacheCanvas將會繪制到內(nèi)存的bitmap上
cacheCanvas.setBitmap(cacheBitmap);
paint = new Paint();
paint.setColor(Color.RED);
paint.setFlags(Paint.DITHER_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
paint.setDither(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
//將cacheBitmap繪制到該View
canvas.drawBitmap(cacheBitmap,0,0,p);
canvas.drawPath(path,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//獲取拖動時間的發(fā)生位置
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:
//這是是調(diào)用了cacheBitmap的Canvas在繪制
cacheCanvas.drawPath(path,paint);
path.reset();
break;
}
invalidate();//在UI線程刷新VIew
return true;
}
}
4.實現(xiàn)的效果

代碼下載:Android實現(xiàn)畫板代碼
以上就是本文的全部內(nèi)容,希望對大家學習Android軟件編程有所幫助。
相關文章
Android?hid發(fā)送apdu格式數(shù)據(jù)示例詳解
這篇文章主要介紹了Android?hid發(fā)送apdu格式數(shù)據(jù),在?Android?中,如果你想通過?HID(Human?Interface?Device)發(fā)送?APDU?格式的數(shù)據(jù),通常會涉及?USB?HID?設備或藍牙?HID?設備,本文給大家講解的非常詳細,需要的朋友可以參考下2023-08-08
Android Gradle Build Error:Some file crunching failed, see l
這篇文章主要介紹了Android Gradle Build Error:Some file crunching failed, see logs for details的快速解決方法的相關資料,需要的朋友可以參考下2016-10-10
Android程序開發(fā)通過HttpURLConnection上傳文件到服務器
這篇文章主要介紹了Android程序開發(fā)通過HttpURLConnection上傳文件到服務器的相關資料,需要的朋友可以參考下2016-01-01

