1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.gallery3d.filtershow.category; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.graphics.Bitmap; 22 import android.graphics.BitmapFactory; 23 import android.graphics.Canvas; 24 import android.graphics.Color; 25 import android.graphics.Paint; 26 import android.graphics.Rect; 27 import android.view.MotionEvent; 28 import android.view.View; 29 import com.android.gallery3d.R; 30 import com.android.gallery3d.filtershow.FilterShowActivity; 31 import com.android.gallery3d.filtershow.ui.SelectionRenderer; 32 33 public class CategoryView extends IconView 34 implements View.OnClickListener, SwipableView{ 35 36 private static final String LOGTAG = "CategoryView"; 37 public static final int VERTICAL = 0; 38 public static final int HORIZONTAL = 1; 39 private Paint mPaint = new Paint(); 40 private Action mAction; 41 private Paint mSelectPaint; 42 CategoryAdapter mAdapter; 43 private int mSelectionStroke; 44 private Paint mBorderPaint; 45 private int mBorderStroke; 46 private float mStartTouchX = 0; 47 private float mStartTouchY = 0; 48 private float mDeleteSlope = 20; 49 private int mSelectionColor = Color.WHITE; 50 private int mSpacerColor = Color.WHITE; 51 private boolean mCanBeRemoved = false; 52 private long mDoubleActionLast = 0; 53 private long mDoubleTapDelay = 250; 54 55 public CategoryView(Context context) { 56 super(context); 57 setOnClickListener(this); 58 Resources res = getResources(); 59 mSelectionStroke = res.getDimensionPixelSize(R.dimen.thumbnail_margin); 60 mSelectPaint = new Paint(); 61 mSelectPaint.setStyle(Paint.Style.FILL); 62 mSelectionColor = res.getColor(R.color.filtershow_category_selection); 63 mSpacerColor = res.getColor(R.color.filtershow_categoryview_text); 64 65 mSelectPaint.setColor(mSelectionColor); 66 mBorderPaint = new Paint(mSelectPaint); 67 mBorderPaint.setColor(Color.BLACK); 68 mBorderStroke = mSelectionStroke / 3; 69 } 70 71 @Override 72 public boolean isHalfImage() { 73 if (mAction == null) { 74 return false; 75 } 76 if (mAction.getType() == Action.CROP_VIEW) { 77 return true; 78 } 79 if (mAction.getType() == Action.ADD_ACTION) { 80 return true; 81 } 82 return false; 83 } 84 85 private boolean canBeRemoved() { 86 return mCanBeRemoved; 87 } 88 89 private void drawSpacer(Canvas canvas) { 90 mPaint.reset(); 91 mPaint.setAntiAlias(true); 92 mPaint.setColor(mSpacerColor); 93 if (getOrientation() == CategoryView.VERTICAL) { 94 canvas.drawCircle(getWidth() / 2, getHeight() / 2, getHeight() / 5, mPaint); 95 } else { 96 canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, mPaint); 97 } 98 } 99 100 @Override 101 public boolean needsCenterText() { 102 if (mAction != null && mAction.getType() == Action.ADD_ACTION) { 103 return true; 104 } 105 return super.needsCenterText(); 106 } 107 108 public void onDraw(Canvas canvas) { 109 if (mAction != null) { 110 if (mAction.getType() == Action.SPACER) { 111 drawSpacer(canvas); 112 return; 113 } 114 if (mAction.isDoubleAction()) { 115 return; 116 } 117 mAction.setImageFrame(new Rect(0, 0, getWidth(), getHeight()), getOrientation()); 118 if (mAction.getImage() != null) { 119 setBitmap(mAction.getImage()); 120 } 121 } 122 super.onDraw(canvas); 123 if (mAdapter.isSelected(this)) { 124 SelectionRenderer.drawSelection(canvas, 0, 0, 125 getWidth(), getHeight(), 126 mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint); 127 } 128 } 129 130 public void setAction(Action action, CategoryAdapter adapter) { 131 mAction = action; 132 setText(mAction.getName()); 133 mAdapter = adapter; 134 mCanBeRemoved = action.canBeRemoved(); 135 setUseOnlyDrawable(false); 136 if (mAction.getType() == Action.ADD_ACTION) { 137 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.filtershow_add); 138 setBitmap(bitmap); 139 setUseOnlyDrawable(true); 140 setText(getResources().getString(R.string.filtershow_add_button_looks)); 141 } else { 142 setBitmap(mAction.getImage()); 143 } 144 invalidate(); 145 } 146 147 @Override 148 public void onClick(View view) { 149 FilterShowActivity activity = (FilterShowActivity) getContext(); 150 if (mAction.getType() == Action.ADD_ACTION) { 151 activity.addNewPreset(); 152 } else if (mAction.getType() != Action.SPACER) { 153 if (mAction.isDoubleAction()) { 154 long current = System.currentTimeMillis() - mDoubleActionLast; 155 if (current < mDoubleTapDelay) { 156 activity.showRepresentation(mAction.getRepresentation()); 157 } 158 mDoubleActionLast = System.currentTimeMillis(); 159 } else { 160 activity.showRepresentation(mAction.getRepresentation()); 161 } 162 mAdapter.setSelected(this); 163 } 164 } 165 166 @Override 167 public boolean onTouchEvent(MotionEvent event) { 168 boolean ret = super.onTouchEvent(event); 169 FilterShowActivity activity = (FilterShowActivity) getContext(); 170 171 if (event.getActionMasked() == MotionEvent.ACTION_UP) { 172 activity.startTouchAnimation(this, event.getX(), event.getY()); 173 } 174 if (!canBeRemoved()) { 175 return ret; 176 } 177 if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { 178 mStartTouchY = event.getY(); 179 mStartTouchX = event.getX(); 180 } 181 if (event.getActionMasked() == MotionEvent.ACTION_UP) { 182 setTranslationX(0); 183 setTranslationY(0); 184 } 185 if (event.getActionMasked() == MotionEvent.ACTION_MOVE) { 186 float delta = event.getY() - mStartTouchY; 187 if (getOrientation() == CategoryView.VERTICAL) { 188 delta = event.getX() - mStartTouchX; 189 } 190 if (Math.abs(delta) > mDeleteSlope) { 191 activity.setHandlesSwipeForView(this, mStartTouchX, mStartTouchY); 192 } 193 } 194 return true; 195 } 196 197 @Override 198 public void delete() { 199 mAdapter.remove(mAction); 200 } 201 } 202