CategoryView.java revision 32cc4dd751569721aa19218b4d947145577060d0
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
17package com.android.gallery3d.filtershow.category;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.Paint;
26import android.graphics.Rect;
27import android.view.MotionEvent;
28import android.view.View;
29import com.android.gallery3d.R;
30import com.android.gallery3d.filtershow.FilterShowActivity;
31import com.android.gallery3d.filtershow.ui.SelectionRenderer;
32
33public 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
53    public CategoryView(Context context) {
54        super(context);
55        setOnClickListener(this);
56        Resources res = getResources();
57        mSelectionStroke = res.getDimensionPixelSize(R.dimen.thumbnail_margin);
58        mSelectPaint = new Paint();
59        mSelectPaint.setStyle(Paint.Style.FILL);
60        mSelectionColor = res.getColor(R.color.filtershow_category_selection);
61        mSpacerColor = res.getColor(R.color.filtershow_categoryview_text);
62
63        mSelectPaint.setColor(mSelectionColor);
64        mBorderPaint = new Paint(mSelectPaint);
65        mBorderPaint.setColor(Color.BLACK);
66        mBorderStroke = mSelectionStroke / 3;
67    }
68
69    @Override
70    public boolean isHalfImage() {
71        if (mAction == null) {
72            return false;
73        }
74        if (mAction.getType() == Action.CROP_VIEW) {
75            return true;
76        }
77        if (mAction.getType() == Action.ADD_ACTION) {
78            return true;
79        }
80        return false;
81    }
82
83    private boolean canBeRemoved() {
84        return mCanBeRemoved;
85    }
86
87    private void drawSpacer(Canvas canvas) {
88        mPaint.reset();
89        mPaint.setAntiAlias(true);
90        mPaint.setColor(mSpacerColor);
91        if (getOrientation() == CategoryView.VERTICAL) {
92            canvas.drawCircle(getWidth() / 2, getHeight() / 2, getHeight() / 5, mPaint);
93        } else {
94            canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 5, mPaint);
95        }
96    }
97
98    @Override
99    public boolean needsCenterText() {
100        if (mAction != null && mAction.getType() == Action.ADD_ACTION) {
101            return true;
102        }
103        return super.needsCenterText();
104    }
105
106    public void onDraw(Canvas canvas) {
107        if (mAction != null) {
108            if (mAction.getType() == Action.SPACER) {
109                drawSpacer(canvas);
110                return;
111            }
112            mAction.setImageFrame(new Rect(0, 0, getWidth(), getHeight()), getOrientation());
113            if (mAction.getImage() != null) {
114                setBitmap(mAction.getImage());
115            }
116        }
117        super.onDraw(canvas);
118        if (mAdapter.isSelected(this)) {
119            SelectionRenderer.drawSelection(canvas, 0, 0,
120                    getWidth(), getHeight(),
121                    mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint);
122        }
123    }
124
125    public void setAction(Action action, CategoryAdapter adapter) {
126        mAction = action;
127        setText(mAction.getName());
128        mAdapter = adapter;
129        mCanBeRemoved = action.canBeRemoved();
130        setUseOnlyDrawable(false);
131        if (mAction.getType() == Action.ADD_ACTION) {
132            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.filtershow_add);
133            setBitmap(bitmap);
134            setUseOnlyDrawable(true);
135            setText(getResources().getString(R.string.filtershow_add_button_looks));
136        } else {
137            setBitmap(mAction.getImage());
138        }
139        invalidate();
140    }
141
142    @Override
143    public void onClick(View view) {
144        FilterShowActivity activity = (FilterShowActivity) getContext();
145        if (mAction.getType() == Action.ADD_ACTION) {
146            activity.addNewPreset();
147        } else if (mAction.getType() != Action.SPACER) {
148            activity.showRepresentation(mAction.getRepresentation());
149            mAdapter.setSelected(this);
150        }
151    }
152
153    @Override
154    public boolean onTouchEvent(MotionEvent event) {
155        boolean ret = super.onTouchEvent(event);
156        FilterShowActivity activity = (FilterShowActivity) getContext();
157
158        if (event.getActionMasked() == MotionEvent.ACTION_UP) {
159            activity.startTouchAnimation(this, event.getX(), event.getY());
160        }
161        if (!canBeRemoved()) {
162            return ret;
163        }
164        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
165            mStartTouchY = event.getY();
166            mStartTouchX = event.getX();
167        }
168        if (event.getActionMasked() == MotionEvent.ACTION_UP) {
169            setTranslationX(0);
170            setTranslationY(0);
171        }
172        if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
173            float delta = event.getY() - mStartTouchY;
174            if (getOrientation() == CategoryView.VERTICAL) {
175                delta = event.getX() - mStartTouchX;
176            }
177            if (Math.abs(delta) > mDeleteSlope) {
178                activity.setHandlesSwipeForView(this, mStartTouchX, mStartTouchY);
179            }
180        }
181        return true;
182    }
183
184    @Override
185    public void delete() {
186        mAdapter.remove(mAction);
187    }
188}
189