Use alternative design with units on NumberButtonView

pull/157/merge
Alinson S. Xavier 9 years ago
parent d03edf2895
commit f0430ffeb3

@ -198,7 +198,7 @@ public class CheckmarkPanelView extends LinearLayout
int values[] = new int[nButtons]; int values[] = new int[nButtons];
for (int i = 0; i < nButtons; i++) for (int i = 0; i < nButtons; i++)
values[i] = new Random().nextInt(3); values[i] = Math.min(2, new Random().nextInt(4));
setValues(values); setValues(values);
} }

@ -20,11 +20,12 @@
package org.isoron.uhabits.activities.habits.list.views; package org.isoron.uhabits.activities.habits.list.views;
import android.content.*; import android.content.*;
import android.content.res.*;
import android.graphics.*; import android.graphics.*;
import android.support.annotation.*; import android.support.annotation.*;
import android.text.*;
import android.util.*; import android.util.*;
import android.view.*; import android.view.*;
import android.widget.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.habits.list.controllers.*; 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.AttributeSetUtils.*;
import static org.isoron.uhabits.utils.ColorUtils.*; 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); Typeface.create("sans-serif-condensed", Typeface.NORMAL);
private int color; private int color;
@ -44,7 +48,19 @@ public class NumberButtonView extends TextView
private int threshold; 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) public NumberButtonView(@Nullable Context context)
{ {
@ -52,20 +68,21 @@ public class NumberButtonView extends TextView
init(); init();
} }
public NumberButtonView(@Nullable Context context, public NumberButtonView(@Nullable Context ctx, @Nullable AttributeSet attrs)
@Nullable AttributeSet attrs)
{ {
super(context, attrs); super(ctx, attrs);
init(); init();
if (context != null && attrs != null) if (ctx != null && attrs != null)
{ {
int color = getIntAttribute(context, attrs, "color", 0); int color = getIntAttribute(ctx, attrs, "color", 0);
int value = getIntAttribute(context, attrs, "value", 0); int value = getIntAttribute(ctx, attrs, "value", 0);
int threshold = getIntAttribute(context, attrs, "threshold", 1); int threshold = getIntAttribute(ctx, attrs, "threshold", 1);
String unit = getAttribute(ctx, attrs, "unit", "min");
setColor(getAndroidTestColor(color)); setColor(getAndroidTestColor(color));
setThreshold(threshold); setThreshold(threshold);
setValue(value); setValue(value);
setUnit(unit);
} }
} }
@ -97,35 +114,72 @@ public class NumberButtonView extends TextView
public void setThreshold(int threshold) public void setThreshold(int threshold)
{ {
this.threshold = threshold; this.threshold = threshold;
updateText(); postInvalidate();
}
public void setUnit(String unit)
{
this.unit = unit;
postInvalidate();
} }
public void setValue(int value) public void setValue(int value)
{ {
this.value = value; this.value = value;
updateText(); postInvalidate();
} }
private void init() @Override
protected void onDraw(Canvas canvas)
{
if(value < threshold)
{
pRegular.setColor(grey);
pBold.setColor(grey);
}
else
{ {
res = new StyledResources(getContext()); pRegular.setColor(color);
pBold.setColor(color);
}
setWillNotDraw(false); String fv = formatValue(value);
setMinHeight( rect.set(0, 0, getWidth(), getHeight());
getResources().getDimensionPixelSize(R.dimen.checkmarkHeight)); rect.offset(0, - 0.1f * em);
setMinWidth( canvas.drawText(fv, rect.centerX(), rect.centerY(), pBold);
getResources().getDimensionPixelSize(R.dimen.checkmarkWidth));
setFocusable(false); rect.offset(0, 1.25f * em);
setGravity(Gravity.CENTER); canvas.drawText(unit, rect.centerX(), rect.centerY(), pRegular);
setTypeface(TYPEFACE);
} }
private void updateText() @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()
{ {
int lowColor = res.getColor(R.attr.lowContrastTextColor); StyledResources sr = new StyledResources(getContext());
setTextColor(value >= threshold ? color : lowColor); res = getContext().getResources();
setText(formatValue(value));
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);
pBold = new TextPaint();
pBold.setTextSize(res.getDimension(R.dimen.smallTextSize));
pBold.setTypeface(BOLD_TYPEFACE);
pBold.setAntiAlias(true);
pBold.setTextAlign(Paint.Align.CENTER);
em = pBold.measureText("m");
grey = sr.getColor(R.attr.lowContrastTextColor);
} }
} }

