首页 > 其他 > 详细

使用Surface View来显示图片

时间:2019-06-05 23:51:48      阅读:962      评论:0      收藏:0      [点我收藏+]

public class YUVImageView extends SurfaceView {
private static final String TAG = "YUVImageView";
private SurfaceHolder surfaceHolder;
private Paint paint;
private Rect srcRect, destRect;
private Canvas canvas;
private Bitmap bitmap;
private BitmapFactory.Options options;

private SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
};

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

public YUVImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public YUVImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView();
}

private void initView() {
    surfaceHolder = getHolder();
    surfaceHolder.setFixedSize(640, 480);
    surfaceHolder.addCallback(callback);
    setZOrderOnTop(true);
    setZOrderMediaOverlay(true);
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    srcRect = new Rect(0, 0, 640, 480);
    destRect = new Rect(0, 0, 640, 480);
    options = new BitmapFactory.Options();
}

public void drawBitmap(int id){
    if (surfaceHolder != null) {
        canvas = surfaceHolder.lockCanvas();
    }
    if (surfaceHolder != null && canvas != null) {
        try {
            canvas.drawColor(Color.WHITE);
            bitmap = BitmapFactory.decodeResource(getResources(),id);
            if (bitmap != null) {
                canvas.drawBitmap(bitmap, srcRect, destRect, paint);
            } else {
                LogUtils.d(TAG, "create bitmap from yuvData array failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(TAG, "drawYUV error ", e);
        } finally {
            if (canvas != null && surfaceHolder != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
            if (bitmap != null) {
                bitmap.recycle();
            }
        }
    }
}

public void drawYUV(byte[] yuvData) {
    if (surfaceHolder != null) {
        canvas = surfaceHolder.lockCanvas();
    }
    if (surfaceHolder != null && canvas != null) {
        try {
            canvas.drawColor(Color.WHITE);
            bitmap = BitmapFactory.decodeByteArray(yuvData, 0, yuvData.length, options);
            if (bitmap != null) {
                canvas.drawBitmap(bitmap, srcRect, destRect, paint);
            } else {
                LogUtils.d(TAG, "create bitmap from yuvData array failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(TAG, "drawYUV error ", e);
        } finally {
            if (canvas != null && surfaceHolder != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
            if (bitmap != null) {
                bitmap.recycle();
            }
        }
    }
}

}

使用Surface View来显示图片

原文:https://www.cnblogs.com/android-lol/p/10982611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!