zoukankan      html  css  js  c++  java
  • 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=2;
        }
        /**
         * 上一张按钮事件
         * @param view
         */
        public void preBt(View view){
            GameSurfaceView.cn=1;
        }

    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=1;
        
        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==1){
                canvas.drawBitmap(bmp_bg1, 0,0, paint);
            }else if(cn==2){
                canvas.drawBitmap(bmp_bg2, 0,0, 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(50>(endTime-startTime)){
                        Thread.sleep(50-(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) {}
    }
  • 相关阅读:
    一个误解: 单个服务器程序可承受最大连接数“理论”上是“65535”
    Memcached 命令简介
    MySQL性能测试
    WCF 面向服务的SOAP消息
    WCF SOAP消息剖析
    深入探析 socket
    C#设计模式(适配器模式)
    LoadRunner中的异常处理
    反射调用性能比较(附源码)
    避免 TCP/IP 端口耗尽
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4280819.html
Copyright © 2011-2022 走看看