一、使用imageView
try {
InputStream inputStream = getAssets().open("11.png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
((ImageView)findViewById(R.id.image1)).setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
二、使用surfaceView
SurfaceView surfaceView = findViewById(R.id.image2);
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
InputStream inputStream = null;
try {
inputStream = getAssets().open("11.png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(bitmap, 0, 0, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
}
});
三、使用自定义view
public class CustomImage extends View {
Paint paint = new Paint();
Bitmap bitmap;
public CustomImage(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
try {
InputStream inputStream = context.getAssets().open("11.png");
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, paint);
}
}
}