在SomeView类的onDrawBitmap方法中将位图拟合到屏幕

时间:2018-12-09 12:37:03

标签: java android bitmap android-canvas android-bitmap

首先,它不仅仅是将位图缩放到所有屏幕。它不是重复的。我搜寻了。

我有一个名为SomeView的类,我在 MainActivity 上将其称为 SomeView类

    setContentView(new SomeView(MainActivity.this, bitmap ));

MainActivity发送Sendind位图。

我正在使用

加载图像
  canvas.drawBitmap(bitmap, 0, 0, null);

我也尝试过。

 RectF dest = new RectF(0,0,getWidth(),getHeight());
  canvas.drawBitmap(bitmap, null, dest, null);

并且也尝试过..

 RectF dest = new RectF(0,0,bitmap.getWidth(),bitmap.getHeight());
      canvas.drawBitmap(bitmap, null, dest, null);

但是对我没有任何帮助...即将来临的位图不适合屏幕且不适合居中。

我有什么。

enter image description here

我需要的。

enter image description here

我的视图活动代码。 SomeView.Java

  public SomeView(Context c, Bitmap b) {
        super(c);

        bitmap = b;
        mContext = c;
        setFocusable(true);
        setFocusableInTouchMode(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
        paint.setStrokeWidth(15);
        paint.setColor(Color.GREEN);


        this.setOnTouchListener(this);
        points = new ArrayList<Point>();

        bfirstpoint = false;
    }

    public SomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setFocusable(true);
        setFocusableInTouchMode(true);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);

        this.setOnTouchListener(this);
        points = new ArrayList<Point>();
        bfirstpoint = false;

    }

    public void onDraw(Canvas canvas) {

        canvas.drawBitmap(bitmap, 0, 0, null);

        Path path = new Path();
        boolean first = true;

        for (int i = 0; i < points.size(); i += 2) {
            Point point = points.get(i);
            if (first) {
                first = false;
                path.moveTo(point.x, point.y);
            } else if (i < points.size() - 1) {
                Point next = points.get(i + 1);
                path.quadTo(point.x, point.y, next.x, next.y);
            } else {
                mlastpoint = points.get(i);
                path.lineTo(point.x, point.y);
            }
        }
        canvas.drawPath(path, paint);
    }

1 个答案:

答案 0 :(得分:1)

这是解决方案。

public class UsuarioViewModel
{
    public IAsyncEnumerable<UsuarioDTO> Usuarios { get; set; }
}