音视频开发day01

图片绘制三策略:

public class Day01 extends AppCompatActivity implements SurfaceHolder.Callback{
    private Intent mIntent;
    private static Bitmap bitmap;

    {
        Context context = App.getApp();
        Resources resources = context.getResources();
        int resID = resources.getIdentifier("vv", "mipmap", context.getPackageName());
        bitmap = BitmapFactory.decodeResource(resources, resID);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day01);
        SurfaceView surfaceView = findViewById(R.id.surfaceview);
        surfaceView.getHolder().addCallback(this);
        //解决surfaceView背景默认透明的问题,换成透明的
        surfaceView.setZOrderOnTop(true);
        surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    }

    /**
     * ImageView绘制图片
     * @param view
     */
    public void image_view(View view) {
        ImageView imageView = findViewById(R.id.imageview);
        imageView.setImageBitmap(bitmap);
    }

    /**
     * SurfaceView绘制图片
     * @param view
     */
    public void surface_view(View view){

    }
    public void draw_view(View view){

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (holder == null){
            return;
        }
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        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) {

    }

    public static class DrawImageView extends View{

        Paint paint;
        public DrawImageView(Context context) {
            this(context,null);
        }

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

        public DrawImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (bitmap != null){
                canvas.drawBitmap(bitmap,0,0,paint);
            }
        }
    }
}


使用自定义内部类view的规则
1.自定义的类必须是静态类;
2.使用view作为XML文件中的tag,注意,v是小写字母,小写字母v,小写字母v;
3.添加class属性,注意,没有带android:命名空间的,表明该自定义view的完整路径,且外部类与内部类之间用美元“”连接,而不是“.”,注意,要美元“”,不要“.”;
4.自定义的view至少应该含有带有Context, AttributeSet这两个参数的构造函数

SurfaceView当可见时,就已经初始化,这个时候就应该设置SurfaceHolder回调,否则不会回调Holder中的监听方法。

你可能感兴趣的:(音视频开发day01)