亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

本文實(shí)例為大家分享了Android實(shí)現(xiàn)畫(huà)板的具體代碼,采用的技術(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)畫(huà)板

 實(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)的效果

代碼下載:Android實(shí)現(xiàn)畫(huà)板代碼

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。

相關(guān)文章

最新評(píng)論