zoukankan      html  css  js  c++  java
  • android 之 surfaceView和普通View的重绘使用

    !自定义控件式需要实现AttrbuteSet   可在xml文件中配置略过创建该对象

    普通的View只能在主线程中绘制界面,适用于简单的被动绘制

    SurfaceView则可以在新线程中绘制界面,不会阻塞主线程,适用于需要不停主动重绘的界面

    xml布局代码

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.lzh.study.TestSurface
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <com.lzh.study.TestView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    </RelativeLayout>

    1.普通View直接重写onDraw()即可

    package com.lzh.study;

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.view.View;

    public class TestView extends View {

    public TestView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO 自动生成的构造函数存根
    }

    @Override
    protected void onDraw(Canvas canvas) {
    // TODO 自动生成的方法存根
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setTextSize(48);
    paint.setColor(Color.RED);
    canvas.drawText("wo", 200, 200, paint);
    // canvas.drawARGB(255, 255, 255,255);
    canvas.drawCircle(200, 200, 150, paint);
    paint.setARGB(100, 24, 75, 200);
    canvas.drawCircle(200, 200, 160, paint);
    paint.setColor(Color.YELLOW);
    paint.setTextSize(24);

    canvas.drawText("水温22度", 150, 200, paint);
    canvas.drawLine(0, 0, 300, 300, paint);
    }
    }

    2.在继承SurfaceView的类中即使重写了onDraw()方法也是没有用的,因为SurfaceView虽然继承自View,但并没
    重写onDraw(),其子类可以重写onDraw()但并不能自动调用,并且调用时会抛出异常

    package com.lzh.study;

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.SurfaceHolder;
    import android.view.SurfaceHolder.Callback;
    import android.view.SurfaceView;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.Log;

    public class TestSurface extends SurfaceView implements Callback {
    GameThread gameThread;

    public TestSurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO 自动生成的构造函数存根
    SurfaceHolder surfaceHolder = getHolder();
    // 添加回调对象
    surfaceHolder.addCallback(this);
    gameThread = new GameThread(surfaceHolder);;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
    // TODO 自动生成的方法存根

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    // TODO 自动生成的方法存根
    gameThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO 自动生成的方法存根

    }

    class GameThread extends Thread {
    SurfaceHolder surfaceHolder;
    // run()函数中控制循环的参数。
    boolean run = true;

    public GameThread(SurfaceHolder surfaceHolder) {
    this.surfaceHolder = surfaceHolder;
    }

    @Override
    public void run() {
    // TODO Auto-generated method stub
    int i = 0;
    while (run) {
    Log.v("gameThread", "GameThread");
    Canvas c = null;
    try {
    synchronized (surfaceHolder) {
    // 我们在屏幕上显示一个计数器,每隔 1 秒钟刷新一次
    c = surfaceHolder.lockCanvas();
    c.drawARGB(255, 0, 255, 255);
    Paint paint = new Paint();
    paint.setTextSize(48);
    paint.setColor(Color.RED);
    c.drawText("" + i++, 200,500, paint);
    Thread.sleep(1000);
    }
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    if (c != null) {
    surfaceHolder.unlockCanvasAndPost(c);
    }
    }
    }
    }
    }
    }

    结果如下图

  • 相关阅读:
    领域驱动设计(Domain Driven Design)
    程序员的梦想:意图编程
    怎样才算是好的软件可维护性设计?
    微软的patternshare.org初步体验
    转:JDepend:管理代码依赖性
    MAB, 专用的amazon浏览器,有点意思!
    宾夕法尼亚大学沃顿商学院:沃顿知识在线
    我的笔记本的鼠标又乱跑了!寻求帮助!
    能否让博客园对Firefox支持得好一些!
    交互设计:《About Face 2.0》中译本精彩节选
  • 原文地址:https://www.cnblogs.com/lzh-Linux/p/4473926.html
Copyright © 2011-2022 走看看