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
|
}
|
<?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);
}
}
Android中实现双缓冲(画板应用)和XML文件定义菜单相关推荐
- C++零食:WTL中使用双缓冲避免闪烁
双缓冲的原理可以这样形象的理解:把电脑屏幕看作一块黑板.首先我们在内存环境中建立一个"虚拟"的黑板,然后在这块黑板上绘制复杂的图形,等图形全部绘制完毕的时候,再一次性的把内存中绘制 ...
- VC中的双缓冲绘图技术
之前在做图形绘制的时候,发现在拖动图形时候,会出现闪烁的情况,后来就上网找了一下双缓冲绘图,本文非原创,只是想保存下来,以后要用的时候不用再到处去搜,也希望能帮助有这方面困惑的朋友 原文来自http: ...
- Qt组件中的双缓冲无闪烁绘图
双缓冲绘图 在Qt4中,所有的窗口部件默认都使用双缓冲进行绘图.使用双缓冲,可以减轻绘制的闪烁感.在有些情况下,用户要关闭双缓冲,自己管理绘图.下面的语句设置了窗口部件的Qt::WA_PaintOnS ...
- VC绘图中的双缓冲技术
VC绘图中的双缓冲技术 转自:VC 绘图,使用双缓冲技术实现 ********************所有的GDI绘图函数使用的都是逻辑坐标(逻辑范围)******************* **** ...
- Winform中的双缓冲
(1)在应用程序中使用双缓冲的最简便的方法是使用 .NET Framework 为窗体和控件提供的默认双缓冲.通过将 DoubleBuffered 属性设置为 true. this.DoubleBuf ...
- Android SurfaceView的双缓冲机制,引起的闪屏问题
SurfaceView相关目录 SurfaceView要点 SurfaceView拥有独立的Surface(绘图表面) SurfaceView是用Zorder排序的,他默认在宿主Window的后面,S ...
- java swing双缓冲_java中的双缓冲技术
毕业设计有个远程协助功能,得到对方的屏幕后,老是会闪,很是不爽,今天用java的双缓冲技术解决了.代码如下,本类重写了Swing中的JLabel,当Label重绘时,会默认的调用它的update方法, ...
- android+双卡imei,以编程方式在Android中为双SIM卡检索IMEI号码
对于单个SIM,以下代码有效: TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); String ...
- 双缓冲 android,Android 的 SurfaceView 双缓冲应用
075 int index = 0;本文引用地址:http://www.eepw.com.cn/article/201610/305442.htm 076 try { 077 index = fiel ...
最新文章
- mysql 时间日期类型 datetime、timestamp、date、time、year
- Windows Phone 7 Image Controller: Zoom In, Zoom Out, and Rotate (WP 7 图像控制器:放大,缩小,旋转)...
- Laravel模板事项
- 【ArcGIS遇上Python】窗体版Python批量处理地理数据--栅格裁剪
- python全网表情包_Python爬虫爬取最右公众号表情包资源
- (实用工具分享)网页元素截图工具
- python行业数据分析_python在数据分析
- OpenCV之感兴趣区域ROI
- 外卖点餐APP效果图
- html 字体兼容,设置兼容浏览器的中文字体
- 【python】pickle
- android开发之发送短信SMS
- C语言二级必须编译吗,c语言是计算机二级吗
- FPGA的PS还有什么PL是什么意思
- [uboot] (第一章)uboot流程——概述
- Minio入门系列【13】使用WebUploader上传文件到Minio服务器
- 【技巧】去哪找高清免费素材?这些资源网站必须收藏!
- matlab智能算法之萤火虫算法
- 基于javaweb+jsp的家庭理财系统(java+SSM+JSP+Tomcat8+Mysql)
- MySQL初级篇(二)