android customView大小无法正常工作

时间:2019-07-05 04:02:15

标签: android arrays android-custom-view

我创建了一个自定义视图,该视图应使用以下几点绘制星星:

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星。但是当我在xml中为StarView设置width = 200dp和height = 200dp时,星形小于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)? 我的第二个问题是如何在活动和Java代码中定义mPoints数组,以防万一我想要一颗外观不同的星星?

0 个答案:

没有答案