CategoryView.java revision a0cd22d21474c112ad78b011fbd6f0be977400e6
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.Canvas;
23import android.graphics.Color;
24import android.graphics.Paint;
25import android.graphics.Rect;
26import android.view.MotionEvent;
27import android.view.View;
28import com.android.gallery3d.R;
29import com.android.gallery3d.filtershow.FilterShowActivity;
30import com.android.gallery3d.filtershow.ui.SelectionRenderer;
31
32public class CategoryView extends IconView
33        implements View.OnClickListener, SwipableView{
34
35    private static final String LOGTAG = "CategoryView";
36    public static final int VERTICAL = 0;
37    public static final int HORIZONTAL = 1;
38    private Paint mPaint = new Paint();
39    private Action mAction;
40    private Paint mSelectPaint;
41    CategoryAdapter mAdapter;
42    private int mSelectionStroke;
43    private Paint mBorderPaint;
44    private int mBorderStroke;
45    private float mStartTouchY = 0;
46    private float mDeleteSlope = 20;
47
48    public CategoryView(Context context) {
49        super(context);
50        setOnClickListener(this);
51        Resources res = getResources();
52        mSelectionStroke = res.getDimensionPixelSize(R.dimen.thumbnail_margin);
53        mSelectPaint = new Paint();
54        mSelectPaint.setStyle(Paint.Style.FILL);
55        mSelectPaint.setColor(res.getColor(R.color.filtershow_category_selection));
56        mBorderPaint = new Paint(mSelectPaint);
57        mBorderPaint.setColor(Color.BLACK);
58        mBorderStroke = mSelectionStroke / 3;
59    }
60
61    @Override
62    public boolean isHalfImage() {
63        if (mAction == null) {
64            return false;
65        }
66        if (mAction.getType() == Action.CROP_VIEW) {
67            return true;
68        }
69        return false;
70    }
71
72    public void onDraw(Canvas canvas) {
73        if (mAction != null) {
74            if (mAction.getImage() == null) {
75                mAction.setImageFrame(new Rect(0, 0, getWidth(), getHeight()), getOrientation());
76            } else {
77                setBitmap(mAction.getImage());
78            }
79        }
80        super.onDraw(canvas);
81        if (mAdapter.isSelected(this)) {
82            SelectionRenderer.drawSelection(canvas, 0, 0,
83                    getWidth(), getHeight(),
84                    mSelectionStroke, mSelectPaint, mBorderStroke, mBorderPaint);
85        }
86    }
87
88    public void setAction(Action action, CategoryAdapter adapter) {
89        mAction = action;
90        setText(mAction.getName());
91        mAdapter = adapter;
92        invalidate();
93    }
94
95    @Override
96    public void onClick(View view) {
97        FilterShowActivity activity = (FilterShowActivity) getContext();
98        activity.showRepresentation(mAction.getRepresentation());
99        mAdapter.setSelected(this);
100    }
101
102    @Override
103    public boolean onTouchEvent(MotionEvent event) {
104        super.onTouchEvent(event);
105        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
106            mStartTouchY = event.getY();
107        }
108        if (event.getActionMasked() == MotionEvent.ACTION_UP) {
109            setTranslationY(0);
110        }
111        if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
112            float delta = event.getY() - mStartTouchY;
113            if (Math.abs(delta) > mDeleteSlope) {
114                FilterShowActivity activity = (FilterShowActivity) getContext();
115                activity.setHandlesSwipeForView(this, mStartTouchY);
116            }
117        }
118        return true;
119    }
120
121    @Override
122    public void delete() {
123        mAdapter.remove(mAction);
124    }
125}
126