一/ 图片展示

1. ImageView(-> Bitmap

Bitmap bitmap = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(bitmap);

2. SurfaceView

SurfaceView surfaceView = new SurfaceView(this);
    surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
             if (holder == null) {
                 return;
             }

             Paint paint = new Paint();
             paint.setAntiAlias(true);
             paint.setStyle(Paint.Style.STROKE);
             Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_img);
             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) {
     }
 });

3. Custom View (-> Android 绘制

public class CustomPicView extends View {

    private Paint paint;
    private Bitmap bitmap;


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

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

    public CustomPicView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.test_img);
    }

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

 

posted @ 2018-06-08 01:08  小爷宋  阅读(229)  评论(0编辑  收藏  举报