SurfaceView的基本使用流程

好久没有写博客了,今天抽个空写篇简单的文章,主要就是来介绍一下SurfaceView的基本使用方法,并附上一个小DEMO供大家下载。废话不多说,直接上代码。
首先自定义了一个SurfaceView:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.yulin.animdemo.R;

/**
 * Created by YuLinixon 2016/11/7 0007.
 */
public class TestSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    RenderThread renderThread;
    SurfaceHolder holder;
    Context context;

    int lastAngle = 0;
    int swipeAngle = 0;

    RectF rectF;
    Paint paint;

    boolean isDraw = true;

    private static final int sleepSpan = 50;
    private static final int plusAngle = 10;

    public TestSurfaceView(Context context) {
        super(context);
        this.context = context;
        holder = this.getHolder();
        holder.addCallback(this);

        renderThread = new RenderThread();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isDraw = true;
        paint = new Paint();
        paint.setDither(true);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(context.getResources().getColor(R.color.colorAccent));
        rectF = new RectF(getWidth() / 4, getWidth() / 4, getWidth() / 4 * 3, getWidth() / 4 * 3);
        renderThread.start();
    }

    @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDraw = false;
    }

    private class RenderThread extends Thread {
        @Override
        public void run() {
            while (true) {
                doDraw();
                try {
                    Thread.sleep(sleepSpan);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                lastAngle = swipeAngle;
                swipeAngle += plusAngle;
            }
        }
    }

    private void doDraw() {
        Canvas canvas = holder.lockCanvas();
        try {
//            canvas.drawArc(rectF, lastAngle, swipeAngle, false, paint);
            canvas.drawArc(rectF, lastAngle, swipeAngle, true, paint);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            holder.unlockCanvasAndPost(canvas);
        }
    }
}

然后是在Activity中的使用:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TestSurfaceView surfaceView = new TestSurfaceView(this);
        setContentView(surfaceView);
    }
 }

最后是显示效果:
效果图
最后了解一下SurfaceView的基本使流程:
1.获取到 SurfaceView 对应的 SurfaceHolder,并且要给 SurfaceHolder 添加一个 SurfaceHolder.callback 对象。
2.创建一个线程用于渲染视图;
3.在SurfaceHolder.callback的surfaceCreated方法中开始渲染视图,绘制结束后,SurfaceHolder.callback的surfaceDestroyed方法中使用 unlockCanvasAndPost方法解锁 Canvas。
感觉是不是很简单?最后附上这个Demo的源码下载地址:

DEMO下载地址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值