• android 之 View


    在进行游戏开发时,需要自定义各种控件和界面。

    自定义View的使用:

    • 绘制屏幕
    • 刷新屏幕:后台数据发生了变化,需要开发人员自己刷新屏幕以显示最新数据

    例子:

    MyView开发,绘制界面View内容:

    package com.sunny;

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.view.View;

    public class MyView extends View {
        static final int ANGLE_MAX = 50;
        static final int SPEED = 4;
        static final int SCREEN_WIDTH = 480;
        static final int SCREEN_HEIGHT = 320;

        static final int LEFT = 2;
        static final int RIGHT = 0;
        static final int UP = 3;
        static final int DOWN = 1;

        int angle = 30;
        int angleChange = 3;
        int radius = 16;
        int centerX = radius;
        int centerY = radius;
        long timeStamp = System.currentTimeMillis();

        int currPhoto=0;
        int direction = RIGHT;
        Bitmap bmpMan;
        Bitmap[] bmpPhotos;
        int[] imgIds = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d };

        public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            bmpMan = BitmapFactory.decodeResource(getResources(), R.drawable.man);
            //System.out.println(bmpMan.getWidth());
            bmpPhotos = new Bitmap[imgIds.length];
            for (int i = 0; i < bmpPhotos.length; i++) {
                bmpPhotos[i] = BitmapFactory.decodeResource(getResources(),
                        imgIds[i]);
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub

            Paint paint = new Paint();
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(bmpMan, centerX - radius, centerY - radius, null);
            paint.setColor(Color.BLACK);
            paint.setAntiAlias(true);
            RectF oval = new RectF(centerX - radius - 1, centerY - radius - 2,
                    centerX - radius - 1 + 2 * radius + 2, centerY - radius - 2 + 2
                            * radius + 4);
            canvas.drawArc(oval, 360-angle+90*direction, 2*angle, true, paint);
            if (System.currentTimeMillis() - timeStamp > 5000) {
                timeStamp = System.currentTimeMillis();
                currPhoto = (currPhoto + 1) % bmpPhotos.length;
            }
            canvas.drawBitmap(bmpPhotos[currPhoto], 80, 40, null);
            super.onDraw(canvas);
        }

    }

    MyThread线程主要控制View中元素的变化,及时刷新View信息:

    package com.sunny;

    public class MyThread extends Thread {
        int sleepSpan = 30;
        MyView myView;

        public MyThread(MyView myView) {
            super();
            this.myView = myView;
        }

        public void run() {
            while (true) {
                myView.angle += myView.angleChange;
                if (myView.angle > MyView.ANGLE_MAX) {
                    myView.angleChange = -3;
                } else if (myView.angle < 0) {
                    myView.angleChange = 3;
                }
                switch (myView.direction) {
                case MyView.RIGHT:
                    myView.centerX = myView.centerX + myView.SPEED;
                    break;
                case MyView.UP:
                    myView.centerY = myView.centerY - myView.SPEED;
                    break;
                case MyView.LEFT:
                    myView.centerX = myView.centerX - myView.SPEED;
                    break;
                case MyView.DOWN:
                    myView.centerY = myView.centerY + myView.SPEED;
                    break;
                }
                if (myView.centerY + myView.radius > MyView.SCREEN_HEIGHT) {
                    myView.centerY = myView.centerY - MyView.SPEED;
                    myView.direction = MyView.LEFT;
                }
                if (myView.centerY - myView.radius < 0) {
                    myView.centerY = myView.centerY + MyView.SPEED;
                    myView.direction = MyView.RIGHT;
                }
                if (myView.centerX + myView.radius > MyView.SCREEN_WIDTH) {
                    myView.centerX = myView.centerX - MyView.SPEED;
                    myView.direction = MyView.DOWN;
                }
                if (myView.centerX - myView.radius < 0) {
                    myView.centerX = myView.radius;
                    myView.direction = MyView.UP;
                }
                myView.postInvalidate();
                try {
                    Thread.sleep(sleepSpan);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    在Activity中,创建View和后台线程:

    public class mainActivity extends Activity {
        /** Called when the activity is first created. */
        MyView myView;
        MyThread mt;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            myView = new MyView(this);
            setContentView(myView);
            mt = new MyThread(myView);
            mt.start();
        }
    }

    imageimage

  • 相关阅读:
    C#多线程编程实战(二)
    C#为什么要多线程开发(一)
    海康威视实时预览回调PS流用EasyRTMP向RTMP服务器推流中视频数据处理的代码
    CentOS "libc.so.6: version 'GLIBC_2.14' not found"解决方法,同理'GLIBC_2.15' not found"
    EasyRTMP结合海康HCNetSDK获取海康摄像机H.264实时流并转化成为RTMP直播推流(附源码)
    基于EasyDSS流媒体解决方案创建视频点播、短视频、视频资源库等视频播放系统
    EasyNVR是怎么做到Web浏览器播放RTSP摄像机直播视频延时控制在一秒内的
    EasyNVR depends on ffmpeg,yasm/nasm not found or too old. Use --disable-yasm for a crippledbuild
    EasyDSS流媒体服务器软件支持HTTPS-启用https服务申请免费证书
    EasyDSS流媒体服务器软件(支持RTMP/HLS/HTTP-FLV/视频点播/视频直播)-正式环境安装部署攻略
  • 原文地址:https://www.cnblogs.com/yechanglv/p/6922962.html
Copyright © 2020-2023  润新知