SurfaceView(3),加入帧率控制


SurfaceView相关目录


加入帧率控制

核心代码

                        while (isRunning) {
                            long startMs = System.currentTimeMillis();
                            draw();
                            long endMs = System.currentTimeMillis();
                            long needTime = 1000 / fps;
                            long usedTime = endMs - startMs;
                            if (usedTime < needTime) {
                                try {
                                    Thread.sleep(needTime - usedTime);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

完整代码

package com.example.zhangyu.cutvideomusic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.zhangyu.cutvideomusic.utils.ThumbUtils;

public class BgSurfaceView extends SurfaceView {

    private SurfaceHolder holder;
    private Canvas canvas;
    private Thread thread;//用于绘制的线程
    private boolean isRunning;//线程的控制开关
    //设置绘制的fps
    private int fps = 30;

    public BgSurfaceView(Context context) {
        this(context, null);
    }

    public BgSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                isRunning = true;
                thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //不断绘制
                        while (isRunning) {
                            long startMs = System.currentTimeMillis();
                            draw();
                            long endMs = System.currentTimeMillis();
                            long needTime = 1000 / fps;
                            long usedTime = endMs - startMs;
                            if (usedTime < needTime) {
                                try {
                                    Thread.sleep(needTime - usedTime);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                });
                thread.start();
            }

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

            }

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

    Bitmap bitmapCache;

    private void draw() {
        //SurfaceView被销毁,但是子线程可能还在进行操作,可能抛出一些异常
        try {
            canvas = holder.lockCanvas();
            //按home或者back,SurfaceView被销毁,所以需要判空canvas
            if (canvas != null) {
                if (bitmapCache == null) {
                    bitmapCache = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
                }
                Canvas canvasCache = new Canvas(bitmapCache);
                drawCache(canvasCache);
                canvas.drawBitmap(bitmapCache, 0, 0, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                                try {
                                    //手动try catch一下这个方法,让程序在4.3的手机上不至于崩溃,部分Android13也会崩溃
                                    holder.unlockCanvasAndPost(canvas);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
            }
        }
    }

    private void drawCache(Canvas canvas) {
        //todo 开始绘制
        
     }

}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
SurfaceView是Android中的一个视图控件,它可以在一个单独的线程中进行绘制操作,可以用来实现一些需要高性能绘制的场景,比如游戏开发、视频播放等。相比于普通的View控件,SurfaceView的优势在于它可以避免UI线程被阻塞,提高了UI的流畅度和响应性。同时,SurfaceView也提供了一些更底层的API,可以对绘制过程进行更加精细的控制。 在使用SurfaceView时,通常需要创建一个继承自SurfaceView的子类,并在其中实现SurfaceHolder.Callback接口。SurfaceHolder.Callback接口包含三个方法:surfaceCreated、surfaceChanged和surfaceDestroyed,分别用于处理SurfaceView的创建、改变和销毁事件。 下面是一个简单的SurfaceView代码示例: ```java public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mSurfaceHolder; private Paint mPaint; public MySurfaceView(Context context) { super(context); init(); } public MySurfaceView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setStyle(Paint.Style.FILL); } @Override public void surfaceCreated(SurfaceHolder holder) { // 在这里进行绘制操作 Canvas canvas = holder.lockCanvas(); canvas.drawColor(Color.WHITE); canvas.drawRect(100, 100, 200, 200, mPaint); holder.unlockCanvasAndPost(canvas); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // SurfaceView的大小发生改变时调用 } @Override public void surfaceDestroyed(SurfaceHolder holder) { // SurfaceView销毁时调用 } } ``` 在上面的代码中,我们创建了一个名为MySurfaceViewSurfaceView子类,并在其中实现了SurfaceHolder.Callback接口。在init方法中,我们获取SurfaceHolder对象并注册Callback回调,同时创建一个Paint对象用于绘制。 在surfaceCreated方法中,我们使用SurfaceHolder.lockCanvas方法获取一个Canvas对象,并在其上进行绘制操作,最后使用SurfaceHolder.unlockCanvasAndPost方法提交绘制结果。这样就可以在SurfaceView上绘制出一个红色的矩形。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值