Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之用雙緩沖技術畫圖

Android開發之用雙緩沖技術畫圖

編輯:關於Android編程

雙緩沖技術主要用在繪圖,動畫效果上,其原理就是:將資源先加載到緩沖區,然後再將緩沖區整個加載到View上面去。雙緩沖技術能夠有效防止閃爍,提高顯示質量。

DrawView.java:

package com.example.handdraw;


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{

float preX;

float preY;

private Path path;

public Paint paint = null;

final int VIEW_WIDTH = 720;

final int VIEW_HEIGHT = 800;

//定義一個內存中的圖片,該圖片將作為緩沖區

Bitmap cacheBitmap = null;

//定義cacheBitmap上的Canvas對象

Canvas cacheCanvas = null;


public DrawView(Context context ,AttributeSet set) {

super(context, set);

// TODO Auto-generated constructor stub

//創建一個與該View相同大小的緩沖區

cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT, Config.ARGB_8888);

cacheCanvas = new Canvas();

path = new Path();

//設置cacheCanvas將會繪制到內存中的cacheBitmap上

cacheCanvas.setBitmap(cacheBitmap);

//設置畫筆的顏色

paint = new Paint(Paint.DITHER_FLAG);

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);

path.reset();

break;

}

invalidate();

//表明處理方法已經處理改事件

return true;

}


@Override

protected void onDraw(Canvas canvas) {

Paint bmpPaint = new Paint();

//將cacheBitmap繪制到該View組件上

canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);

//沿著path繪制

canvas.drawPath(path, paint);

}

}

my_menu.xml:

vcg==">

Activity.java:

package com.example.handdraw;


import android.os.Bundle;
import android.app.Activity;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;


public class MainActivity extends Activity {

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.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
DrawView dv = (DrawView)findViewById(R.id.draw);
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(1);
break;
case R.id.width_3:
dv.paint.setStrokeWidth(3);
break;
case R.id.width_5:
dv.paint.setStrokeWidth(5);
break;
case R.id.blur:
dv.paint.setMaskFilter(blur);
break;
case R.id.emboss:
dv.paint.setMaskFilter(emboss);
break;
}
return true;
}
}

main.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >


android:id="@+id/draw"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved