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.StaticLayout;
import android.text.TextPaint; import android.text.TextPaint;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import android.view.View;
import org.isoron.helpers.ColorHelper; import org.isoron.helpers.ColorHelper;
@ -134,15 +135,11 @@ public class CheckmarkView extends View
pCard.setColor(color); pCard.setColor(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
canvas.drawRoundRect(leftMargin, topMargin, width - leftMargin, height - topMargin, padding, canvas.drawRoundRect(leftMargin, topMargin, width - leftMargin, height - topMargin, padding,
padding, pCard); padding, pCard);
}
else else
{
canvas.drawRect(leftMargin, topMargin, width - leftMargin, height - topMargin, pCard); canvas.drawRect(leftMargin, topMargin, width - leftMargin, height - topMargin, pCard);
} }
}
private void drawCheckmark(Canvas canvas) private void drawCheckmark(Canvas canvas)
{ {
@ -177,29 +174,24 @@ public class CheckmarkView extends View
} }
@Override @Override
protected void onMeasure(int width, int height) protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{ {
super.onMeasure(width, height); int width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, height); setMeasuredDimension(width, (int) (width * 1.25));
} }
@Override @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.width = getMeasuredWidth();
this.height = height; this.height = getMeasuredHeight();
leftMargin = (int) (width * 0.015); leftMargin = (int) (width * 0.015);
topMargin = (int) (height * 0.015); topMargin = (int) (height * 0.015);
padding = 8 * leftMargin; padding = 8 * leftMargin;
textPaint.setTextSize(0.15f * width); textPaint.setTextSize(0.15f * width);
updateLabel();
} }
private void updateLabel() private void updateLabel()

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

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

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

@ -21,11 +21,12 @@ import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
@ -36,6 +37,8 @@ import org.isoron.uhabits.models.Habit;
public abstract class BaseWidgetProvider extends AppWidgetProvider public abstract class BaseWidgetProvider extends AppWidgetProvider
{ {
private int width, height;
protected abstract int getDefaultHeight(); protected abstract int getDefaultHeight();
protected abstract int getDefaultWidth(); protected abstract int getDefaultWidth();
@ -44,8 +47,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
protected abstract int getLayoutId(); protected abstract int getLayoutId();
protected abstract View buildCustomView(Context context, int max_height, int max_width, protected abstract View buildCustomView(Context context, Habit habit);
Habit habit);
public static String getHabitIdKey(long widgetId) 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) private void updateWidget(Context context, AppWidgetManager manager, int widgetId, Bundle options)
{ {
int maxWidth = getDefaultWidth(); updateWidgetSize(context, options);
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));
}
Context appContext = context.getApplicationContext(); Context appContext = context.getApplicationContext();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), getLayoutId()); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), getLayoutId());
@ -104,7 +97,9 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
if(habitId < 0) return; if(habitId < 0) return;
Habit habit = Habit.get(habitId); 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.setDrawingCacheEnabled(true);
widgetView.buildDrawingCache(true); widgetView.buildDrawingCache(true);
Bitmap drawingCache = widgetView.getDrawingCache(); Bitmap drawingCache = widgetView.getDrawingCache();
@ -117,4 +112,48 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
manager.updateAppWidget(widgetId, remoteViews); 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 public class CheckmarkWidgetProvider extends BaseWidgetProvider
{ {
@Override @Override
protected View buildCustomView(Context context, int maxHeight, int maxWidth, Habit habit) protected View buildCustomView(Context context, Habit habit)
{ {
CheckmarkView widgetView = new CheckmarkView(context); CheckmarkView view = new CheckmarkView(context);
view.setHabit(habit);
widgetView.setHabit(habit); return view;
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;
} }
@Override @Override
@ -58,7 +49,7 @@ public class CheckmarkWidgetProvider extends BaseWidgetProvider
@Override @Override
protected int getDefaultWidth() protected int getDefaultWidth()
{ {
return 160; return 200;
} }
@Override @Override

@ -19,7 +19,6 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
import org.isoron.helpers.DialogHelper;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.views.HabitHistoryView; import org.isoron.uhabits.views.HabitHistoryView;
@ -27,20 +26,11 @@ import org.isoron.uhabits.views.HabitHistoryView;
public class HistoryWidgetProvider extends BaseWidgetProvider public class HistoryWidgetProvider extends BaseWidgetProvider
{ {
@Override @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); HabitHistoryView view = new HabitHistoryView(context, null);
view.setIsBackgroundTransparent(true);
view.setHabit(habit); view.setHabit(habit);
view.measure(maxWidth, maxHeight); view.setIsBackgroundTransparent(true);
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; return view;
} }

@ -19,7 +19,6 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
import org.isoron.helpers.DialogHelper;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.views.HabitScoreView; import org.isoron.uhabits.views.HabitScoreView;
@ -27,20 +26,11 @@ import org.isoron.uhabits.views.HabitScoreView;
public class ScoreWidgetProvider extends BaseWidgetProvider public class ScoreWidgetProvider extends BaseWidgetProvider
{ {
@Override @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); HabitScoreView view = new HabitScoreView(context, null);
view.setIsBackgroundTransparent(true); view.setIsBackgroundTransparent(true);
view.setHabit(habit); 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; return view;
} }

@ -19,7 +19,6 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.view.View; import android.view.View;
import org.isoron.helpers.DialogHelper;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit; import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.views.HabitStreakView; import org.isoron.uhabits.views.HabitStreakView;
@ -27,20 +26,11 @@ import org.isoron.uhabits.views.HabitStreakView;
public class StreakWidgetProvider extends BaseWidgetProvider public class StreakWidgetProvider extends BaseWidgetProvider
{ {
@Override @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); HabitStreakView view = new HabitStreakView(context, null);
view.setIsBackgroundTransparent(true); view.setIsBackgroundTransparent(true);
view.setHabit(habit); 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; return view;
} }

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

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

Loading…
Cancel
Save