正文  基础知识 > 开发实例 >

Android中实现双缓冲(画板应用)和XML文件定义菜单

1.什么是双缓冲技术?双缓冲技术就是当用户操作界面完成后,会有一个缓冲区保存用户操作的结果。为什么要使用双缓冲技术?拿Android 游戏开发来说,界面贞每次都是全部重画的,也就说画了新的,旧的就没了,所以......

 1.什么是双缓冲技术?双缓冲技术就是当用户操作界面完成后,会有一个缓冲区保存用户操作的结果。
 
为什么要使用双缓冲技术?拿Android 游戏开发来说,界面贞每次都是全部重画的,也就说画了新的,旧的就没了,所以需要使用双缓冲技术保存之前的内容。
 
如何实现双缓冲?使用一个Bitmap对象保留之前的画布即可。
package com.example.phonegaptest;
 
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.content.Context;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.graphics.Bitmap;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.graphics.Bitmap.Config;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.graphics.Canvas;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.graphics.Color;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.graphics.Paint;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.graphics.Path;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.util.AttributeSet;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.view.MotionEvent;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.view.View;
 
public class DrawView extends View {
float preX;
float preY;
private Path path;
public Paint paint = null;
final int VIEW_WIDTH = 320;
final int VIEW_HEIGHT = 480;
Bitmap cacheBitmap = null;
Canvas cacheCanvas = null;
 
public DrawView(Context context, AttributeSet set) {
super(context, set);
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT,
Config.ARGB_8888);
cacheCanvas = new Canvas();
 
path = new Path();
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) {
super.onDraw(canvas);
Paint bmpPaint = new Paint();
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
canvas.drawPath(path, paint);
}
 
}
 2.XMl文件实现菜单功能
 
XMl文件如下:
 
<?xml version="1.0" encoding="utf-8"?>  
<menu xmlns:<a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>="http://schemas.<a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.com/apk/res/<a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>" >  
    <item  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/close"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:icon="@drawable/ic_launcher"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:orderInCategory="3"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="Close"/>  
    <item  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/no_icon"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:orderInCategory="2"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="Sans Icon"/>  
    <item  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/disabled"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:enabled="true"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:orderInCategory="4"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="Disabled"/>  
    <!-- orderInCategory表示的是item的排序-->  
    <group  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/other_stuff"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:menuCategory="secondary"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:visible="true" >  
        <item  
            <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/later"  
            <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:orderInCategory="0"  
            <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="2nd-To-Last"/>  
        <item  
            <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/last"  
            <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:orderInCategory="1"  
            <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="Last"/>  
    </group>  
    <!--表示的是二级菜单-->  
    <item  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/submenu"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:orderInCategory="3"  
        <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="A Submenu">  
        <menu>  
            <item  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/non_ghost"  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:alphabeticShortcut="n"  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="Non-Ghost"  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:visible="true"/>  
            <item  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:id="@+id/ghost"  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:alphabeticShortcut="g"  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:title="A Ghost"  
                <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>:visible="true"/>  
        </menu>  
    </item>  
  
</menu>  
 
主文件
 
package com.example.receiveractivity;
 
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.app.Activity;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.os.Bundle;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.util.Log;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.view.Menu;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.view.MenuInflater;
import <a title="android" href="http://www.android-study.com/pingtaikaifa/553.html">android</a>.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);  
    }