A new pit

Start to learn audio and video related knowledge from today

Learning material is relatively simple is this Android audio and video development learning ideas, thank big guy W

Here’s the point


Android audio and Video development (I) : Draw pictures in three ways

Three ways to draw:

  • ImageView
  • SurfaceView
  • The custom View

To apply for permission

Because of the Android permission mechanism, the application whose targetSdkVersion is greater than or equal to 23(6.0) needs to pay attention to the dynamic access to the permission. What I do here is relatively simple. In the actual use of the onCreate method application do not directly use magic values like I did here do requestCode…

AndroidManifest.xml

<! Read data from SDCard -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Copy the code

MainActivity:onCreate()

  @Override
  protected void onCreate(Bundle savedInstanceState) {...if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) ! = PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this.new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
    } else{ showPicture(); }}Copy the code

MainActivity:onRequestPermissionResult()

  @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
      @NonNull int[] grantResults) {
    switch (requestCode) {
      case 1: {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          showPicture();
        }
        return;
      }
      default:}}Copy the code

ImageView

MainActivity:showPicture()

  private void showPicture(a) {
    Bitmap bitmap = BitmapFactory.decodeFile(
        Environment.getExternalStorageDirectory().getPath() + File.separator + "11.jpg");
    mImageView.setImageBitmap(bitmap);
  }
Copy the code

DecodeFile method to get a picture in the root directory of the built-in memory card

SurfaceView

MainActivity:showPicture()

  private void showPicture(a) {
    mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
      @Override public void surfaceCreated(SurfaceHolder holder) {
        if (holder == null) {
          return;
        }

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);

        Bitmap bitmap = BitmapFactory.decodeFile(
            Environment.getExternalStorageDirectory().getPath() + File.separator + "11.jpg");
        Canvas canvas = holder.lockCanvas();
        canvas.drawBitmap(bitmap, 0.0, paint);
        holder.unlockCanvasAndPost(canvas);
      }

      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}@Override public void surfaceDestroyed(SurfaceHolder holder) {}}); }Copy the code

Surface is also a frequently used drawing method, which has many advantages. Independent thread drawing does not affect the main thread, and double buffering mechanism is both. This is done by drawing the prepared Bitmap in the initialized callback. Note that lockCanvas is used before drawing and unlockAndPost is used after drawing

The custom View

MyView

public class MyView extends View {

  Paint mPaint;
  Bitmap mBitmap;

  public MyView(Context context) {
    super(context);
    init();
  }

  public MyView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  private void init(a) {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.STROKE);
    mBitmap = BitmapFactory.decodeFile(
        Environment.getExternalStorageDirectory().getPath() + File.separator + "11.jpg");
  }

  @Override protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(mBitmap ! =null) {
      canvas.drawBitmap(mBitmap, 0.0, mPaint); }}}Copy the code

Custom views are also used a lot, and I recommend the HenCoder series to learn more about custom views

conclusion

The three ways to draw, very basic, each has its advantages, ImageView is easy to learn, SurfaceView does not block the main thread.