@ -57,6 +57,8 @@ public class NumberPanelView extends LinearLayout
private Controller controller; private Controller controller;
private String unit;
@NonNull @NonNull
private Habit habit; private Habit habit;
@ -79,11 +81,18 @@ public class NumberPanelView extends LinearLayout
setColor(getAndroidTestColor(paletteColor)); setColor(getAndroidTestColor(paletteColor));
setButtonCount(getIntAttribute(ctx, attrs, "button_count", 5)); setButtonCount(getIntAttribute(ctx, attrs, "button_count", 5));
setThreshold(getIntAttribute(ctx, attrs, "threshold", 1)); setThreshold(getIntAttribute(ctx, attrs, "threshold", 1));
setUnit(getAttribute(ctx, attrs, "unit", "min"));
} }
if(isInEditMode()) initEditMode(); if(isInEditMode()) initEditMode();
} }
public void setUnit(String unit)
{
this.unit = unit;
setupButtons();
}
public void initEditMode() public void initEditMode()
{ {
int values[] = new int[nButtons]; int values[] = new int[nButtons];
@ -243,6 +252,7 @@ public class NumberPanelView extends LinearLayout
buttonView.setValue(values[i + dataOffset]); buttonView.setValue(values[i + dataOffset]);
buttonView.setColor(color); buttonView.setColor(color);
buttonView.setThreshold(threshold); buttonView.setThreshold(threshold);
buttonView.setUnit(unit);
setupButtonControllers(timestamp, buttonView); setupButtonControllers(timestamp, buttonView);
timestamp -= day; timestamp -= day;
} }

@ -30,7 +30,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:button_count="8" app:button_count="8"
app:color="1" app:color="1"
app:threshold="10" app:threshold="10000"
app:unit="steps"
/> />
<org.isoron.uhabits.activities.habits.list.views.NumberPanelView <org.isoron.uhabits.activities.habits.list.views.NumberPanelView
@ -38,7 +39,22 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:button_count="8" app:button_count="8"
app:color="2" 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 <org.isoron.uhabits.activities.habits.list.views.NumberPanelView
@ -47,6 +63,7 @@
app:button_count="8" app:button_count="8"
app:color="6" app:color="6"
app:threshold="10" app:threshold="10"
app:unit="min"
/> />
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView <org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
@ -67,7 +84,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:button_count="8" app:button_count="8"
app:color="10" app:color="10"
app:threshold="5" app:threshold="750"
app:unit="words"
/> />
<org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView <org.isoron.uhabits.activities.habits.list.views.CheckmarkPanelView
@ -77,4 +95,13 @@
app:color="10" 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> </LinearLayout>

@ -19,7 +19,7 @@
<resources> <resources>
<dimen name="baseSize">20dp</dimen> <dimen name="baseSize">20dp</dimen>
<dimen name="checkmarkWidth">42dp</dimen> <dimen name="checkmarkWidth">48dp</dimen>
<dimen name="checkmarkHeight">48dp</dimen> <dimen name="checkmarkHeight">48dp</dimen>
<dimen name="history_editor_max_height">450dp</dimen> <dimen name="history_editor_max_height">450dp</dimen>
<dimen name="history_editor_padding">8dp</dimen> <dimen name="history_editor_padding">8dp</dimen>

Loading…
Cancel
Save