Android音视频(一):绘制图片的三种方式

1、ImageView显示图片

Bitmap bitmap = BitmapFactory.decodeFile(imgPath +"/123.jpg");
iv.setImageBitmap(bitmap);

2、SurfaceView绘制图片

SurfaceView sv = findViewById(R.id.sv);
        sv.getHolder().addCallback(new SurfaceHolder.Callback()
        {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder)
            {
                if (holder == null)
                    return;
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setStyle(Paint.Style.STROKE);

                Bitmap bitmap = BitmapFactory.decodeFile(imgPath + "/123.jpg");
                Canvas canvas = holder.lockCanvas();
                canvas.drawBitmap(bitmap,0,0,paint);
                holder.unlockCanvasAndPost(canvas);
            }

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

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder)
            {

            }
        });

3、自定义View绘制图片

public class ImgView extends View
{
    private final Paint paint;
    private final Bitmap bitmap;

    public ImgView(Context context)
    {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);

        final String imgPath = context.getExternalFilesDir(null).toString()+"/Img";
        bitmap = BitmapFactory.decodeFile(imgPath + "/123.jpg");
    }

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

        if (bitmap != null)
            canvas.drawBitmap(bitmap,0,0,paint);
    }
}

你可能感兴趣的:(Android音视频(一):绘制图片的三种方式)