Android中兑现双缓冲(画板应用)和XML文件定义菜单
Android中实现双缓冲(画板应用)和XML文件定义菜单
主文件
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文件如下:
<?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>
主文件
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; case R.id.later: Log.v(TAG, "------------laster"); break; default: break; } return super.onOptionsItemSelected(item); } }
- 1楼MaSurQ前天 15:36
- 相当好 ,学习中