Android Button悬浮在SurfaceView上
实现Button悬浮于与SurfaceView之上实现
注意:你实现的SurfaceView和android中的Button,EditView是同级的,不能把一个包含在另一个里面
1.创建自己的SurfaceView类,一定要实现2个参数的那个函数,因为你要在XMl中使用,第二个参数指的自定义的组件的一些属性长宽等。
public GameSurfaceView(Context context, AttributeSet attrs){
super(context,attrs);
}
<dk.game.GameSurfaceView android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
2.处理SurfaceView中事件一定要在Activity中。我们在xml中定义我们的surfaceview 和 组件button、textview等等的时候 他们是同一级别的!!而不是把button包含在surfaceview里,所以虽然在surfaceview中可以根据id索引到button但绑定的 时候是无法找到button的,只有我们的 activitysetContentView(R.layout.main); 显示的button,所以只能在显示它的activity中去绑定,这里需要注意
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
/**
* 下一张按钮事件
* @param view
*/
public void nextBt(View view){
GameSurfaceView.cn=;
}
/**
* 上一张按钮事件
* @param view
*/
public void preBt(View view){
GameSurfaceView.cn=;
}
3.在Layout中是相对布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <dk.game.GameSurfaceView android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/> <Button
android:id="@+id/nextBt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="下一张"
android:onClick="nextBt" /> <Button
android:id="@+id/preBt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/nextBt"
android:layout_alignParentBottom="true"
android:text="上一张"
android:onClick="preBt" /> </RelativeLayout>
GameSurfaceView类的源码如下
package dk.game; import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView; public class GameSurfaceView extends SurfaceView implements Runnable ,SurfaceHolder.Callback{ private Thread gameViewThread;
private SurfaceHolder surfaceHolder;
private Paint paint;
private boolean runFlag=false;
public static int screen_width,screen_height;
protected Resources resources;
private Canvas canvas;
private Bitmap bmp_bg1,bmp_bg2;
public static int cn=; public GameSurfaceView(Context context, AttributeSet attrs){
super(context,attrs);
init();
} public GameSurfaceView(Context context) {
super(context);
init();
} private void init(){
surfaceHolder=getHolder();
surfaceHolder.addCallback(this);
resources=getResources(); paint=new Paint();
paint.setAntiAlias(true); bmp_bg1=BitmapFactory.decodeResource(getResources(), R.drawable.bg1);
bmp_bg2=BitmapFactory.decodeResource(getResources(), R.drawable.bg2);
// setFocusable(true);
// setFocusableInTouchMode(true);
// setClickable(true);
setKeepScreenOn(true);
} public void draw(Canvas canvas,Paint paint){
canvas.drawColor(Color.BLACK);
if(cn==){
canvas.drawBitmap(bmp_bg1, ,, paint);
}else if(cn==){
canvas.drawBitmap(bmp_bg2, ,, paint);
}
} @Override
public void run() {
while(runFlag){
try{
canvas=surfaceHolder.lockCanvas();
long startTime=System.currentTimeMillis();
canvas.drawColor(Color.BLACK);
draw(canvas,paint);
long endTime=System.currentTimeMillis();
if(>(endTime-startTime)){
Thread.sleep(-(endTime-startTime));
}
}catch (Exception e) {
Log.e("Error", "刷新屏幕出错!"+e);
}finally{
if(canvas!=null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
} @Override
public void surfaceCreated(SurfaceHolder holder) {
screen_width=getWidth();
screen_height=getHeight();
runFlag=true;
gameViewThread=new Thread(this);
gameViewThread.start();
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
runFlag=false;
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}
}
Android Button悬浮在SurfaceView上的更多相关文章
- Android游戏开发学习(5)--实现Button悬浮于与SurfaceView之上
原文:http://daikainan.iteye.com/blog/1407355 实现Button悬浮于与SurfaceView之上实现 先看效果: 注意:你实现的SurfaceView和andr ...
- Android捕捉图像后在SurfaceView上变形显示问题的处理
我们在Android中经常会使用SurfaceView编写自定义的摄像头,可是有的时候会经常会出现图像的变形,我们就会很郁闷的问这到底是为什么呢?其实这个最根本的原因是SurfaceView和PreV ...
- 我的Android进阶之旅------>android Button上面的英文字符串自动大写的问题解决
今天碰到一个关于Button的问题:android Button上面的英文字符串会自动变成大写,运行的Android 5.1版本,如下图所示: 图1:Button 图2:TextView 这个Butt ...
- 我的Android进阶之旅------>android Button上面的英文字符串自己主动大写的问题解决
今天碰到一个关于Button的问题:android Button上面的英文字符串会自己主动变成大写,执行的Android 5.1版本号,例如以下图所看到的: 图1:Button 图2:TextView ...
- Android 学习笔记之SurfaceView的使用+如何实现视频播放...
学习内容: 1.掌握Surface的使用... 2.Android中如何实现视频播放... 1.SurfaceView类的使用 在Android中,一般播放音频时我们可以去使用Android提供的 ...
- android桌面悬浮窗实现
首先是一个小的悬浮窗显示的是当前使用了百分之多少的内存,点击一下小悬浮窗,就会弹出一个大的悬浮窗,可以一键加速.好,我们现在就来模拟实现一下类似的效果. ...
- Android -- 桌面悬浮,QQ管家火箭实现
续上一篇博客<Android -- 桌面悬浮,仿360>,传送门:http://www.cnblogs.com/yydcdut/p/3909888.html,在此代码上继续添加实现. 比起 ...
- Android WindowManager悬浮窗:不需要申请权限实现悬浮
Android WindowManager悬浮窗:不需要申请权限实现悬浮 附录文章1介绍了Android平台上的悬浮窗WindowManager,WindowManager悬浮窗可以悬浮在And ...
- Android应用截图和SurfaceView截图问题总结
最近在做android截图应用的过程遇到很多问题,接触了好些截图方法,但是还是不能实现SufaceView截图功能.今天就把我尝试过的方法总结下,希望把我惨痛的经历写出来后能够帮助到要做此 ...
随机推荐
- kettle Argument, Parameter, Variable
1. Argument, Parameter, Variable 的区别 a.Argument作为位置参数不能复用,而其他2个可以根据名称重复使用 b. Argument, Parameter作用域局 ...
- smarty半小时快速上手入门教程
http://www.jb51.net/article/56754.htm http://www.yiibai.com/smarty/smarty_functions.html http://www. ...
- OC1_数组创建
// // main.m // OC1_数组创建 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxuemi ...
- 07_XPath_01_入门
[工程截图] [person.xml] <?xml version="1.0" encoding="UTF-8"?> <students> ...
- 关于“undefined reference to”错误
哪些问题可能产生undefined reference to错误? 1.没用生成目标文件 比如说hello.o文件,由于名字写错.路径不对.Makefile缺少. 2.没用添加相应的库文件.so/dl ...
- 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3789052.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- 九度OJ 1533 最长上升子序列 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1533 题目描述: 给定一个整型数组, 求这个数组的最长严格递增子序列的长度. 譬如序列1 2 2 4 3 的最长严 ...
- WebClient以POST方式发送Web请求
本例使用WebClient以POST方式发送Web请求并下载一个文件,难点是postData的构造,发送Web请求时有的网站要求可能要求 Cookies前后一致.其中application/x-www ...
- iOS局部刷新
iOS: TableView如何刷新指定的cell 或section //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithInd ...
- div重叠不变形
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...