1.什麼是雙緩沖技術?雙緩沖技術就是當用戶操作界面完成後,會有一個緩沖區保存用戶操作的結果。
為什麼要使用雙緩沖技術?拿Android 游戲開發來說,界面貞每次都是全部重畫的,也就說畫了新的,舊的就沒了,所以需要使用雙緩沖技術保存之前的內容。
如何實現雙緩沖?使用一個Bitmap對象保留之前的畫布即可。
查看源碼打印?
01
package com.example.phonegaptest;
02
03
import android.content.Context;
04
import android.graphics.Bitmap;
05
import android.graphics.Bitmap.Config;
06
import android.graphics.Canvas;
07
import android.graphics.Color;
08
import android.graphics.Paint;
09
import android.graphics.Path;
10
import android.util.AttributeSet;
11
import android.view.MotionEvent;
12
import android.view.View;
13
14
public class DrawView extends View {
15
float preX;
16
float preY;
17
private Path path;
18
public Paint paint = null;
19
final int VIEW_WIDTH = 320;
20
final int VIEW_HEIGHT = 480;
21
Bitmap cacheBitmap = null;//定義一個內存中的圖片,這張圖片會作為緩沖區
22
Canvas cacheCanvas = null;//定義cacheBitmap上的畫布cacheCanvas
23
24
public DrawView(Context context, AttributeSet set) {
25
super(context, set);
26
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT,
27
Config.ARGB_8888);//創建緩沖區
28
cacheCanvas = new Canvas();
29
30
path = new Path();
31
cacheCanvas.setBitmap(cacheBitmap);
32
33
paint = new Paint(Paint.DITHER_FLAG);
34
paint.setColor(Color.RED);
35
paint.setStyle(Paint.Style.STROKE);
36
paint.setStrokeWidth(1);
37
paint.setAntiAlias(true);
38
paint.setDither(true);
39
}
40
41
@Override
42
public boolean onTouchEvent(MotionEvent event) {
43
float x = event.getX();
44
float y = event.getY();
45
46
switch (event.getAction()) {
47
case MotionEvent.ACTION_DOWN:
48
path.moveTo(x, y);
49
preX = x;
50
preY = y;
51
break;
52
case MotionEvent.ACTION_MOVE:
53
path.quadTo(preX, preY, x, y);
54
preX = x;
55
preY = y;
56
break;
57
case MotionEvent.ACTION_UP:
58
cacheCanvas.drawPath(path, paint);
59
path.reset();
60
break;
61
}
62
invalidate();
63
return true;//返回true表明處理方法已經處理該事件,該事件不會擴散
64
}
65
66
@Override
67
protected void onDraw(Canvas canvas) {
68
super.onDraw(canvas);
69
Paint bmpPaint = new Paint();
70
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
71
canvas.drawPath(path, paint);
72
}
73
74
}
2.XMl文件實現菜單功能
XMl文件如下:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/close"
android:icon="@drawable/ic_launcher"
android:orderInCategory="3"
android:title="Close"/>
<item
android:id="@+id/no_icon"
android:orderInCategory="2"
android:title="Sans Icon"/>
<item
android:id="@+id/disabled"
android:enabled="true"
android:orderInCategory="4"
android:title="Disabled"/>
<!-- orderInCategory表示的是item的排序-->
<group
android:id="@+id/other_stuff"
android:menuCategory="secondary"
android:visible="true" >
<item
android:id="@+id/later"
android:orderInCategory="0"
android:title="2nd-To-Last"/>
<item
android:id="@+id/last"
android:orderInCategory="1"
android:title="Last"/>
</group>
<!--表示的是二級菜單-->
<item
android:id="@+id/submenu"
android:orderInCategory="3"
android:title="A Submenu">
<menu>
<item
android:id="@+id/non_ghost"
android:alphabeticShortcut="n"
android:title="Non-Ghost"
android:visible="true"/>
<item
android:id="@+id/ghost"
android:alphabeticShortcut="g"
android:title="A Ghost"
android:visible="true"/>
</menu>
</item>
</menu>
主文件
[java] view plaincopy
package com.example.receiveractivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class ReceiverActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG="BruceZhang";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater mflater=new MenuInflater(this);
mflater.inflate(R.menu.activity_receiver, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.close:
Log.v(TAG, "------------close");
break;
case R.id.no_icon:
Log.v(TAG, "------------noicon");
break;
case R.id.submenu:
Log.v(TAG, "------------submenu");
break;
case R.id.non_ghost:
Log.v(TAG, "------------non_ghost");
break;
case R.id.ghost:
Log.v(TAG, "------------ghost");
break;
case R.id.disabled:
Log.v(TAG, "------------disabled");
break;
case R.id.last:
Log.v(TAG, "------------last");
break; www.2cto.com
case R.id.later:
Log.v(TAG, "------------laster");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}