From 2ce97880ca19ee79f7bafec4c44fb1aa9dce7c9c 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/colors.xml | 3 + .../android/launcher3/DynamicGridSizeFragment.java | 225 +++++++++++---------- 7 files changed, 124 insertions(+), 108 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 38db4b05c..5109e73d8 100644 --- a/res/layout/dynamic_grid_size_screen.xml +++ b/res/layout/dynamic_grid_size_screen.xml @@ -34,12 +34,12 @@ android:textSize="16sp" /> - #FFb2b0ab #26000000 #50000000 + + #FFFFFFFF + #FF000000 diff --git a/src/com/android/launcher3/DynamicGridSizeFragment.java b/src/com/android/launcher3/DynamicGridSizeFragment.java index 490335ddf..9ab1ad0bd 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; @@ -8,12 +25,11 @@ import android.content.Context; import android.content.DialogInterface; 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.LayoutInflater; import android.view.View; @@ -25,13 +41,18 @@ 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; @@ -43,11 +64,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); @@ -60,16 +79,15 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On ((GridSizeArrayAdapter) 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() { @@ -83,11 +101,11 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On SettingsProvider.getIntCustomDefault(getActivity(), SettingsProvider.SETTINGS_UI_DYNAMIC_GRID_SIZE, 0)); - setCurrentImage(); + updateGridMetrics(); mListView = (ListView) v.findViewById(R.id.dynamic_grid_list); Resources res = getResources(); - String [] values = { + String[] values = { res.getString(R.string.grid_size_comfortable), res.getString(R.string.grid_size_cozy), res.getString(R.string.grid_size_condensed), @@ -99,64 +117,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) { @@ -167,21 +137,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; @@ -208,23 +171,23 @@ 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; } + if (mCustomGridColumns == 0) { + mCustomGridColumns = (int) grid.numColumns; + } nPRows.setMinValue(Math.max(MIN_DYNAMIC_GRID_ROWS, rows - DeviceProfile.GRID_SIZE_MIN)); nPRows.setMaxValue(rows + DeviceProfile.GRID_SIZE_MAX); @@ -233,15 +196,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) { @@ -249,6 +213,7 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On } } }); + mDialog.setOnDismissListener(this); mDialog.show(); } @@ -278,16 +243,14 @@ 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 { Context mContext; String[] mTitles; - public GridSizeArrayAdapter(Context context, int textViewResourceId, - String[] objects) { + public GridSizeArrayAdapter(Context context, int textViewResourceId, String[] objects) { super(context, textViewResourceId, objects); mContext = context; @@ -296,27 +259,29 @@ public class DynamicGridSizeFragment extends Fragment implements NumberPicker.On @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); + 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); textView.setText(mTitles[position]); - // Set Selected State + + // Set selected state if (position == mCurrentSize.getValue()) { + if (mCurrentSelection != null) { + setCleared(mCurrentSelection); + } mCurrentSelection = convertView; setSelected(mCurrentSelection); } if (position == DeviceProfile.GridSize.Custom.getValue()) { - LauncherAppState app = LauncherAppState.getInstance(); - DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); - 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); + SettingsProvider.SETTINGS_UI_HOMESCREEN_COLUMNS, getGrid().numColumnsBase); String gridSize = rows + " " + "\u00d7" + " " + columns; textView.setText(getString(R.string.grid_size_custom_and_size, gridSize)); @@ -327,4 +292,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