zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    oracle重新学习,主要是命令实用,有借鉴其他人博客
    cdn缓存立刻刷新
    python3发送需要双向认证的wss请求
    【2021.08.03】平等交流
    【2021.07.31】正面回应才是面对挑战的最好方式
    【2021.07.28】向晚
    【2021.07.25】过个生日
    【2021.07.17】Asoul——那些无法杀死我的,终会使我变得更加强大
    phaser3微信小游戏2
    eve-ng 配置apache2的虚拟目录
  • 原文地址:https://www.cnblogs.com/yechanglv/p/6922962.html
Copyright © 2011-2022 走看看