Improve widget measuring

pull/30/head
Alinson S. Xavier 10 years ago
parent 5428209543
commit 162ded66d8

@ -27,6 +27,7 @@ import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import org.isoron.helpers.ColorHelper;
@ -134,14 +135,10 @@ public class CheckmarkView extends View
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)
@ -177,29 +174,24 @@ public class CheckmarkView extends View
}
@Override
protected void onMeasure(int width, int height)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(width, height);
setMeasuredDimension(width, height);
int width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, (int) (width * 1.25));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
{
super.onSizeChanged(w, h, oldw, oldh);
updateSize(w, h);
updateLabel();
}
private void updateSize(int width, int height)
{
this.width = width;
this.height = height;
this.width = getMeasuredWidth();
this.height = getMeasuredHeight();
leftMargin = (int) (width * 0.015);
topMargin = (int) (height * 0.015);
padding = 8 * leftMargin;
textPaint.setTextSize(0.15f * width);
updateLabel();
}
private void updateLabel()

@ -23,6 +23,7 @@ import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import org.isoron.helpers.ColorHelper;
import org.isoron.helpers.DateHelper;
@ -107,17 +108,14 @@ public class HabitHistoryView extends ScrollableDataView
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int b = height / 8;
height = b * 8;
width = (width / b) * b;
setMeasuredDimension(width, height);
}
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
{
if(height < 8) height = 200;
baseSize = height / 8;
setScrollerBucketSize(baseSize);

@ -137,20 +137,14 @@ public class HabitScoreView extends ScrollableDataView
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if(height > 0)
{
int b = height / 9;
height = b * 9;
width = (width / b) * b;
}
setMeasuredDimension(width, height);
}
@Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
{
if(height < 9) height = 200;
baseSize = height / 9;
setScrollerBucketSize(baseSize);

@ -88,11 +88,6 @@ public class HabitStreakView extends ScrollableDataView
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int b = height / 10;
height = b * 10;
width = (width / b) * b;
setMeasuredDimension(width, height);
}

@ -21,11 +21,12 @@ import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RemoteViews;
@ -36,6 +37,8 @@ import org.isoron.uhabits.models.Habit;
public abstract class BaseWidgetProvider extends AppWidgetProvider
{
private int width, height;
protected abstract int getDefaultHeight();
protected abstract int getDefaultWidth();
@ -44,8 +47,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
protected abstract int getLayoutId();
protected abstract View buildCustomView(Context context, int max_height, int max_width,
Habit habit);
protected abstract View buildCustomView(Context context, Habit habit);
public static String getHabitIdKey(long widgetId)
{
@ -85,16 +87,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
private void updateWidget(Context context, AppWidgetManager manager, int widgetId, Bundle options)
{
int maxWidth = getDefaultWidth();
int maxHeight = getDefaultHeight();
if (options != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
maxWidth = (int) DialogHelper.dpToPixels(context,
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH));
maxHeight = (int) DialogHelper.dpToPixels(context,
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT));
}
updateWidgetSize(context, options);
Context appContext = context.getApplicationContext();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
@ -104,7 +97,9 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
if(habitId < 0) return;
Habit habit = Habit.get(habitId);
View widgetView = buildCustomView(context, maxHeight, maxWidth, habit);
View widgetView = buildCustomView(context, habit);
measureCustomView(context, width, height, widgetView);
widgetView.setDrawingCacheEnabled(true);
widgetView.buildDrawingCache(true);
Bitmap drawingCache = widgetView.getDrawingCache();
@ -117,4 +112,48 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
manager.updateAppWidget(widgetId, remoteViews);
}
private void updateWidgetSize(Context context, Bundle options)
{
int maxWidth = getDefaultWidth();
int minWidth = getDefaultWidth();
int maxHeight = getDefaultHeight();
int minHeight = getDefaultHeight();
if (options != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
maxWidth = (int) DialogHelper.dpToPixels(context,
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH));
maxHeight = (int) DialogHelper.dpToPixels(context,
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT));
minWidth = (int) DialogHelper.dpToPixels(context,
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH));
minHeight = (int) DialogHelper.dpToPixels(context,
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT));
}
width = maxWidth;
height = maxHeight;
}
private void measureCustomView(Context context, int w, int h, View customView)
{
LayoutInflater inflater = LayoutInflater.from(context);
View entireView = inflater.inflate(getLayoutId(), null);
int specWidth = View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY);
int specHeight = View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY);
entireView.measure(specWidth, specHeight);
entireView.layout(0, 0, entireView.getMeasuredWidth(), entireView.getMeasuredHeight());
View imageView = entireView.findViewById(R.id.imageView);
w = imageView.getMeasuredWidth();
h = imageView.getMeasuredHeight();
specWidth = View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY);
specHeight = View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY);
customView.measure(specWidth, specHeight);
customView.layout(0, 0, customView.getMeasuredWidth(), customView.getMeasuredHeight());
}
}

