mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Refactor RingView; make text size consistent
This commit is contained in:
@@ -102,9 +102,9 @@ public class NumberView extends View
|
|||||||
width = MeasureSpec.getSize(widthMeasureSpec);
|
width = MeasureSpec.getSize(widthMeasureSpec);
|
||||||
height = MeasureSpec.getSize(heightMeasureSpec);
|
height = MeasureSpec.getSize(heightMeasureSpec);
|
||||||
|
|
||||||
labelTextSize = textSize * 0.35f;
|
labelTextSize = textSize;
|
||||||
labelMarginTop = textSize * 0.125f;
|
labelMarginTop = textSize * 0.35f;
|
||||||
numberTextSize = textSize;
|
numberTextSize = textSize * 2.85f;
|
||||||
|
|
||||||
createNumberLayout();
|
createNumberLayout();
|
||||||
int numberWidth = numberLayout.getWidth();
|
int numberWidth = numberLayout.getWidth();
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.views;
|
package org.isoron.uhabits.views;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
@@ -32,12 +33,9 @@ import android.view.View;
|
|||||||
|
|
||||||
import org.isoron.helpers.ColorHelper;
|
import org.isoron.helpers.ColorHelper;
|
||||||
import org.isoron.helpers.DialogHelper;
|
import org.isoron.helpers.DialogHelper;
|
||||||
import org.isoron.uhabits.R;
|
|
||||||
|
|
||||||
public class RingView extends View
|
public class RingView extends View
|
||||||
{
|
{
|
||||||
|
|
||||||
private int size;
|
|
||||||
private int color;
|
private int color;
|
||||||
private float percentage;
|
private float percentage;
|
||||||
private float labelMarginTop;
|
private float labelMarginTop;
|
||||||
@@ -46,12 +44,22 @@ public class RingView extends View
|
|||||||
private RectF rect;
|
private RectF rect;
|
||||||
private StaticLayout labelLayout;
|
private StaticLayout labelLayout;
|
||||||
|
|
||||||
|
private int width;
|
||||||
|
private int height;
|
||||||
|
private float diameter;
|
||||||
|
private float maxDiameter;
|
||||||
|
private float textSize;
|
||||||
|
|
||||||
public RingView(Context context, AttributeSet attrs)
|
public RingView(Context context, AttributeSet attrs)
|
||||||
{
|
{
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
|
||||||
this.size = (int) context.getResources().getDimension(R.dimen.small_square_size) * 4;
|
|
||||||
this.label = DialogHelper.getAttribute(context, attrs, "label");
|
this.label = DialogHelper.getAttribute(context, attrs, "label");
|
||||||
|
this.maxDiameter = DialogHelper.getFloatAttribute(context, attrs, "maxDiameter");
|
||||||
|
this.textSize = DialogHelper.getFloatAttribute(context, attrs, "textSize");
|
||||||
|
|
||||||
|
this.maxDiameter = DialogHelper.dpToPixels(context, maxDiameter);
|
||||||
|
this.textSize = DialogHelper.spToPixels(context, textSize);
|
||||||
this.color = ColorHelper.palette[7];
|
this.color = ColorHelper.palette[7];
|
||||||
this.percentage = 0.75f;
|
this.percentage = 0.75f;
|
||||||
init();
|
init();
|
||||||
@@ -77,21 +85,27 @@ public class RingView extends View
|
|||||||
pRing.setColor(color);
|
pRing.setColor(color);
|
||||||
pRing.setTextAlign(Paint.Align.CENTER);
|
pRing.setTextAlign(Paint.Align.CENTER);
|
||||||
|
|
||||||
pRing.setTextSize(size * 0.15f);
|
|
||||||
labelMarginTop = size * 0.10f;
|
|
||||||
labelLayout = new StaticLayout(label, pRing, size, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f,
|
|
||||||
false);
|
|
||||||
|
|
||||||
rect = new RectF();
|
rect = new RectF();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressLint("DrawAllocation")
|
||||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
||||||
{
|
{
|
||||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||||
|
|
||||||
int width = Math.max(size, labelLayout.getWidth());
|
width = MeasureSpec.getSize(widthMeasureSpec);
|
||||||
int height = (int) (size + labelLayout.getHeight() + labelMarginTop);
|
height = MeasureSpec.getSize(heightMeasureSpec);
|
||||||
|
|
||||||
|
diameter = Math.min(maxDiameter, width);
|
||||||
|
|
||||||
|
pRing.setTextSize(textSize);
|
||||||
|
labelMarginTop = textSize * 0.80f;
|
||||||
|
labelLayout = new StaticLayout(label, pRing, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f,
|
||||||
|
false);
|
||||||
|
|
||||||
|
width = Math.max(width, labelLayout.getWidth());
|
||||||
|
height = (int) (diameter + labelLayout.getHeight() + labelMarginTop);
|
||||||
|
|
||||||
setMeasuredDimension(width, height);
|
setMeasuredDimension(width, height);
|
||||||
}
|
}
|
||||||
@@ -100,10 +114,11 @@ public class RingView extends View
|
|||||||
protected void onDraw(Canvas canvas)
|
protected void onDraw(Canvas canvas)
|
||||||
{
|
{
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
float thickness = size * 0.15f;
|
float thickness = diameter * 0.15f;
|
||||||
|
|
||||||
pRing.setColor(color);
|
pRing.setColor(color);
|
||||||
rect.set(0, 0, size, size);
|
rect.set(0, 0, diameter, diameter);
|
||||||
|
rect.offset((width - diameter) / 2, 0);
|
||||||
canvas.drawArc(rect, -90, 360 * percentage, true, pRing);
|
canvas.drawArc(rect, -90, 360 * percentage, true, pRing);
|
||||||
|
|
||||||
pRing.setColor(Color.rgb(230, 230, 230));
|
pRing.setColor(Color.rgb(230, 230, 230));
|
||||||
@@ -113,14 +128,14 @@ public class RingView extends View
|
|||||||
rect.inset(thickness, thickness);
|
rect.inset(thickness, thickness);
|
||||||
canvas.drawArc(rect, -90, 360, true, pRing);
|
canvas.drawArc(rect, -90, 360, true, pRing);
|
||||||
|
|
||||||
float lineHeight = pRing.getFontSpacing();
|
|
||||||
pRing.setColor(Color.GRAY);
|
pRing.setColor(Color.GRAY);
|
||||||
pRing.setTextSize(size * 0.2f);
|
pRing.setTextSize(diameter * 0.2f);
|
||||||
|
float lineHeight = pRing.getFontSpacing();
|
||||||
canvas.drawText(String.format("%.0f%%", percentage * 100), rect.centerX(),
|
canvas.drawText(String.format("%.0f%%", percentage * 100), rect.centerX(),
|
||||||
rect.centerY() + lineHeight / 3, pRing);
|
rect.centerY() + lineHeight / 3, pRing);
|
||||||
|
|
||||||
pRing.setTextSize(size * 0.15f);
|
pRing.setTextSize(textSize);
|
||||||
canvas.translate(size / 2, size + labelMarginTop);
|
canvas.translate(width / 2, diameter + labelMarginTop);
|
||||||
labelLayout.draw(canvas);
|
labelLayout.draw(canvas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,18 +31,28 @@
|
|||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
style="@style/cardStyle"
|
style="@style/cardStyle"
|
||||||
android:gravity="center">
|
android:gravity="start">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tvOverview"
|
android:id="@+id/tvOverview"
|
||||||
style="@style/cardHeaderStyle"
|
style="@style/cardHeaderStyle"
|
||||||
android:text="@string/overview"/>
|
android:text="@string/overview"/>
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RingView
|
<LinearLayout
|
||||||
android:id="@+id/scoreRing"
|
android:id="@+id/llOverview"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
app:label="@string/habit_strength"/>
|
android:layout_gravity="center"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<org.isoron.uhabits.views.RingView
|
||||||
|
android:id="@+id/scoreRing"
|
||||||
|
style="@style/smallDataViewStyle"
|
||||||
|
app:label="@string/habit_strength"
|
||||||
|
app:maxDiameter="60"
|
||||||
|
app:textSize="12"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
@@ -65,28 +75,28 @@
|
|||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
<org.isoron.uhabits.views.RepetitionCountView
|
||||||
app:label="@string/month"
|
style="@style/smallDataViewStyle"
|
||||||
app:interval="31"
|
app:interval="31"
|
||||||
app:textSize="34"
|
app:label="@string/month"
|
||||||
style="@style/repetitionCountStyle"/>
|
app:textSize="12"/>
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
<org.isoron.uhabits.views.RepetitionCountView
|
||||||
app:label="@string/quarter"
|
style="@style/smallDataViewStyle"
|
||||||
app:interval="92"
|
app:interval="92"
|
||||||
app:textSize="34"
|
app:label="@string/quarter"
|
||||||
style="@style/repetitionCountStyle"/>
|
app:textSize="12"/>
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
<org.isoron.uhabits.views.RepetitionCountView
|
||||||
app:label="@string/year"
|
style="@style/smallDataViewStyle"
|
||||||
app:interval="365"
|
app:interval="365"
|
||||||
app:textSize="34"
|
app:label="@string/year"
|
||||||
style="@style/repetitionCountStyle"/>
|
app:textSize="12"/>
|
||||||
|
|
||||||
<org.isoron.uhabits.views.RepetitionCountView
|
<org.isoron.uhabits.views.RepetitionCountView
|
||||||
app:label="@string/all_time"
|
style="@style/smallDataViewStyle"
|
||||||
app:interval="0"
|
app:interval="0"
|
||||||
app:textSize="34"
|
app:label="@string/all_time"
|
||||||
style="@style/repetitionCountStyle"/>
|
app:textSize="12"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|||||||
@@ -93,7 +93,7 @@
|
|||||||
<item name="android:paddingTop">12dp</item>
|
<item name="android:paddingTop">12dp</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style name="repetitionCountStyle">
|
<style name="smallDataViewStyle">
|
||||||
<item name="android:layout_width">0dp</item>
|
<item name="android:layout_width">0dp</item>
|
||||||
<item name="android:layout_height">wrap_content</item>
|
<item name="android:layout_height">wrap_content</item>
|
||||||
<item name="android:layout_weight">1</item>
|
<item name="android:layout_weight">1</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user