android 之 View

时间:2024-04-08 08:56:38

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

自定义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();
    }
}

android 之 Viewandroid 之 View

转载于:https://www.cnblogs.com/sunnychuh/archive/2011/04/17/2019138.html