preface

Recently, audio and video are very popular, so it is necessary to study, here is their own study notes ~~ take your time

The essence of a video is multiple images, so let’s start with the drawing

1.ImageView draws pictures

This must have done Android development all know how to draw. Is simple:

  val path = applicationContext.cacheDir.absolutePath + File.separator + "image.jpg"
  val bitmap = BitmapFactory.decodeFile(path)
  binding.imageView.setImageBitmap(bitmap)
Copy the code

2. SurfaceView draws pictures

This is a little more complicated than ImageView drawing:

binding.surface.holder.addCallback(object : SurfaceHolder.Callback { override fun surfaceCreated(surfaceHolder: SurfaceHolder) {val paint = paint () paint. IsAntiAlias = true // anti-aliasing paint. Style = paint applicationContext.cacheDir.absolutePath + File.separator + "image.jpg" val bitmap = BitmapFactory.decodeFile(path) val Canvas = SurfaceHolder.lockCanvas () canvas. DrawBitmap (bitmap, 0f, 0f, paint); / / perform drawing operations surfaceHolder. UnlockCanvasAndPost (canvas); } void void (surfaceHolder: surfaceHolder, p1: Int, p2: Int, p3: Int)} void void (surfaceHolder: surfaceHolder, P2: Int, p3: Int) Int) { } override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) { } })Copy the code

3. Customize View to draw pictures

This can be done easily if you have experience drawing custom views

class CustomView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val bitmap: Bitmap private val paint = paint () init {paint. IsAntiAlias = true // Anti-aliasing paint. Style = paint  = context.cacheDir.absolutePath + File.separator + "image.jpg" bitmap = BitmapFactory.decodeFile(path) } override fun DrawBitmap (bitmap, 0f, 0f, paint); drawBitmap(bitmap, 0f, 0f, paint); }}Copy the code

Note: If you are using a directory other than your own file, don’t forget the permissions,Otherwise it won’t show success.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

The next article is all about the various apis