缩放画布绘图以适合活动中的自定义视图

问题描述 投票:0回答:1

我可以使用一些帮助来试图找出如何缩放画布以适应活动中的自定义视图。目前,绘制画布上的绘图以适合应用程序正在运行的任何设备的屏幕大小(即,它在中间绘制),例如,如下所示:

enter image description here

但是,当在活动的自定义视图中查看时,由于活动中自定义视图的大小小于屏幕大小,因此图像被截断,例如:

enter image description here

所需的结果是缩放画布绘图以适合活动中的自定义视图(例如,它显示第一个图像),下面是我的代码:

绘制画布的类:

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
    }
}

活动类:

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的新手,可以真正使用一些帮助,干杯!

java android canvas scaling custom-view
1个回答
0
投票

如果为自定义视图覆盖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);
}
© www.soinside.com 2019 - 2024. All rights reserved.