preface

In Android audio and video development, online knowledge is too fragmented, self-learning is very difficult, but audio and video cattle Jhuster put forward “Android audio and video from the entry to improve – task list”. This article is the first Android audio and video task list, corresponding to the content to learn is: draw a picture on the Android platform, using at least three different API, ImageView, SurfaceView, custom View.


Audio and video task list

Audio and video task list: Click here to jump to view.


directory


(1) ImageView draws pictures

(1) Put the pictures into MIpMAP-HDPI and Assets respectively

(2) Layout file:

<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_picture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ImageView>

</LinearLayout>
Copy the code

(3) Code:

// ImageView is loaded from several sources:
// (1)drawable/mipmap (r.drawabe. XXX
// (2) Assests path resources

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView customImageView = findViewById(R.id.iv_picture);
        Drawable /mipmap: use r.draabe. XXX to load image resources
        // customImageView.setBackgroundResource(R.mipmap.wuqian);

        // Method two: Load the assests path resources
        customImageView.setImageBitmap(getImageFromAssetsFile(this."lilangdi.jpg"));
    }

    public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
        Bitmap image = null;
        AssetManager am = context.getResources().getAssets();
        try {
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
        returnimage; }}Copy the code

(4) Results


(2) SurfaceView drawing pictures

(1) Put the picture in the mobile phone folder

(2) Layout:

<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SurfaceView
        android:id="@+id/sv_drawpicture"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </SurfaceView>

</LinearLayout>
Copy the code

(3) Add permissions

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

(4) Code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        verifyStoragePermissions(this);
        setContentView(R.layout.activity_main);

        SurfaceView mSurfaceView = findViewById(R.id.sv_drawpicture);

        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);

                / / a: Environment. External.getexternalstoragedirectory () getPath () access path is: / storage/emulated / 0
                Separator gets: /
                Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + File.separator + "wuqian.jpg");  / / get the bitmap

                // Method 2:
                // Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/wuqian.jpg"); / / get the bitmap

                Canvas canvas = holder.lockCanvas();  // Lock the canvas of the current surfaceView
                canvas.drawBitmap(bitmap, 0.0, paint); // Perform the draw operation
                holder.unlockCanvasAndPost(canvas); // Unlock it and display it on the interface
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}@Override
            public void surfaceDestroyed(SurfaceHolder holder) {}}); }private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE"."android.permission.WRITE_EXTERNAL_STORAGE" };

    public static void verifyStoragePermissions(Activity activity) {
        try {
            // Check whether you have write permission
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if(permission ! = PackageManager.PERMISSION_GRANTED) {// There is no write permission. If you apply for the write permission, a dialog box will be displayedActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE); }}catch(Exception e) { e.printStackTrace(); }}}Copy the code

(5) Results


(3) Custom View drawing pictures

(1) Put the picture into the mobile phone or miPMAP-HDPI

(2) Layout:

<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.lzacking.viewdemo.CustomView
        android:id="@+id/customview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.lzacking.viewdemo.CustomView>

</LinearLayout>
Copy the code

(3) Add permissions

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

(4) Code:

CustomView:

public class CustomView extends View {

    Paint paint = new Paint();
    Bitmap bitmap;

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        // Method 1: Read the image from the phone
        bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + File.separator + "kugou.png");  / / get the bitmap

        // Method 2: Read the image from mipmap-hdpi
        // bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.taylor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // It is not recommended to do any memory allocation in onDraw
        if(bitmap ! =null) {
            canvas.drawBitmap(bitmap, 0.0, paint); }}}Copy the code

MainActivity:

// Method 1: Read the image from the phone
// Method 2: Read the image from mipmap-hdpi
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If you want to read pictures from your mobile phone, you need to apply for permission
        verifyStoragePermissions(this);
        setContentView(R.layout.activity_main);
    }

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE"."android.permission.WRITE_EXTERNAL_STORAGE" };

    public static void verifyStoragePermissions(Activity activity) {
        try {
            // Check whether you have write permission
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if(permission ! = PackageManager.PERMISSION_GRANTED) {// There is no write permission. If you apply for the write permission, a dialog box will be displayedActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE); }}catch(Exception e) { e.printStackTrace(); }}}Copy the code

(5) Results


Source code: Android audio and video development foundation (a) : through three ways (ImageView, SurfaceView, custom View) draw pictures