音视频开发任务(1)——绘制图片

一、ImageView设置图片的方式

  1. imageView.setImageDrawable
    imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.baby1));
    drawable中有一张图片baby.png

  2. imageView.setsetImageBitmap(bitmap)
    查看该方式源码:

    public void setImageBitmap(Bitmap bm) {
            // Hacky fix to force setImageDrawable to do a full setImageDrawable
            // instead of doing an object reference comparison
            mDrawable = null;
            if (mRecycleableBitmapDrawable == null) {
                mRecycleableBitmapDrawable = new BitmapDrawable(mContext.getResources(), bm);
            } else {
                mRecycleableBitmapDrawable.setBitmap(bm);
            }
            setImageDrawable(mRecycleableBitmapDrawable);
        }

    可以看出该方法依旧时是把bitmap图像转为了drawable对象

  3. imageView.setImageResource(int id)
    使用这种方式,图片必须是drawable目录下的资源,另外该方法是在UI线程中对图片读取和解析,可能会造成延迟,官方注释如下:

    /**
     * Sets a drawable as the content of this ImageView.
     * 

    This does Bitmap reading and decoding on the UI * thread, which can cause a latency hiccup. If that's a concern, * consider using {@link #setImageDrawable(android.graphics.drawable.Drawable)} or * {@link #setImageBitmap(android.graphics.Bitmap)} and * {@link android.graphics.BitmapFactory} instead.

    * * @param resId the resource identifier of the drawable * * @attr ref android.R.styleable#ImageView_src */ @android.view.RemotableViewMethod(asyncImpl="setImageResourceAsync") public void setImageResource(@DrawableRes int resId)

二、SurfaceView绘制图片

surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                Log.i(TAG,"surfaceCreated");
                if (surfaceHolder == null) {
                    return;
                }

                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setStyle(Paint.Style.STROKE);

                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.baby2);  // 获取bitmap
                //原始尺寸
                int width=bitmap.getWidth();
                int height=bitmap.getHeight();
                Log.i(TAG,"w:"+width+"  "+"h:"+height);
                //目标尺寸
                int newWidth=surfaceView.getWidth();
                int newHeigtht=surfaceView.getHeight();
                //压缩比
                float scaleWidth=(float)newWidth/width;
                float scaleHeight=(float)newHeigtht/height;
                //获取matrix
                Matrix matrix=new Matrix();
                matrix.postScale(scaleWidth,scaleHeight);
                //获取新的bitmap
                bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
                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) {

            }
        });

三、自定义View绘制图片

  1. 创建自定义View

    public class MyView extends View {
    Paint paint=new Paint();
    
    public MyView(Context context) {
        super(context);
    }
    
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    
    }
    
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        paint.setAntiAlias(true); // 是否抗锯齿
    
        //画圆
        //canvas.drawCircle(50,50,50,paint);
    
        //画图片
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.baby2);
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        //目标尺寸
        int newWidth=300;
        int newHeigtht=200;
        //压缩比
        float scaleWidth=(float)newWidth/width;
        float scaleHeight=(float)newHeigtht/height;
        //获取matrix
        Matrix matrix=new Matrix();
        matrix.postScale(scaleWidth,scaleHeight);
        //获取新的bitmap
        bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
        canvas.drawBitmap(bitmap,0,0,paint);
    
    }

}

然后在XML布局文件中引入Myview即可

你可能感兴趣的:(音视频开发任务(1)——绘制图片)