欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android采用雙緩沖技術(shù)實現(xiàn)畫板

 更新時間:2021年05月19日 09:31:56   作者:請叫我小東子  
這篇文章主要為大家詳細(xì)介紹了Android采用雙緩沖技術(shù)實現(xiàn)畫板的相關(guān)資料,思路清晰,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)畫板的具體代碼,采用的技術(shù)是雙緩沖技術(shù),供大家參考,具體內(nèi)容如下

1.雙緩沖技術(shù)的概念

所謂的雙緩沖技術(shù)其實很簡單,當(dāng)程序需要在指定的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).設(shè)置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
*@描述 : 使用雙緩存技術(shù)實現(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();
  //設(shè)置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)容,希望對大家學(xué)習(xí)Android軟件編程有所幫助。

相關(guān)文章

最新評論