Android采用雙緩沖技術(shù)實(shí)現(xiàn)畫板
本文實(shí)例為大家分享了Android實(shí)現(xiàn)畫板的具體代碼,采用的技術(shù)是雙緩沖技術(shù),供大家參考,具體內(nèi)容如下
1.雙緩沖技術(shù)的概念
所謂的雙緩沖技術(shù)其實(shí)很簡(jiǎn)單,當(dāng)程序需要在指定的View上進(jìn)行繪制時(shí),程序并不需要直接繪制到該View組件,而是先繪制到一個(gè)內(nèi)存中的Bitmap圖片上(就是緩沖),等內(nèi)存中的Bitmap繪制好之后,再一次性將Bitmap繪制到View組件上。
2.Android采用雙緩沖實(shí)現(xiàn)畫板
實(shí)現(xiàn)的思路:
1).定義一個(gè)內(nèi)存中圖片,將他作為緩沖區(qū)Bitmap cacheBitmap = null;
2).定義緩沖區(qū)Cache的Canvas對(duì)象 Canvas cacheCanvas = null;
3).設(shè)置cacheCanvas將會(huì)繪制到內(nèi)存的bitmap上。
cacheCanvas.setBitmap(cacheBitmap);
4). 將cacheBitmap繪制到該View上.
canvas.drawBitmap(cacheBitmap,0,0,p);
3.代碼實(shí)現(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
*@描述 : 使用雙緩存技術(shù)實(shí)現(xiàn)繪制
*@時(shí)間 : 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;
//定義一個(gè)內(nèi)存中圖片,將他作為緩沖區(qū)
Bitmap cacheBitmap = null;
//定義緩沖區(qū)Cache的Canvas對(duì)象
Canvas cacheCanvas = null;
public DrawView(Context context) {
this(context,null);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
//創(chuàng)建一個(gè)與該VIew相同大小的緩沖區(qū)
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH,VIEW_HEIGHT,Bitmap.Config.ARGB_8888);
//創(chuàng)建緩沖區(qū)Cache的Canvas對(duì)象
cacheCanvas = new Canvas();
path = new Path();
//設(shè)置cacheCanvas將會(huì)繪制到內(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) {
//獲取拖動(dòng)時(shí)間的發(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.實(shí)現(xiàn)的效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- android實(shí)現(xiàn)簡(jiǎn)單的畫畫板實(shí)例代碼
- Android實(shí)現(xiàn)畫板、寫字板功能(附源碼下載)
- Android自定義SurfaceView實(shí)現(xiàn)畫板功能
- Android畫板開發(fā)之添加背景和保存畫板內(nèi)容為圖片
- Android畫板開發(fā)之橡皮擦功能
- Android實(shí)現(xiàn)畫畫板案例
- Android編程實(shí)現(xiàn)畫板功能的方法總結(jié)【附源碼下載】
- Android畫板開發(fā)之撤銷反撤銷功能
- Android多媒體之畫畫板開發(fā)案例分享
- Android自定義View實(shí)現(xiàn)簡(jiǎn)易畫板功能
相關(guān)文章
利用Kotlin開發(fā)你的第一個(gè)Android應(yīng)用
Kotlin最近真的是大熱啊,所以下面這篇文章主要給大家介紹了關(guān)于利用Kotlin開發(fā)你的第一個(gè)Android應(yīng)用的相關(guān)資料,文中將實(shí)現(xiàn)的步驟介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-10-10
Android?hid發(fā)送apdu格式數(shù)據(jù)示例詳解
這篇文章主要介紹了Android?hid發(fā)送apdu格式數(shù)據(jù),在?Android?中,如果你想通過?HID(Human?Interface?Device)發(fā)送?APDU?格式的數(shù)據(jù),通常會(huì)涉及?USB?HID?設(shè)備或藍(lán)牙?HID?設(shè)備,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-08-08
Android Scroll滑動(dòng)效果實(shí)例
這篇文章主要為大家分享了Android Scroll滑動(dòng)效果實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Android Gradle Build Error:Some file crunching failed, see l
這篇文章主要介紹了Android Gradle Build Error:Some file crunching failed, see logs for details的快速解決方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器
這篇文章主要介紹了Android程序開發(fā)通過HttpURLConnection上傳文件到服務(wù)器的相關(guān)資料,需要的朋友可以參考下2016-01-01

