作为应届生,刚入职公司,现在想从零开始学习一个全新的领域——音视频开发,仅此记录一下。
ImageView绘制图片
这种常规的绘制图片方式,简单粗暴。
String pathName = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/Audio/1.png";
binding.btBitmap.setOnClickListener(view -> {
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
binding.imageView.setImageBitmap(bitmap);
});
SurFaceView绘制图片
使用surFaceView可以在独立的线程绘制UI,不会引起UI线程的卡帧。底层实现了双缓冲机制,绘制效率较高。
xml代码
<SurfaceView
android:id="@+id/surface"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="invisible"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
java代码
binding.surface.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
if (surfaceHolder == null) {
return;
}
Paint paint = new Paint();
//去锯齿
paint.setAntiAlias(true);
//描边
paint.setStyle(Paint.Style.STROKE);
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
//锁定surface画布
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(bitmap, 0, 0, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
}
});
自定义View绘制
使用自定义view会更加灵活,当然如果只绘制一张普通的图片其实,这种方式我觉得并不方便。
public class JImageView extends View {
Paint paint = new Paint();
Bitmap bitmap;
String pathName = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/Audio/1.png";
public JImageView(Context context) {
super(context);
init();
}
public JImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public JImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
protected void init(){
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
bitmap = BitmapFactory.decodeFile(pathName);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap !=null){
canvas.drawBitmap(bitmap,0,0,paint);
}
}
}
版权说明,非原创,转载请注明出处 https://www.cnblogs.com/renhui/p/7456956.html
版权声明:本文为m0_57372054原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。