关于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

packagecom.rong.activity;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.util.AttributeSet;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView;/***自定义SurfaceView**@author徐荣**/publicclassXRSurfaceViewextendsSurfaceViewimplementsSurfaceHolder.Callback,Runnable{//SurfaceView的宽intsurfaceWidth;//SurfaceView的高intsurfaceHeight;//SurfaceHolder对象SurfaceHoldermSurfaceHolder;//开关线程的标志位booleanisRunning=true;//画笔PaintmPaint;//圆的半径floatradius=0;//圆是变大还是缩小的状态booleanstatus=true;//圆变化的速度intmSpeed=3;publicXRSurfaceView(Contextcontext,AttributeSetattrs){super(context,attrs);initView();}privatevoidinitView(){//获取mSurfaceHoldermSurfaceHolder=getHolder();//添加回调mSurfaceHolder.addCallback(this);}@OverridepublicvoidsurfaceCreated(SurfaceHolderholder){isRunning=true;//初始化画笔mPaint=newPaint();mPaint.setAntiAlias(true);mPaint.setColor(Color.BLUE);//开启绘制线程newThread(this).start();}@OverridepublicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,intheight){//获取surface的宽surfaceWidth=width;//获取surface的高surfaceHeight=height;}@OverridepublicvoidsurfaceDestroyed(SurfaceHolderholder){//关闭绘制线程isRunning=false;}@Overridepublicvoidrun(){CanvasmCanvas=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(Exceptione){e.printStackTrace();}finally{//解除画布锁mSurfaceHolder.unlockCanvasAndPost(mCanvas);}}}}

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

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns: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.XRSurfaceViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerInParent="true"android:orientation="vertical"/></RelativeLayout>

Run

更多相关文章

  1. Android序列化学习
  2. Android(安卓)AOP之AspectJ入门
  3. Android(安卓)DataBinding使用详解(一)
  4. 序列化原理(二):从源码理解Parcelable
  5. android 【点击输入框调出输入法前的】输入框获取焦点和输入法的
  6. Android实现局部重绘
  7. 使用 SQLiteDatabase 操作 SQLite 数据库
  8. Android(安卓)studio 中与本地 html 页面交互
  9. Android(安卓)开发者必知必会的权限管理知识

随机推荐

  1. 使用IntelliJ IDEA(androidstudio前身)开发
  2. android apk系统签名
  3. Android 获取手机IMEI 和 IMSI 号
  4. You need to use a Theme.AppCompat them
  5. 【Android(安卓)Debug】 Skipping insecu
  6. Android开发之广播机制
  7. android个人视频学习笔记(二)
  8. Android(安卓)Lib层打印log之------bioni
  9. 移动应用开发辅助服务推荐
  10. 在代码中实现按下Home键的效果