Add ring to check mark widget

pull/84/merge
Alinson S. Xavier 10 years ago
parent aee5d975db
commit 980db1d171

@ -122,7 +122,7 @@ public class ListHabitsFragment extends Fragment
loader.setCheckmarkCount(helper.getButtonCount()); loader.setCheckmarkCount(helper.getButtonCount());
llHint.setOnClickListener(this); llHint.setOnClickListener(this);
tvStarEmpty.setTypeface(UIHelper.getFontAwesome()); tvStarEmpty.setTypeface(UIHelper.getFontAwesome(activity));
adapter = new HabitListAdapter(getActivity(), loader); adapter = new HabitListAdapter(getActivity(), loader);
adapter.setSelectedPositions(selectedPositions); adapter.setSelectedPositions(selectedPositions);

@ -29,19 +29,19 @@ public class ColorHelper
{ {
public static int CSV_PALETTE[] = public static int CSV_PALETTE[] =
{ {
Color.parseColor("#D32F2F"), // red Color.parseColor("#D32F2F"), // 0 red
Color.parseColor("#E64A19"), // orange Color.parseColor("#E64A19"), // 1 orange
Color.parseColor("#F9A825"), // yellow Color.parseColor("#F9A825"), // 2 yellow
Color.parseColor("#AFB42B"), // light green Color.parseColor("#AFB42B"), // 3 light green
Color.parseColor("#388E3C"), // dark green Color.parseColor("#388E3C"), // 4 dark green
Color.parseColor("#00897B"), // teal Color.parseColor("#00897B"), // 5 teal
Color.parseColor("#00ACC1"), // cyan Color.parseColor("#00ACC1"), // 6 cyan
Color.parseColor("#039BE5"), // blue Color.parseColor("#039BE5"), // 7 blue
Color.parseColor("#5E35B1"), // deep purple Color.parseColor("#5E35B1"), // 8 deep purple
Color.parseColor("#8E24AA"), // purple Color.parseColor("#8E24AA"), // 9 purple
Color.parseColor("#D81B60"), // pink Color.parseColor("#D81B60"), // 10 pink
Color.parseColor("#303030"), // dark grey Color.parseColor("#303030"), // 11 dark grey
Color.parseColor("#aaaaaa") // light grey Color.parseColor("#aaaaaa") // 12 light grey
}; };
public static int colorToPaletteIndex(Context context, int color) public static int colorToPaletteIndex(Context context, int color)

@ -198,7 +198,7 @@ public class ListHabitsHelper
{ {
View check = inflater.inflate(R.layout.list_habits_item_check, null); View check = inflater.inflate(R.layout.list_habits_item_check, null);
TextView btCheck = (TextView) check.findViewById(R.id.tvCheck); TextView btCheck = (TextView) check.findViewById(R.id.tvCheck);
btCheck.setTypeface(UIHelper.getFontAwesome()); btCheck.setTypeface(UIHelper.getFontAwesome(context));
btCheck.setOnLongClickListener(onLongClickListener); btCheck.setOnLongClickListener(onLongClickListener);
btCheck.setOnClickListener(onClickListener); btCheck.setOnClickListener(onClickListener);
btCheck.setHapticFeedbackEnabled(false); btCheck.setHapticFeedbackEnabled(false);

@ -57,15 +57,10 @@ public abstract class UIHelper
void onSaved(Command command, Object savedObject); void onSaved(Command command, Object savedObject);
} }
public static Typeface getFontAwesome() public static Typeface getFontAwesome(Context context)
{ {
if(fontAwesome == null) if(fontAwesome == null)
{
Context context = HabitsApplication.getContext();
if(context == null) throw new RuntimeException("Could not find application context");
fontAwesome = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf"); fontAwesome = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
}
return fontAwesome; return fontAwesome;
} }
@ -107,6 +102,14 @@ public abstract class UIHelper
else return defaultValue; else return defaultValue;
} }
public static Integer getColorAttribute(Context context, AttributeSet attrs, String name,
Integer defaultValue)
{
int resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0);
if (resId != 0) return context.getResources().getColor(resId);
else return defaultValue;
}
public static int getIntAttribute(Context context, AttributeSet attrs, String name, public static int getIntAttribute(Context context, AttributeSet attrs, String name,
int defaultValue) int defaultValue)
{ {

@ -20,184 +20,180 @@
package org.isoron.uhabits.views; package org.isoron.uhabits.views;
import android.content.Context; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Rect; import android.graphics.drawable.InsetDrawable;
import android.graphics.Typeface; import android.graphics.drawable.ShapeDrawable;
import android.os.Build; import android.graphics.drawable.shapes.RoundRectShape;
import android.text.Layout; import android.support.annotation.NonNull;
import android.text.StaticLayout; import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.helpers.ColorHelper; import org.isoron.uhabits.helpers.ColorHelper;
import org.isoron.uhabits.helpers.UIHelper;
import org.isoron.uhabits.models.Checkmark;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.models.Score;
public class CheckmarkView extends View implements HabitDataView import java.util.Arrays;
public class CheckmarkView extends FrameLayout implements HabitDataView
{ {
private Paint pCard; @Nullable
private Paint pIcon; private InsetDrawable background;
private int primaryColor; @Nullable
private int timesColor; private Paint backgroundPaint;
private int darkGrey;
private int width; @Nullable
private int height; private Habit habit;
private float leftMargin;
private float topMargin;
private float padding;
private String label;
private String fa_check; private int activeColor;
private String fa_times; private float percentage;
private int check_status; @Nullable
private String name;
private Rect rect; @Nullable
private TextPaint textPaint; private RingView ring;
private StaticLayout labelLayout; private ViewGroup frame;
private Habit habit; private TextView label;
private int checkmarkValue;
private int inactiveColor;
public CheckmarkView(Context context) public CheckmarkView(Context context)
{ {
super(context); super(context);
init(context); init();
} }
public CheckmarkView(Context context, AttributeSet attrs) public CheckmarkView(Context context, AttributeSet attrs)
{ {
super(context, attrs); super(context, attrs);
init(context); init();
} }
private void init(Context context) private void init()
{ {
Typeface fontawesome = inflate(getContext(), R.layout.widget_checkmark_inner, this);
Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
pCard = new Paint(); int shadowRadius = (int) UIHelper.dpToPixels(getContext(), 2);
pCard.setAntiAlias(true); int shadowOffset = (int) UIHelper.dpToPixels(getContext(), 1);
int shadowColor = Color.argb(96, 0, 0, 0);
pIcon = new Paint(); float cornerRadius = UIHelper.dpToPixels(getContext(), 5);
pIcon.setAntiAlias(true); float[] radii = new float[8];
pIcon.setTypeface(fontawesome); Arrays.fill(radii, cornerRadius);
pIcon.setTextAlign(Paint.Align.CENTER);
textPaint = new TextPaint(); RoundRectShape shape = new RoundRectShape(radii, null, null);
textPaint.setColor(Color.WHITE); ShapeDrawable innerDrawable = new ShapeDrawable(shape);
textPaint.setAntiAlias(true);
fa_check = context.getString(R.string.fa_check); int insetLeftTop = Math.max(shadowRadius - shadowOffset, 0);
fa_times = context.getString(R.string.fa_times); int insetRightBottom = shadowRadius + shadowOffset;
primaryColor = ColorHelper.getColor(getContext(), 10); background = new InsetDrawable(innerDrawable, insetLeftTop, insetLeftTop, insetRightBottom,
timesColor = Color.argb(128, 255, 255, 255); insetRightBottom);
darkGrey = Color.argb(64, 0, 0, 0); backgroundPaint = innerDrawable.getPaint();
backgroundPaint.setAlpha(100);
rect = new Rect(); if (backgroundPaint != null)
check_status = 0; backgroundPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
label = "Habit";
} ring = (RingView) findViewById(R.id.scoreRing);
frame = (ViewGroup) findViewById(R.id.frame);
label = (TextView) findViewById(R.id.label);
inactiveColor = ColorHelper.CSV_PALETTE[11];
public void setHabit(Habit habit) if(isInEditMode())
{ {
this.habit = habit; percentage = 0.75f;
name = "Wake up early";
activeColor = ColorHelper.CSV_PALETTE[6];
checkmarkValue = Checkmark.CHECKED_EXPLICITLY;
refresh();
}
} }
@Override @Override
protected void onDraw(Canvas canvas) public void setHabit(@NonNull Habit habit)
{ {
super.onDraw(canvas); this.habit = habit;
this.activeColor = ColorHelper.getColor(getContext(), habit.color);
drawBackground(canvas); refresh();
drawCheckmark(canvas); postInvalidate();
drawLabel(canvas);
} }
private void drawBackground(Canvas canvas) public void refresh()
{ {
int color = (check_status == 2 ? primaryColor : darkGrey); if (backgroundPaint == null || frame == null || ring == null) return;
pCard.setColor(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
canvas.drawRoundRect(leftMargin, topMargin, width - leftMargin, height - topMargin, padding,
padding, pCard);
else
canvas.drawRect(leftMargin, topMargin, width - leftMargin, height - topMargin, pCard);
}
private void drawCheckmark(Canvas canvas) String text;
int color;
switch (checkmarkValue)
{ {
String text = (check_status == 0 ? fa_times : fa_check); case Checkmark.CHECKED_EXPLICITLY:
int color = (check_status == 2 ? Color.WHITE : timesColor); text = getResources().getString(R.string.fa_check);
color = activeColor;
pIcon.setColor(color); break;
pIcon.setTextSize(width * 0.5f);
pIcon.getTextBounds(text, 0, 1, rect); case Checkmark.CHECKED_IMPLICITLY:
text = getResources().getString(R.string.fa_check);
int y = (int) ((0.67f * height - rect.bottom - rect.top) / 2); color = inactiveColor;
canvas.drawText(text, width / 2, y, pIcon); break;
case Checkmark.UNCHECKED:
default:
text = getResources().getString(R.string.fa_times);
color = inactiveColor;
break;
} }
private void drawLabel(Canvas canvas) backgroundPaint.setColor(color);
{ frame.setBackgroundDrawable(background);
canvas.save();
float y;
int nLines = labelLayout.getLineCount();
if(nLines == 1) ring.setPercentage(percentage);
y = height * 0.8f - padding; ring.setPrecision(0.125f);
else ring.setColor(Color.WHITE);
y = height * 0.7f - padding; ring.setBackgroundColor(color);
ring.setText(text);
canvas.translate(leftMargin + padding, y); label.setText(name);
labelLayout.draw(canvas); postInvalidate();
canvas.restore();
} }
@Override @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{ {
int width = MeasureSpec.getSize(widthMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, (int) (width * 1.25)); int height = MeasureSpec.getSize(heightMeasureSpec);
}
@Override float w = width;
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) float h = width * 1.25f;
{ float scale = Math.min(width / w, height / h);
this.width = getMeasuredWidth();
this.height = getMeasuredHeight();
leftMargin = (width * 0.015f); w *= scale;
topMargin = (height * 0.015f); h *= scale;
padding = 8 * leftMargin;
textPaint.setTextSize(0.15f * width);
updateLabel(); widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) w, MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) h, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} }
@Override
public void refreshData() public void refreshData()
{ {
this.check_status = habit.checkmarks.getTodayValue(); if(habit == null) return;
int color = ColorHelper.getColor(getContext(), habit.color); this.name = habit.name;
this.primaryColor = Color.argb(230, Color.red(color), Color.green(color), this.percentage = (float) habit.scores.getTodayValue() / Score.MAX_VALUE;
Color.blue(color)); this.checkmarkValue = habit.checkmarks.getTodayValue();
this.label = habit.name;
updateLabel(); refresh();
postInvalidate(); postInvalidate();
} }
private void updateLabel()
{
textPaint.setColor(Color.WHITE);
labelLayout = new StaticLayout(label, textPaint,
(int) (width - 2 * leftMargin - 2 * padding),
Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
}
} }

@ -48,8 +48,8 @@ public class RingView extends View
private float thickness; private float thickness;
private int backgroundColor; private Integer backgroundColor;
private int inactiveColor; private Integer inactiveColor;
private float em; private float em;
private String text; private String text;
@ -66,6 +66,10 @@ public class RingView extends View
percentage = UIHelper.getFloatAttribute(context, attrs, "percentage", 0); percentage = UIHelper.getFloatAttribute(context, attrs, "percentage", 0);
precision = UIHelper.getFloatAttribute(context, attrs, "precision", 0.01f); precision = UIHelper.getFloatAttribute(context, attrs, "precision", 0.01f);
color = UIHelper.getColorAttribute(context, attrs, "color", 0);
backgroundColor = UIHelper.getColorAttribute(context, attrs, "backgroundColor", null);
inactiveColor = UIHelper.getColorAttribute(context, attrs, "inactiveColor", null);
thickness = UIHelper.getFloatAttribute(context, attrs, "thickness", 0); thickness = UIHelper.getFloatAttribute(context, attrs, "thickness", 0);
thickness = UIHelper.dpToPixels(context, thickness); thickness = UIHelper.dpToPixels(context, thickness);
@ -86,6 +90,13 @@ public class RingView extends View
postInvalidate(); postInvalidate();
} }
@Override
public void setBackgroundColor(int backgroundColor)
{
this.backgroundColor = backgroundColor;
postInvalidate();
}
public void setPercentage(float percentage) public void setPercentage(float percentage)
{ {
this.percentage = percentage; this.percentage = percentage;
@ -117,10 +128,12 @@ public class RingView extends View
pRing.setColor(color); pRing.setColor(color);
pRing.setTextAlign(Paint.Align.CENTER); pRing.setTextAlign(Paint.Align.CENTER);
if(backgroundColor == null)
backgroundColor = UIHelper.getStyledColor(getContext(), R.attr.cardBackgroundColor); backgroundColor = UIHelper.getStyledColor(getContext(), R.attr.cardBackgroundColor);
color = ColorHelper.CSV_PALETTE[6]; if(inactiveColor == null)
inactiveColor = UIHelper.getStyledColor(getContext(), R.attr.highContrastTextColor); inactiveColor = UIHelper.getStyledColor(getContext(), R.attr.highContrastTextColor);
inactiveColor = ColorHelper.setAlpha(inactiveColor, 0.1f); inactiveColor = ColorHelper.setAlpha(inactiveColor, 0.1f);
rect = new RectF(); rect = new RectF();
@ -161,12 +174,12 @@ public class RingView extends View
{ {
pRing.setColor(backgroundColor); pRing.setColor(backgroundColor);
rect.inset(thickness, thickness); rect.inset(thickness, thickness);
canvas.drawArc(rect, -90, 360, true, pRing); canvas.drawArc(rect, 0, 360, true, pRing);
pRing.setColor(color); pRing.setColor(color);
pRing.setTextSize(textSize); pRing.setTextSize(textSize);
if(enableFontAwesome) pRing.setTypeface(UIHelper.getFontAwesome()); if(enableFontAwesome) pRing.setTypeface(UIHelper.getFontAwesome(getContext()));
canvas.drawText(text, rect.centerX(), rect.centerY() + 0.5f * em, pRing); canvas.drawText(text, rect.centerX(), rect.centerY() + 0.4f * em, pRing);
} }
} }
} }

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
~
~ This file is part of Loop Habit Tracker.
~
~ Loop Habit Tracker is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by the
~ Free Software Foundation, either version 3 of the License, or (at your
~ option) any later version.
~
~ Loop Habit Tracker is distributed in the hope that it will be useful, but
~ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
~ more details.
~
~ You should have received a copy of the GNU General Public License along
~ with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<LinearLayout
android:id="@+id/frame"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:habit="http://isoron.org/android"
android:gravity="center"
android:orientation="vertical">
<org.isoron.uhabits.views.RingView
android:id="@+id/scoreRing"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
habit:percentage="0.25"
habit:thickness="2"
habit:textSize="16"
habit:color="@color/white"
habit:inactiveColor="@color/white"
habit:enableFontAwesome="true"
habit:text="@string/fa_check"
android:layout_marginTop="8dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"/>
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textSize="12sp"
android:textColor="@color/white"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:gravity="center"
android:scrollHorizontally="true"
android:ellipsize="end"
android:maxLines="2"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:fontFamily="sans-serif-condensed"/>
</LinearLayout>
Loading…
Cancel
Save