gpt4 book ai didi

java - 在 SomeView 类 onDrawBitmap 方法中使位图适合屏幕

转载 作者:行者123 更新时间:2023-12-02 10:29:29 25 4
gpt4 key购买 nike

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

我有一个名为 SomeView 的类,我在 MainActivity 上调用此 SomeView 类,如下所示;

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

MainActivity发送位图。

我正在使用

加载图像
  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

我的查看 Activity 代码。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);
}

最佳答案

这是解决方案。

RectF src = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF dst = new RectF(0, 0, getWidth(), getHeight());

matrix = new Matrix();
matrix.setRectToRect(src, dst, Matrix.ScaleToFit.CENTER);

Log.d(TAG, "MATRIX VALUE: " + matrix);

canvas.drawBitmap(bitmap, matrix, null);

关于java - 在 SomeView 类 onDrawBitmap 方法中使位图适合屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53692358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com