mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Use alternative design with units on NumberButtonView
This commit is contained in:
@@ -198,7 +198,7 @@ public class CheckmarkPanelView extends LinearLayout
|
||||
int values[] = new int[nButtons];
|
||||
|
||||
for (int i = 0; i < nButtons; i++)
|
||||
values[i] = new Random().nextInt(3);
|
||||
values[i] = Math.min(2, new Random().nextInt(4));
|
||||
|
||||
setValues(values);
|
||||
}
|
||||
|
||||
@@ -20,11 +20,12 @@
|
||||
package org.isoron.uhabits.activities.habits.list.views;
|
||||
|
||||
import android.content.*;
|
||||
import android.content.res.*;
|
||||
import android.graphics.*;
|
||||
import android.support.annotation.*;
|
||||
import android.text.*;
|
||||
import android.util.*;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.activities.habits.list.controllers.*;
|
||||
@@ -33,9 +34,12 @@ import org.isoron.uhabits.utils.*;
|
||||
import static org.isoron.uhabits.utils.AttributeSetUtils.*;
|
||||
import static org.isoron.uhabits.utils.ColorUtils.*;
|
||||
|
||||
public class NumberButtonView extends TextView
|
||||
public class NumberButtonView extends View
|
||||
{
|
||||
private static Typeface TYPEFACE =
|
||||
private static Typeface BOLD_TYPEFACE =
|
||||
Typeface.create("sans-serif-condensed", Typeface.BOLD);
|
||||
|
||||
private static Typeface NORMAL_TYPEFACE =
|
||||
Typeface.create("sans-serif-condensed", Typeface.NORMAL);
|
||||
|
||||
private int color;
|
||||
@@ -44,7 +48,19 @@ public class NumberButtonView extends TextView
|
||||
|
||||
private int threshold;
|
||||
|
||||
private StyledResources res;
|
||||
private String unit;
|
||||
|
||||
private RectF rect;
|
||||
|
||||
private TextPaint pRegular;
|
||||
|
||||
private Resources res;
|
||||
|
||||
private TextPaint pBold;
|
||||
|
||||
private int grey;
|
||||
|
||||
private float em;
|
||||
|
||||
public NumberButtonView(@Nullable Context context)
|
||||
{
|
||||
@@ -52,33 +68,34 @@ public class NumberButtonView extends TextView
|
||||
init();
|
||||
}
|
||||
|
||||
public NumberButtonView(@Nullable Context context,
|
||||
@Nullable AttributeSet attrs)
|
||||
public NumberButtonView(@Nullable Context ctx, @Nullable AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
super(ctx, attrs);
|
||||
init();
|
||||
|
||||
if (context != null && attrs != null)
|
||||
if (ctx != null && attrs != null)
|
||||
{
|
||||
int color = getIntAttribute(context, attrs, "color", 0);
|
||||
int value = getIntAttribute(context, attrs, "value", 0);
|
||||
int threshold = getIntAttribute(context, attrs, "threshold", 1);
|
||||
int color = getIntAttribute(ctx, attrs, "color", 0);
|
||||
int value = getIntAttribute(ctx, attrs, "value", 0);
|
||||
int threshold = getIntAttribute(ctx, attrs, "threshold", 1);
|
||||
String unit = getAttribute(ctx, attrs, "unit", "min");
|
||||
setColor(getAndroidTestColor(color));
|
||||
setThreshold(threshold);
|
||||
setValue(value);
|
||||
setUnit(unit);
|
||||
}
|
||||
}
|
||||
|
||||
private static String formatValue(int v)
|
||||
{
|
||||
double fv = (double) v;
|
||||
if(v >= 1e9) return String.format("%.1fG", fv / 1e9);
|
||||
if(v >= 1e8) return String.format("%.0fM", fv / 1e6);
|
||||
if(v >= 1e7) return String.format("%.1fM", fv / 1e6);
|
||||
if(v >= 1e6) return String.format("%.1fM", fv / 1e6);
|
||||
if(v >= 1e5) return String.format("%.0fk", fv / 1e3);
|
||||
if(v >= 1e4) return String.format("%.1fk", fv / 1e3);
|
||||
if(v >= 1e3) return String.format("%.1fk", fv / 1e3);
|
||||
if (v >= 1e9) return String.format("%.1fG", fv / 1e9);
|
||||
if (v >= 1e8) return String.format("%.0fM", fv / 1e6);
|
||||
if (v >= 1e7) return String.format("%.1fM", fv / 1e6);
|
||||
if (v >= 1e6) return String.format("%.1fM", fv / 1e6);
|
||||
if (v >= 1e5) return String.format("%.0fk", fv / 1e3);
|
||||
if (v >= 1e4) return String.format("%.1fk", fv / 1e3);
|
||||
if (v >= 1e3) return String.format("%.1fk", fv / 1e3);
|
||||
return String.format("%d", v);
|
||||
}
|
||||
|
||||
@@ -97,35 +114,72 @@ public class NumberButtonView extends TextView
|
||||
public void setThreshold(int threshold)
|
||||
{
|
||||
this.threshold = threshold;
|
||||
updateText();
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
public void setUnit(String unit)
|
||||
{
|
||||
this.unit = unit;
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
public void setValue(int value)
|
||||
{
|
||||
this.value = value;
|
||||
updateText();
|
||||
postInvalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas)
|
||||
{
|
||||
if(value < threshold)
|
||||
{
|
||||
pRegular.setColor(grey);
|
||||
pBold.setColor(grey);
|
||||
}
|
||||
else
|
||||
{
|
||||
pRegular.setColor(color);
|
||||
pBold.setColor(color);
|
||||
}
|
||||
|
||||
String fv = formatValue(value);
|
||||
|
||||
rect.set(0, 0, getWidth(), getHeight());
|
||||
rect.offset(0, - 0.1f * em);
|
||||
canvas.drawText(fv, rect.centerX(), rect.centerY(), pBold);
|
||||
|
||||
rect.offset(0, 1.25f * em);
|
||||
canvas.drawText(unit, rect.centerX(), rect.centerY(), pRegular);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
||||
{
|
||||
int width = (int) res.getDimension(R.dimen.checkmarkWidth);
|
||||
int height = (int) res.getDimension(R.dimen.checkmarkHeight);
|
||||
setMeasuredDimension(width, height);
|
||||
}
|
||||
|
||||
private void init()
|
||||
{
|
||||
res = new StyledResources(getContext());
|
||||
StyledResources sr = new StyledResources(getContext());
|
||||
res = getContext().getResources();
|
||||
|
||||
setWillNotDraw(false);
|
||||
rect = new RectF();
|
||||
pRegular = new TextPaint();
|
||||
pRegular.setTextSize(res.getDimension(R.dimen.smallerTextSize));
|
||||
pRegular.setTypeface(NORMAL_TYPEFACE);
|
||||
pRegular.setAntiAlias(true);
|
||||
pRegular.setTextAlign(Paint.Align.CENTER);
|
||||
|
||||
setMinHeight(
|
||||
getResources().getDimensionPixelSize(R.dimen.checkmarkHeight));
|
||||
setMinWidth(
|
||||
getResources().getDimensionPixelSize(R.dimen.checkmarkWidth));
|
||||
pBold = new TextPaint();
|
||||
pBold.setTextSize(res.getDimension(R.dimen.smallTextSize));
|
||||
pBold.setTypeface(BOLD_TYPEFACE);
|
||||
pBold.setAntiAlias(true);
|
||||
pBold.setTextAlign(Paint.Align.CENTER);
|
||||
|
||||
setFocusable(false);
|
||||
setGravity(Gravity.CENTER);
|
||||
setTypeface(TYPEFACE);
|
||||
}
|
||||
|
||||
private void updateText()
|
||||
{
|
||||
int lowColor = res.getColor(R.attr.lowContrastTextColor);
|
||||
setTextColor(value >= threshold ? color : lowColor);
|
||||
setText(formatValue(value));
|
||||
em = pBold.measureText("m");
|
||||
grey = sr.getColor(R.attr.lowContrastTextColor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,8 @@ public class NumberPanelView extends LinearLayout
|
||||
|
||||
private Controller controller;
|
||||
|
||||
private String unit;
|
||||
|
||||
@NonNull
|
||||
private Habit habit;
|
||||
|
||||
@@ -79,11 +81,18 @@ public class NumberPanelView extends LinearLayout
|
||||
setColor(getAndroidTestColor(paletteColor));
|
||||
setButtonCount(getIntAttribute(ctx, attrs, "button_count", 5));
|
||||
setThreshold(getIntAttribute(ctx, attrs, "threshold", 1));
|
||||
setUnit(getAttribute(ctx, attrs, "unit", "min"));
|
||||
}
|
||||
|
||||
if(isInEditMode()) initEditMode();
|
||||
}
|
||||
|
||||
public void setUnit(String unit)
|
||||
{
|
||||
this.unit = unit;
|
||||
setupButtons();
|
||||
}
|
||||
|
||||
public void initEditMode()
|
||||
{
|
||||
int values[] = new int[nButtons];
|
||||
@@ -243,6 +252,7 @@ public class NumberPanelView extends LinearLayout
|
||||
buttonView.setValue(values[i + dataOffset]);
|
||||
buttonView.setColor(color);
|
||||
buttonView.setThreshold(threshold);
|
||||
buttonView.setUnit(unit);
|
||||
setupButtonControllers(timestamp, buttonView);
|
||||
timestamp -= day;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="1"
|
||||
app:threshold="10"
|
||||
app:threshold="10000"
|
||||
app:unit="steps"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
|
||||
@@ -38,7 +39,22 @@
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="2"
|
||||
app:threshold="5000"
|
||||
app:threshold="2000"
|
||||
app:unit="cals"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="2"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="2"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
|
||||
@@ -47,6 +63,7 @@
|
||||
app:button_count="8"
|
||||
app:color="6"
|
||||
app:threshold="10"
|
||||
app:unit="min"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
|
||||
@@ -67,7 +84,8 @@
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="10"
|
||||
app:threshold="5"
|
||||
app:threshold="750"
|
||||
app:unit="words"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
|
||||
@@ -77,4 +95,13 @@
|
||||
app:color="10"
|
||||
/>
|
||||
|
||||
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:button_count="8"
|
||||
app:color="8"
|
||||
app:threshold="75"
|
||||
app:unit="pages"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
<resources>
|
||||
<dimen name="baseSize">20dp</dimen>
|
||||
<dimen name="checkmarkWidth">42dp</dimen>
|
||||
<dimen name="checkmarkWidth">48dp</dimen>
|
||||
<dimen name="checkmarkHeight">48dp</dimen>
|
||||
<dimen name="history_editor_max_height">450dp</dimen>
|
||||
<dimen name="history_editor_padding">8dp</dimen>
|
||||
|
||||
Reference in New Issue
Block a user