@ -27,20 +27,11 @@ import org.isoron.uhabits.views.CheckmarkView;
public class CheckmarkWidgetProvider extends BaseWidgetProvider
{
@Override
protected View buildCustomView(Context context, int maxHeight, int maxWidth, Habit habit)
protected View buildCustomView(Context context, Habit habit)
{
CheckmarkView widgetView = new CheckmarkView(context);
widgetView.setHabit(habit);
widgetView.measure(maxWidth, maxHeight);
widgetView.layout(0, 0, maxWidth, maxHeight);
int width = widgetView.getMeasuredWidth();
int height = widgetView.getMeasuredHeight();
widgetView.measure(width, height);
widgetView.layout(0, 0, width, height);
return widgetView;
CheckmarkView view = new CheckmarkView(context);
view.setHabit(habit);
return view;
}
@Override
@ -58,7 +49,7 @@ public class CheckmarkWidgetProvider extends BaseWidgetProvider
@Override
protected int getDefaultWidth()
{
return 160;
return 200;
}
@Override

@ -19,7 +19,6 @@ import android.app.PendingIntent;
import android.content.Context;
import android.view.View;
import org.isoron.helpers.DialogHelper;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.views.HabitHistoryView;
@ -27,20 +26,11 @@ import org.isoron.uhabits.views.HabitHistoryView;
public class HistoryWidgetProvider extends BaseWidgetProvider
{
@Override
protected View buildCustomView(Context context, int maxHeight, int maxWidth, Habit habit)
protected View buildCustomView(Context context, Habit habit)
{
HabitHistoryView view = new HabitHistoryView(context, null);
view.setIsBackgroundTransparent(true);
view.setHabit(habit);
view.measure(maxWidth, maxHeight);
view.layout(0, 0, maxWidth, maxHeight);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
height -= DialogHelper.dpToPixels(context, 12);
view.measure(width, height);
view.layout(0, 0, width, height);
view.setIsBackgroundTransparent(true);
return view;
}

@ -19,7 +19,6 @@ import android.app.PendingIntent;
import android.content.Context;
import android.view.View;
import org.isoron.helpers.DialogHelper;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.views.HabitScoreView;
@ -27,20 +26,11 @@ import org.isoron.uhabits.views.HabitScoreView;
public class ScoreWidgetProvider extends BaseWidgetProvider
{
@Override
protected View buildCustomView(Context context, int maxHeight, int maxWidth, Habit habit)
protected View buildCustomView(Context context, Habit habit)
{
HabitScoreView view = new HabitScoreView(context, null);
view.setIsBackgroundTransparent(true);
view.setHabit(habit);
view.measure(maxWidth, maxHeight);
view.layout(0, 0, maxWidth, maxHeight);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
height -= DialogHelper.dpToPixels(context, 12);
view.measure(width, height);
view.layout(0, 0, width, height);
return view;
}

@ -19,7 +19,6 @@ import android.app.PendingIntent;
import android.content.Context;
import android.view.View;
import org.isoron.helpers.DialogHelper;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.views.HabitStreakView;
@ -27,20 +26,11 @@ import org.isoron.uhabits.views.HabitStreakView;
public class StreakWidgetProvider extends BaseWidgetProvider
{
@Override
protected View buildCustomView(Context context, int maxHeight, int maxWidth, Habit habit)
protected View buildCustomView(Context context, Habit habit)
{
HabitStreakView view = new HabitStreakView(context, null);
view.setIsBackgroundTransparent(true);
view.setHabit(habit);
view.measure(maxWidth, maxHeight);
view.layout(0, 0, maxWidth, maxHeight);
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
height -= DialogHelper.dpToPixels(context, 12);
view.measure(width, height);
view.layout(0, 0, width, height);
return view;
}

@ -5,7 +5,7 @@
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="4dp">
android:padding="0dp">
<ImageView
android:id="@+id/imageView"

@ -7,8 +7,9 @@
android:gravity="center"
android:orientation="vertical"
android:paddingTop="4dp"
android:paddingLeft="10dp"
android:paddingRight="0dp">
android:paddingBottom="0dp"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<TextView
android:id="@+id/label"

Loading…
Cancel
Save