Android 音视频开发(一) : 通过三种方式绘制图片

实践任务一:绘制一张图片。首先选定显示图片的控件,一般用ImageView,还有SurefaceView和自定义View

1:ImageView

这个是就是android自带的显示图片控件,有三种图片的来源。

iv_show.setImageDrawable();//传入的是Drawable
iv_show.setImageResource();//传入图片资源id
iv_show.setImageBitmap();//传入Bitmap

 

2:SurfaceView显示图片

SurfaceView与普通的View的区别在于:

  • surfaceview不需要在UI线程绘制,可以在子线程绘制
  • surfaceview提供了双缓冲的机制,绘制效率高
  • surfaceview是创建一个置于应用窗口之上的窗口,所有不能应用动画、变换和缩放,也没有办法叠加。

具体实现代码:

public class SurfaceViewActivity extends AppCompatActivity implements SurfaceHolder.Callback {
    SurfaceView suf_view;
    private ExecutorService mThread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surface_view);
        suf_view = findViewById(R.id.suf_view);
        // 创建一个只有一个线程的线程池,其实用Thread也可以
        mThread = new ThreadPoolExecutor(1, 1, 2000L, TimeUnit.MILLISECONDS,
                new LinkedBlockingDeque<Runnable>());
        // 添加SurfaceHolder.callback,在回调中可以绘制
        suf_view.getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 执行绘制
        mThread.execute(new DrawRunnable());
    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (!mThread.isShutdown()) {
            mThread.shutdown();
        }
    }
    private class DrawRunnable implements Runnable {
        @Override
        public void run() {
            Bitmap bimap = BitmapFactory.decodeResource(SurfaceViewActivity.this.getResources(),
                    R.drawable.timg);
            SurfaceHolder surfaceHolder = suf_view.getHolder();
            Canvas canvas = surfaceHolder.lockCanvas(); // 获取画布
            Paint paint = new Paint();
            Rect srcRect = new Rect(0, 0, bimap.getHeight(), bimap.getWidth());
            Rect destRect = BitmapUtil.getBitmapRect(bimap,suf_view);
            canvas.drawBitmap(bimap, srcRect, destRect, paint);
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }


}

3:自定义view显示图片

源码:

public class CustomView extends View {
    private Paint mPaint;
    private Bitmap mBitmap;

    public CustomView(Context context) {
        this(context,null);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.timg);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Rect srcRect = new Rect(0, 0, mBitmap.getHeight(), mBitmap.getWidth());
        Rect destRect = BitmapUtil.getBitmapRect(mBitmap,CustomView.this); // 获取调整后的bitmap的显示位置
        canvas.drawBitmap(mBitmap, srcRect, destRect, mPaint);
    }
}

注:如果图片来自手机的sd卡,要加上权限

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

源码:https://github.com/ChloeDimen/AudioAndVideo/tree/master

发布了64 篇原创文章 · 获赞 51 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览