android 音视频开发第一课 三种形式显示图片

1.ImageView 加载图片

private ImageView mShowImage;
mShowImage = findViewById(R.id.iv_show_image);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
mShowImage.setImageBitmap(bitmap);

2.surfaceView加载图片

public class MySurFaceView extends SurfaceView implements Runnable, SurfaceHolder.Callback {
    private SurfaceHolder mSurfaceHolder;
    private Canvas mCanvas;
    private Paint mPaint;
    private Thread mThread;
    private int x = 50, y = 50, r = 100; // 圆的坐标和半径

    public MySurFaceView(Context context) {
        super(context);
        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        setFocusable(true);


    }

    public MySurFaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        setFocusable(true);

    }

    public MySurFaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public MySurFaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void run() {
        doDraw();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        mThread = new Thread(this); 
        mThread.start(); 

    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }


    public void doDraw() {

        mCanvas = mSurfaceHolder.lockCanvas(); 
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        mCanvas.drawBitmap(bitmap,10,10,mPaint);
        mSurfaceHolder.unlockCanvasAndPost(mCanvas); 


    }

3.自定义view 显示图片

public class MyViw extends View {
    private Paint mPaint;

    public MyViw(Context context) {
        super(context);
    }

    public MyViw(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
    }

    public MyViw(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyViw(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        canvas.drawBitmap(bitmap, 10, 10, mPaint);
    }
}

布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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_show_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <demo.yuwan.myapplication.MySurFaceView
        android:id="@+id/my_sur_view"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center" />

    <demo.yuwan.myapplication.MyViw
        android:layout_marginLeft="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</android.support.design.widget.CoordinatorLayout>

猜你喜欢

转载自blog.csdn.net/Y_xiaohe1234/article/details/86525412