I've created a custom view that should draw a star using these points:
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};
as you can see this is a 200*200 star. but when I set width = 200dp and height = 200dp for StarView in xml the star is smaller than the StarView size.
look at this image:
my StarView code:
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);
}
}
and my 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>
How ca I make 200*200 star fill 200*200 canvas (StarView)? my second question is how to define mPoints array in activity and java code, in case I want a star with different look?