Implement dual-buffering (canvas application) and XML file definition menus in Android

Source: Internet
Author: User

1. What is double buffering technology? The dual-buffer technology means that after the user operation interface is complete, a buffer zone will be provided to save the user operation results. Why is dual-buffer technology used? For Android game development, the UI is all re-painted every time, so it is said that there is a new image, and the old one is gone, so we need to use the dual-buffer technology to save the previous content. How to implement dual buffering? Use a Bitmap object to retain the previous canvas. View Source code printing? 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 extend S 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; // defines an image in the memory. This image is used as the buffer zone 22 Canvas cacheCanvas = null; // defines the Canvas cacheCanvas23 24 public DrawView (Context context Context, attributeSet) {25 super (context, set); 26 cacheBitmap = Bitmap. createBitmap (VIEW_WIDTH, VIEW_HEIGHT, 27 Config. ARGB_8888); // create the buffer 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 @ Override42 public boolean onTouchEvent (MotionEvent event) {43 float x = event. g EtX (); 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; // return true indicates that the processing method has handled the event. Event will not spread 64} 65 66 @ Override67 protected void onDraw (Canvas canvas) {68 super. onDraw (canvas); 69 Paint BMP Paint = new Paint (); 70 canvas. drawBitmap (cacheBitmap, 0, 0, BMP paint); 71 canvas. drawPath (path, paint); 72} 73 74} 2. XMl file implementation menu function XMl file: [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 indicates the sorting of items --> <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> <! -- Indicates the level-2 menu --> <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> main file [java] view plaincopypackage 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_cycler) ;}@ 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 );}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.