发布于 2017-09-28 09:06:29 | 112 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程之SurfaceView用法,简要分析了View和SurfaceView的区别,并结合实例形式分析了SurfaceView的具体使用步骤与相关技巧,需要的朋友可以参考下

本文实例讲述了Android编程之SurfaceView用法。分享给大家供大家参考,具体如下:

关于surfaceView相关知识:

View和SurfaceView主要区别:

1. View只能在UI线程中刷新,而SurfaceView可以在子线程中刷新

2. SurfaceView可以控制刷新频率

SurfaceView几个重要的方法:

1. 继承SurfaceView 后调用getHolder()方法可以获取到mSurfaceHolder对象这个对于可以控制SurfaceView的绘制

2. 实现这个SurfaceHolder.Callback接口并且mSurfaceHolder.addCallback(this)添加回调可以感知到SurfaceView的生命周期

3. 绘制的时候mCanvas.drawColor(Color.BLACK);这个方法很重要,这个方法是清理上一次绘制的东西,这个方法一定要调用才能看到效果

实现效果 如下:

第一步:新建XRSurfaceView继承SurfaceView


package com.rong.activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
 * 自定义SurfaceView
 * 
 * @author 徐荣
 *
 */
public class XRSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
  // SurfaceView的宽
  int surfaceWidth;
  // SurfaceView的高
  int surfaceHeight;
  // SurfaceHolder对象
  SurfaceHolder mSurfaceHolder;
  // 开关线程的标志位
  boolean isRunning = true;
  // 画笔
  Paint mPaint;
  // 圆的半径
  float radius = 0;
  // 圆是变大还是缩小的状态
  boolean status = true;
  // 圆变化的速度
  int mSpeed = 3;
  public XRSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView();
  }
  private void initView() {
    // 获取mSurfaceHolder
    mSurfaceHolder = getHolder();
    // 添加回调
    mSurfaceHolder.addCallback(this);
  }
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    isRunning = true;
    // 初始化画笔
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.BLUE);
    // 开启绘制线程
    new Thread(this).start();
  }
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // 获取surface的宽
    surfaceWidth = width;
    // 获取surface的高
    surfaceHeight = height;
  }
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    // 关闭绘制线程
    isRunning = false;
  }
  @Override
  public void run() {
    Canvas mCanvas = null;
    while (isRunning) {
      try {
        // 锁定canva进行绘制
        mCanvas = mSurfaceHolder.lockCanvas(null);
        // 这个方法很重要,相当于重绘(一定要调用不然看不到效果)
        mCanvas.drawColor(Color.BLACK);
        // 画圆
        mCanvas.drawCircle((surfaceWidth / 2), (surfaceHeight / 2), radius, mPaint);
        // 更改半径变量
        if (status) {
          radius = radius + mSpeed;
          if (radius > 200) {
            status = false;
          }
        } else {
          radius = radius - mSpeed;
          if (radius < 0) {
            status = true;
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        // 解除画布锁
        mSurfaceHolder.unlockCanvasAndPost(mCanvas);
      }
    }
  }
}

第二步:新建布局文件activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  android:orientation="vertical" >
  <com.rong.activity.XRSurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:orientation="vertical" />
</RelativeLayout>

运行!

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务