Android CustomView размер не работает правильно - PullRequest
0 голосов
/ 05 июля 2019

Я создал собственный вид, который должен рисовать звезду, используя следующие точки:

float [] mPoints = {0, 100, 75, 75, 75, 75, 100, 0, 100, 0, 125, 75, 125, 75, 200, 100, 200, 100, 125, 125, 125, 125,
        100, 200, 100, 200, 75, 125, 75, 125, 0, 100};

, как вы можете видеть, это звезда 200 * 200.но когда я устанавливаю width = 200dp и height = 200dp для StarView в xml, звезда меньше, чем размер StarView.посмотрите на это изображение: SrarView is bigger than Star

мой код StarView:

package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;

import androidx.annotation.Nullable;

public class StarView extends View {
    Paint mPaint;

    public StarView(Context context) {
        super(context);
        init(null);
    }

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

    public StarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(@Nullable AttributeSet set) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int desiredWidth = 200;
        int desiredHeight = 200;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        //Measure Width
        if (widthMode == MeasureSpec.EXACTLY) {
            //Must be this size
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            width = Math.min(desiredWidth, widthSize);
        } else {
            //Be whatever you want
            width = desiredWidth;
        }

        //Measure Height
        if (heightMode == MeasureSpec.EXACTLY) {
            //Must be this size
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            height = Math.min(desiredHeight, heightSize);
        } else {
            //Be whatever you want
            height = desiredHeight;
        }

        //MUST CALL THIS
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float[] mPoints = {0, 100, 75, 75, 75, 75, 100, 0, 100, 0, 125, 75, 125, 75, 200, 100, 200, 100, 125, 125, 125, 125,
                100, 200, 100, 200, 75, 125, 75, 125, 0, 100};
        canvas.drawLines(mPoints, mPaint);

    }
}

и мой xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/myLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.myapplication.StarView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:padding="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Как мне сделать200 * 200 звезда заполнить 200 * 200 холст (StarView)?Мой второй вопрос, как определить массив mPoints в активности и код Java, если я хочу звезду с другим взглядом?

...