简体   繁体   English

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

[英]Scaling canvas drawing to fit custom view in activity

I could use some help trying to figure out how to scale a canvas to fit a custom view in an activity.我可以使用一些帮助来尝试弄清楚如何缩放画布以适应活动中的自定义视图。 Currently the drawings on the canvas are drawn to fit the size of the screen for whatever device the app is being run on (ie it is drawn in the middle) for example, like this:当前,画布上的绘图被绘制以适合运行应用程序的任何设备的屏幕大小(即它被绘制在中间),例如,如下所示:

在此处输入图片说明

However, when it is viewed in the custom view in the activity the image is cut off due to the size of the custom view in the activity being smaller than the size of the screen eg:但是,当在活动的自定义视图中查看时,由于活动中自定义视图的大小小于屏幕大小,图像被截断,例如:

在此处输入图片说明

The desired result is to scale the canvas drawing to fit the custom view in the activity (eg it shows the first image), below is my code:所需的结果是缩放画布绘图以适应活动中的自定义视图(例如它显示第一张图像),下面是我的代码:

The class that draws the canvas:绘制画布的类:

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

The 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);
    }
}

The XML: 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>

I am very new to Android Studio and could really use some help, cheers!我对 Android Studio 很陌生,真的可以使用一些帮助,干杯!

If you override onSizeChanged(int w, int h, int oldw, int oldh) for your custom view, you can (re)calculate your measurements and hold them in member variables where you can use them when onDraw is called. 如果为自定义视图覆盖onSizeChanged(int w,int h,int oldw,int oldh) ,则可以(重新)计算测量值并将其保存在成员变量中,以便在调用onDraw时使用它们。

Something like: 就像是:

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

You can use您可以使用

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

where bitmap is your image.其中bitmap是您的图像。 src would contain the dimensions of the image src将包含图像的尺寸

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

Your destination dst would be where you want it drawn inside the canvas.您的目的地dst将是您希望它在画布内绘制的位置。

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

for example, it would scale your bitmap so that it fits in the top left of the canvas in a 50x50 square.例如,它会缩放您的位图,使其适合画布左上角的 50x50 正方形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM