diff options
Diffstat (limited to 'src/com/android/gallery3d/filtershow/category/CategoryView.java')
-rw-r--r-- | src/com/android/gallery3d/filtershow/category/CategoryView.java | 83 |
1 files changed, 60 insertions, 23 deletions
diff --git a/src/com/android/gallery3d/filtershow/category/CategoryView.java b/src/com/android/gallery3d/filtershow/category/CategoryView.java index e045cf452..c456dc207 100644 --- a/src/com/android/gallery3d/filtershow/category/CategoryView.java +++ b/src/com/android/gallery3d/filtershow/category/CategoryView.java @@ -24,7 +24,6 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Typeface; -import android.util.Log; import android.view.View; import com.android.gallery3d.R; import com.android.gallery3d.filtershow.FilterShowActivity; @@ -39,8 +38,8 @@ public class CategoryView extends View implements View.OnClickListener { private Paint mPaint = new Paint(); private Action mAction; private Rect mTextBounds = new Rect(); - private static int sMargin = 16; - private static int sTextSize = 32; + private int mMargin = 16; + private int mTextSize = 32; private int mTextColor; private int mBackgroundColor; private Paint mSelectPaint; @@ -50,14 +49,6 @@ public class CategoryView extends View implements View.OnClickListener { private int mBorderStroke; private int mOrientation = VERTICAL; - public static void setTextSize(int size) { - sTextSize = size; - } - - public static void setMargin(int margin) { - sMargin = margin; - } - public CategoryView(Context context) { super(context); setOnClickListener(this); @@ -65,6 +56,8 @@ public class CategoryView extends View implements View.OnClickListener { mBackgroundColor = res.getColor(R.color.filtershow_categoryview_background); mTextColor = res.getColor(R.color.filtershow_categoryview_text); mSelectionStroke = res.getDimensionPixelSize(R.dimen.thumbnail_margin); + mTextSize = res.getDimensionPixelSize(R.dimen.category_panel_text_size); + mMargin = res.getDimensionPixelOffset(R.dimen.category_panel_margin); mSelectPaint = new Paint(); mSelectPaint.setStyle(Paint.Style.FILL); mSelectPaint.setColor(res.getColor(R.color.filtershow_category_selection)); @@ -73,36 +66,80 @@ public class CategoryView extends View implements View.OnClickListener { mBorderStroke = mSelectionStroke / 3; } + private void computeTextPosition(String text) { + if (text == null) { + return; + } + mPaint.setTextSize(mTextSize); + if (mOrientation == VERTICAL) { + text = text.toUpperCase(); + // TODO: set this in xml + mPaint.setTypeface(Typeface.DEFAULT_BOLD); + } + mPaint.getTextBounds(text, 0, text.length(), mTextBounds); + } + public void drawText(Canvas canvas, String text) { if (text == null) { return; } - text = text.toUpperCase(); - mPaint.setTextSize(sTextSize); - mPaint.setTypeface(Typeface.DEFAULT_BOLD); float textWidth = mPaint.measureText(text); - mPaint.getTextBounds(text, 0, text.length(), mTextBounds); - int x = (int) (canvas.getWidth() - textWidth - sMargin); - int y = canvas.getHeight() - sMargin; + int x = (int) (canvas.getWidth() - textWidth - mMargin); + if (mOrientation == HORIZONTAL) { + x = (int) ((canvas.getWidth() - textWidth) / 2.0f); + } + if (x < 0) { + // If the text takes more than the view width, + // justify to the left. + x = mMargin; + } + int y = canvas.getHeight() - mMargin; canvas.drawText(text, x, y, mPaint); } @Override + public CharSequence getContentDescription () { + if (mAction != null) { + return mAction.getName(); + } + return null; + } + + @Override public void onDraw(Canvas canvas) { canvas.drawColor(mBackgroundColor); if (mAction != null) { mPaint.reset(); mPaint.setAntiAlias(true); + computeTextPosition(mAction.getName()); if (mAction.getImage() == null) { - mAction.setImageFrame(new Rect(0, 0, canvas.getWidth(), canvas.getHeight())); + mAction.setImageFrame(new Rect(0, 0, getWidth(), getHeight()), mOrientation); } else { Bitmap bitmap = mAction.getImage(); - canvas.drawBitmap(bitmap, 0, 0, mPaint); + canvas.save(); + Rect clipRect = new Rect(mSelectionStroke, mSelectionStroke, + getWidth() - mSelectionStroke, + getHeight() - 2* mMargin - mTextSize); + int offsetx = 0; + int offsety = 0; + if (mOrientation == HORIZONTAL) { + canvas.clipRect(clipRect); + offsetx = - (bitmap.getWidth() - clipRect.width()) / 2; + offsety = - (bitmap.getHeight() - clipRect.height()) / 2; + } + canvas.drawBitmap(bitmap, offsetx, offsety, mPaint); + canvas.restore(); if (mAdapter.isSelected(this)) { - SelectionRenderer.drawSelection(canvas, 0, 0, - Math.min(bitmap.getWidth(), getWidth()), - Math.min(bitmap.getHeight(), getHeight()), - mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint); + if (mOrientation == HORIZONTAL) { + SelectionRenderer.drawSelection(canvas, 0, 0, + getWidth(), getHeight() - mMargin - mTextSize, + mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint); + } else { + SelectionRenderer.drawSelection(canvas, 0, 0, + Math.min(bitmap.getWidth(), getWidth()), + Math.min(bitmap.getHeight(), getHeight()), + mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint); + } } } mPaint.setColor(mBackgroundColor); |