From 90ae307eedcb2f90d4cdcb0276018b6e0e92c304 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Wed, 2 Jul 2014 23:36:27 +0200 Subject: Trebuchet: Draw the dynamic grid image dynamically This commits makes it possible to draw the image which previews the selected grid dynamically. Change-Id: I7c029f06110afdf1fe62145a079bba76166bb690 --- res/drawable/grid.png | Bin 1971 -> 0 bytes res/drawable/grid_comfortable.png | Bin 1984 -> 0 bytes res/drawable/grid_condensed.png | Bin 2033 -> 0 bytes res/drawable/grid_cozy.png | Bin 2006 -> 0 bytes res/layout/dynamic_grid_size_screen.xml | 4 +- res/values/cm_strings.xml | 4 +- res/values/colors.xml | 3 + .../android/launcher3/DynamicGridSizeFragment.java | 271 +++++++++++---------- 8 files changed, 154 insertions(+), 128 deletions(-) delete mode 100644 res/drawable/grid.png delete mode 100644 res/drawable/grid_comfortable.png delete mode 100644 res/drawable/grid_condensed.png delete mode 100644 res/drawable/grid_cozy.png diff --git a/res/drawable/grid.png b/res/drawable/grid.png deleted file mode 100644 index 79fe901d1..000000000 Binary files a/res/drawable/grid.png and /dev/null differ diff --git a/res/drawable/grid_comfortable.png b/res/drawable/grid_comfortable.png deleted file mode 100644 index 34e3ef2ee..000000000 Binary files a/res/drawable/grid_comfortable.png and /dev/null differ diff --git a/res/drawable/grid_condensed.png b/res/drawable/grid_condensed.png deleted file mode 100644 index ebc9d4e54..000000000 Binary files a/res/drawable/grid_condensed.png and /dev/null differ diff --git a/res/drawable/grid_cozy.png b/res/drawable/grid_cozy.png deleted file mode 100644 index 929bcb541..000000000 Binary files a/res/drawable/grid_cozy.png and /dev/null differ diff --git a/res/layout/dynamic_grid_size_screen.xml b/res/layout/dynamic_grid_size_screen.xml index 984f24a0f..8b6ed95ca 100644 --- a/res/layout/dynamic_grid_size_screen.xml +++ b/res/layout/dynamic_grid_size_screen.xml @@ -35,12 +35,12 @@ android:textSize="16sp" /> - - + Trebuchet @@ -68,7 +68,7 @@ Comfortable Cozy Condensed - Custom + Custom (%1$d \u00d7 %2$d) Select custom size Number of rows Number of columns diff --git a/res/values/colors.xml b/res/values/colors.xml index fd50ffd73..948801f5b 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -46,4 +46,7 @@ #50000000 #F44336 + + #FFFFFFFF + #FF000000 diff --git a/src/com/android/launcher3/DynamicGridSizeFragment.java b/src/com/android/launcher3/DynamicGridSizeFragment.java index f8ada16f6..55d2bf0cf 100644 --- a/src/com/android/launcher3/DynamicGridSizeFragment.java +++ b/src/com/android/launcher3/DynamicGridSizeFragment.java @@ -1,6 +1,23 @@ +/* + * Copyright (C) 2014 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.android.launcher3; import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; import android.animation.ObjectAnimator; import android.app.Dialog; import android.app.Fragment; @@ -9,34 +26,38 @@ import android.content.DialogInterface; import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; -import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.NumberPicker; import android.widget.TextView; + import com.android.launcher3.settings.SettingsProvider; -public class DynamicGridSizeFragment extends Fragment implements NumberPicker.OnValueChangeListener, Dialog.OnDismissListener{ - public static final String DYNAMIC_GRID_SIZE_FRAGMENT = "dynamicGridSizeFragment"; +public class DynamicGridSizeFragment extends Fragment + implements NumberPicker.OnValueChangeListener, Dialog.OnDismissListener { + public static final String DYNAMIC_GRID_SIZE_FRAGMENT = "DynamicGridSizeFragment"; + public static final int MIN_DYNAMIC_GRID_ROWS = 2; public static final int MIN_DYNAMIC_GRID_COLUMNS = 3; - ImageView mDynamicGridImage; + + GridSizeView mDynamicGrid; + ListView mListView; View mCurrentSelection; - GridSizeArrayAdapter mAdapter; + GridSizeAdapter mAdapter; DeviceProfile.GridSize mCurrentSize; Dialog mDialog; @@ -45,11 +66,9 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On int mCustomGridColumns = 0; View.OnClickListener mSettingsItemListener = new View.OnClickListener() { - @Override public void onClick(View v) { - mCurrentSize = DeviceProfile.GridSize - .getModeForValue((Integer) v.getTag()); + mCurrentSize = DeviceProfile.GridSize.getModeForValue((Integer) v.getTag()); setCleared(mCurrentSelection); setSelected(v); @@ -59,19 +78,18 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On showNumberPicker(); } - ((GridSizeArrayAdapter) mListView.getAdapter()).notifyDataSetChanged(); + ((GridSizeAdapter) mListView.getAdapter()).notifyDataSetChanged(); mAdapter.notifyDataSetInvalidated(); - setCurrentImage(); + updateGridMetrics(); } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + Bundle savedInstanceState) { View v = inflater.inflate(R.layout.dynamic_grid_size_screen, container, false); - mDynamicGridImage = (ImageView) v.findViewById(R.id.dynamic_grid_size_image); - mDynamicGridImage.setBackground(getResources().getDrawable(R.drawable.grid)); + mDynamicGrid = (GridSizeView) v.findViewById(R.id.dynamic_grid_size_image); LinearLayout titleLayout = (LinearLayout) v.findViewById(R.id.dynamic_grid_title); titleLayout.setOnClickListener(new View.OnClickListener() { @@ -85,17 +103,21 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On SettingsProvider.getIntCustomDefault(getActivity(), SettingsProvider.SETTINGS_UI_DYNAMIC_GRID_SIZE, 0)); - setCurrentImage(); + DeviceProfile grid = getGrid(); + mCustomGridRows = (int) grid.numRows; + mCustomGridColumns = (int) grid.numColumns; + + updateGridMetrics(); mListView = (ListView) v.findViewById(R.id.dynamic_grid_list); Resources res = getResources(); - String [] values = { - res.getString(R.string.grid_size_comfortable), - res.getString(R.string.grid_size_cozy), - res.getString(R.string.grid_size_condensed), - res.getString(R.string.grid_size_custom)}; - mAdapter = new GridSizeArrayAdapter(getActivity(), - R.layout.settings_pane_list_item, values); + int[] valueResIds = { + R.string.grid_size_comfortable, + R.string.grid_size_cozy, + R.string.grid_size_condensed, + R.string.grid_size_custom + }; + mAdapter = new GridSizeAdapter(getActivity(), valueResIds); mListView.setAdapter(mAdapter); // RTL @@ -108,64 +130,16 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On return v; } - private void setCurrentImage() { - Drawable d = null; - boolean custom = false; - - switch (mCurrentSize) { - case Comfortable: - d = getResources().getDrawable(R.drawable.grid_comfortable); - break; - case Cozy: - d = getResources().getDrawable(R.drawable.grid_cozy); - break; - case Condensed: - d = getResources().getDrawable(R.drawable.grid_condensed); - break; - default: - - custom = true; - break; - } - - if (d != null && !custom) { - mDynamicGridImage.setImageBitmap(null); - mDynamicGridImage.setBackground(d); - } else if (custom) { - mDynamicGridImage.setBackground(null); - mDynamicGridImage.setImageBitmap(writeOnDrawable(R.drawable.grid)); + private void updateGridMetrics() { + if (mCurrentSize == DeviceProfile.GridSize.Custom) { + mDynamicGrid.setMetrics(mCustomGridRows, mCustomGridColumns); + } else { + DeviceProfile grid = getGrid(); + mDynamicGrid.setMetrics(grid.numRowsBase + mCurrentSize.getValue(), + grid.numColumnsBase + mCurrentSize.getValue()); } } - public Bitmap writeOnDrawable(int drawableId){ - LauncherAppState app = LauncherAppState.getInstance(); - DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); - - int rows = mCustomGridRows == 0 ? (int) grid.numRows : mCustomGridRows; - int columns = mCustomGridColumns == 0 ? (int) grid.numColumns : mCustomGridColumns; - - String text = rows + " " + "\u00d7" + " " + columns; - - Bitmap bm = BitmapFactory.decodeResource(getResources(), - drawableId).copy(Bitmap.Config.ARGB_8888, true); - - Paint paint = new Paint(); - paint.setStyle(Paint.Style.FILL); - paint.setColor(Color.BLACK); - int px = getResources().getDimensionPixelOffset(R.dimen.grid_custom_text); - paint.setTextSize(px); - - Canvas canvas = new Canvas(bm); - - float canvasWidth = canvas.getWidth(); - float sentenceWidth = paint.measureText(text); - float startPositionX = (canvasWidth - sentenceWidth) / 2; - - canvas.drawText(text, startPositionX, bm.getHeight()/2, paint); - - return bm; - } - @Override public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) { if (enter) { @@ -182,21 +156,14 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On final View darkPanel = ((Launcher) getActivity()).getDarkPanel(); darkPanel.setVisibility(View.VISIBLE); - ObjectAnimator anim2 = ObjectAnimator.ofFloat( - darkPanel , "alpha", 0.0f, 0.3f); + ObjectAnimator anim2 = ObjectAnimator.ofFloat(darkPanel, "alpha", 0.0f, 0.3f); anim2.start(); - anim.addListener(new Animator.AnimatorListener() { - @Override - public void onAnimationStart(Animator arg0) {} + anim.addListener(new AnimatorListenerAdapter() { @Override - public void onAnimationRepeat(Animator arg0) {} - @Override - public void onAnimationEnd(Animator arg0) { + public void onAnimationEnd (Animator animation) { darkPanel.setVisibility(View.GONE); } - @Override - public void onAnimationCancel(Animator arg0) {} }); return anim; @@ -223,23 +190,16 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On private void showNumberPicker() { mDialog = new Dialog(getActivity()); - mDialog.setTitle(getResources().getString(R.string.preferences_interface_homescreen_custom)); + mDialog.setTitle(getResources().getString( + R.string.preferences_interface_homescreen_custom)); mDialog.setContentView(R.layout.custom_grid_size_dialog); - NumberPicker nPRows= (NumberPicker) mDialog.findViewById(R.id.custom_rows); + NumberPicker nPRows = (NumberPicker) mDialog.findViewById(R.id.custom_rows); NumberPicker nPColumns = (NumberPicker) mDialog.findViewById(R.id.custom_columns); - LauncherAppState app = LauncherAppState.getInstance(); - DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); - + DeviceProfile grid = getGrid(); int rows = grid.numRowsBase; int columns = grid.numColumnsBase; - if (mCustomGridColumns == 0) { - mCustomGridColumns = (int) grid.numColumns; - } - if (mCustomGridRows == 0) { - mCustomGridRows = (int) grid.numRows; - } nPRows.setMinValue(Math.max(MIN_DYNAMIC_GRID_ROWS, rows - DeviceProfile.GRID_SIZE_MIN)); nPRows.setMaxValue(rows + DeviceProfile.GRID_SIZE_MAX); @@ -248,15 +208,16 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On nPRows.setOnValueChangedListener(this); nPRows.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); - nPColumns.setMinValue(Math.max(MIN_DYNAMIC_GRID_COLUMNS, columns - DeviceProfile.GRID_SIZE_MIN)); + nPColumns.setMinValue(Math.max(MIN_DYNAMIC_GRID_COLUMNS, + columns - DeviceProfile.GRID_SIZE_MIN)); nPColumns.setMaxValue(columns + DeviceProfile.GRID_SIZE_MAX); nPColumns.setValue(mCustomGridColumns); nPColumns.setWrapSelectorWheel(false); nPColumns.setOnValueChangedListener(this); nPColumns.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); - Button b = (Button) mDialog.findViewById(R.id.dialog_confirm_button); - b.setOnClickListener(new View.OnClickListener() { + Button button = (Button) mDialog.findViewById(R.id.dialog_confirm_button); + button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mDialog != null) { @@ -264,6 +225,7 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On } } }); + mDialog.setOnDismissListener(this); mDialog.show(); } @@ -293,31 +255,42 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On SettingsProvider.SETTINGS_UI_HOMESCREEN_COLUMNS, mCustomGridColumns); mAdapter.notifyDataSetInvalidated(); - - setCurrentImage(); + mDynamicGrid.setMetrics(mCustomGridRows, mCustomGridColumns); } - private class GridSizeArrayAdapter extends ArrayAdapter { + private class GridSizeAdapter extends BaseAdapter { Context mContext; - String[] mTitles; - - public GridSizeArrayAdapter(Context context, int textViewResourceId, - String[] objects) { - super(context, textViewResourceId, objects); + int[] mTitleResIds; + public GridSizeAdapter(Context context, int[] resIds) { mContext = context; - mTitles = objects; + mTitleResIds = resIds; + } + + @Override + public int getCount() { + return mTitleResIds.length; + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public Object getItem(int position) { + return mContext.getString(mTitleResIds[position]); } @Override public View getView(int position, View convertView, ViewGroup parent) { - LayoutInflater inflater = (LayoutInflater) mContext - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = inflater.inflate(R.layout.settings_pane_list_item, - parent, false); - TextView textView = (TextView) convertView - .findViewById(R.id.item_name); - textView.setText(mTitles[position]); + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) + mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.settings_pane_list_item, parent, false); + } + + TextView textView = (TextView) convertView.findViewById(R.id.item_name); // RTL Configuration config = getResources().getConfiguration(); @@ -327,6 +300,9 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On // Set selected state if (position == mCurrentSize.getValue()) { + if (mCurrentSelection != null) { + setCleared(mCurrentSelection); + } mCurrentSelection = convertView; setSelected(mCurrentSelection); } @@ -335,14 +311,13 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On LauncherAppState app = LauncherAppState.getInstance(); DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); - String state = mTitles[position]; int rows = SettingsProvider.getIntCustomDefault(getActivity(), - SettingsProvider.SETTINGS_UI_HOMESCREEN_ROWS, grid.numRowsBase); + SettingsProvider.SETTINGS_UI_HOMESCREEN_ROWS, getGrid().numRowsBase); int columns = SettingsProvider.getIntCustomDefault(getActivity(), - SettingsProvider.SETTINGS_UI_HOMESCREEN_COLUMNS, grid.numColumnsBase); - state += " " + "(" + rows + " " + "\u00d7" + " " + columns + ")"; - - textView.setText(state); + SettingsProvider.SETTINGS_UI_HOMESCREEN_COLUMNS, getGrid().numColumnsBase); + textView.setText(mContext.getString(mTitleResIds[position], rows, columns)); + } else { + textView.setText(mTitleResIds[position]); } convertView.setOnClickListener(mSettingsItemListener); @@ -350,4 +325,52 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On return convertView; } } + + private DeviceProfile getGrid() { + LauncherAppState app = LauncherAppState.getInstance(); + return app.getDynamicGrid().getDeviceProfile(); + } + + private static class GridSizeView extends View { + private int mRows = 0, mColumns = 0; + private Paint mForegroundPaint; + private int mBackgroundColor; + + public GridSizeView(Context context, AttributeSet attrs) { + super(context, attrs); + Resources res = context.getResources(); + + mForegroundPaint = new Paint(); + mForegroundPaint.setColor(res.getColor(R.color.dynamic_grid_preview_foreground)); + mBackgroundColor = res.getColor(R.color.dynamic_grid_preview_background); + } + + public void setMetrics(int rows, int columns) { + mRows = rows; + mColumns = columns; + invalidate(); + } + + @Override + protected void onDraw(Canvas canvas) { + float width = getWidth() - getPaddingLeft() - getPaddingRight(); + float height = getHeight() - getPaddingTop() - getPaddingBottom(); + float xOffset = getPaddingLeft(); + float yOffset = getPaddingTop(); + + canvas.drawColor(mBackgroundColor); + + // Draw rows + for (int i = 1; i < mRows; i++) { + float yPos = yOffset + height / mRows * i; + canvas.drawLine(xOffset, yPos, xOffset + width, yPos, mForegroundPaint); + } + + // Draw columns + for (int j = 1; j < mColumns; j++) { + float xPos = xOffset + width / mColumns * j; + canvas.drawLine(xPos, yOffset, xPos, yOffset + height, mForegroundPaint); + } + } + } } -- cgit v1.2.3