Android audio and video three ways to draw pictures

To draw a picture on the Android platform, use at least 3 different APIs, ImageView, SurfaceView, and custom View.

1. ImageView draws pictures

Anyone who has done Android development must know how to draw. It's simple:

Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + File.separator + "11.jpg");
imageView.setImageBitmap(bitmap);

It's easy to see the picture we drew on the interface.

2. SurfaceView draws pictures

This is a little more complicated than ImageView drawing pictures:

SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface);
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

        if (surfaceHolder == 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");  // 获取bitmap
        Canvas canvas = surfaceHolder.lockCanvas();  // 先锁定当前surfaceView的画布
        canvas.drawBitmap(bitmap, 0, 0, paint); //执行绘制操作
        surfaceHolder.unlockCanvasAndPost(canvas); // 解除锁定并显示在界面上
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }
});

3. Customize View to draw pictures

This can be done easily if you have experience in drawing custom Views. I have also briefly sorted out the knowledge of Android custom View drawing :

public class CustomView extends View {  
  
    Paint paint = new Paint();  
    Bitmap bitmap;  
  
    public CustomView(Context context) {  
        super(context);  
        paint.setAntiAlias(true);  
        paint.setStyle(Paint.Style.STROKE);  
        bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + File.separator + "11.jpg");  // 获取bitmap  
    }  
  
    @Override  
    protected void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
  
        // 不建议在onDraw做任何分配内存的操作  
        if (bitmap != null) {  
            canvas.drawBitmap(bitmap, 0, 0, paint);  
        }  
    }  
}

Note: Don't forget the permission, otherwise it will not show success.

<uses-permission android:name="android.permission.READ\_EXTERNAL\_STORAGE"/>

Scan the QR code below to get more Android audio and video learning materials for free!

Enterprise-level Android audio and video development learning route + project practice (with source code)

With the advent of the 5G era, short videos have gradually taken over people's daily lives. Now a large number of companies have begun to pay attention to audio and video talents, and the salary of audio and video engineers is about 30% higher than that of ordinary developers.

At present, audio and video practitioners basically have two directions: one is audio and video algorithms, which require deep mathematical ability and algorithm background. This kind of talent is almost monopolized by large companies, with an average annual salary of more than one million; There are relatively many such people in audio and video business development. They have a solid theoretical foundation, deep technical skills, and are very familiar with audio and video.

Due to the large content of the article and the limited space, the information has been organized into PDF documents. If you need the complete document of "Audio and Video Development Complete Route Information", you can add WeChat to get it for free! (Promise: 100% free)

24 lessons with five actual projects , namely: Android audio and video codec project actual combat, pan-entertainment live broadcast system analysis and streaming actual combat, Douyu live broadcast project actual combat, OpenGL ES handwritten beauty camera APP project actual combat, cross-compilation and CameraX Project combat.

img

Guess you like

Origin blog.csdn.net/Android_XG/article/details/129666223