缩放 canvas 绘图以适应 activity 中的自定义视图

Scaling canvas drawing to fit custom view in activity

我需要一些帮助来弄清楚如何缩放 canvas 以适应 activity 中的自定义视图。目前 canvas 上的绘图是根据应用 运行 所在的任何设备的屏幕大小绘制的(即绘制在中间),例如,如下所示:

但是,在 activity 中的自定义视图中查看时,由于 activity 中自定义视图的大小小于屏幕大小,图像被截断了例如:

期望的结果是缩放 canvas 绘图以适应 activity 中的自定义视图(例如它显示第一张图片),下面是我的代码:

绘制canvas的class:

public class DrawGraphTest extends View {

    int mWidth = this.getResources().getDisplayMetrics().widthPixels;
    int mHeight = this.getResources().getDisplayMetrics().heightPixels;


    public DrawGraphTest(Context context) {
        super(context);
        init();
    }

    public DrawGraphTest(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        //Various paints and such...

        //Set point to middle of screen for drawings
        point1 = new Point(mWidth / 2, mHeight / 2);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //Draw various stuff to canvas
    }
}

activity class:

public class GraphActivity extends AppCompatActivity {
    DrawGraphTest drawGraphTest;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graph);

        drawGraphTest = (DrawGraphTest)findViewById(R.id.drawGraphTest);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GraphActivity">

    <g.myPackage.DrawGraphTest
        android:id="@+id/drawGraphTest"
        android:layout_width="350dp"
        android:layout_height="300dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.041" />  
</android.support.constraint.ConstraintLayout>

我是 Android Studio 的新手,真的需要一些帮助,干杯!

如果您为您的自定义视图覆盖 onSizeChanged(int w, int h, int oldw, int oldh),您可以(重新)计算您的测量并将它们保存在成员变量中,您可以在调用 onDraw 时使用它们。

类似于:

private int width;
private int height;
private int paddingLeft;
private int paddingRight;
private int paddingTop;
private int paddingBottom;
private int centerX;
private int centerY;

@Override
public void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = w;
    height = h;
    paddingLeft = getPaddingLeft();
    paddingRight = getPaddingRight();
    paddingTop = getPaddingTop();
    paddingBottom = getPaddingBottom();
    int usableWidth = width - (paddingLeft + paddingRight);
    int usableHeight = height - (paddingTop + paddingBottom);
    centerX = paddingLeft + (usableWidth / 2);
    centerY = paddingTop + (usableHeight / 2);
}

您可以使用

Canvas.drawBitmap(Bitmap bitmap, 
                Rect src, 
                Rect dst, 
                Paint paint) 

其中 bitmap 是您的图片。 src 将包含图像的尺寸

src.top = 0, src.left = 0, src.right = src + bitmap.width, src.bottom = bitmap.height

您的目的地 dst 将是您希望它在 canvas 内绘制的位置。

dst.top = 0, dst.left = 0, dst.right = 50, dst.bottom = 50

例如,它会缩放您的位图,使其适合 50x50 正方形中 canvas 的左上角。