ImageTinyPlanet.java revision 2c6ea941a80ea22317d664e329aed51f5f7417b9
1/*
2 * Copyright (C) 2012 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.imageshow;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.MotionEvent;
22
23import com.android.gallery3d.filtershow.filters.ImageFilterTinyPlanet;
24
25public class ImageTinyPlanet extends ImageSlave {
26
27    private float mTouchCenterX = 0;
28    private float mTouchCenterY = 0;
29    private float mCurrentX = 0;
30    private float mCurrentY = 0;
31    private float mCenterX = 0;
32    private float mCenterY = 0;
33    private float mStartAngle = 0;
34
35    public ImageTinyPlanet(Context context) {
36        super(context);
37    }
38
39    public ImageTinyPlanet(Context context, AttributeSet attrs) {
40        super(context, attrs);
41    }
42
43    protected static float angleFor(float dx, float dy) {
44        return (float) (Math.atan2(dx, dy) * 180 / Math.PI);
45    }
46
47    protected float getCurrentTouchAngle() {
48        if (mCurrentX == mTouchCenterX && mCurrentY == mTouchCenterY) {
49            return 0;
50        }
51        float dX1 = mTouchCenterX - mCenterX;
52        float dY1 = mTouchCenterY - mCenterY;
53        float dX2 = mCurrentX - mCenterX;
54        float dY2 = mCurrentY - mCenterY;
55
56        float angleA = angleFor(dX1, dY1);
57        float angleB = angleFor(dX2, dY2);
58        return (float) (((angleB - angleA) % 360) * Math.PI / 180);
59    }
60
61    @Override
62    public boolean onTouchEvent(MotionEvent event) {
63        ImageFilterTinyPlanet filter = (ImageFilterTinyPlanet) getCurrentFilter();
64        float x = event.getX();
65        float y = event.getY();
66        mCurrentX = x;
67        mCurrentY = y;
68        mCenterX = getWidth() / 2;
69        mCenterY = getHeight() / 2;
70        switch (event.getActionMasked()) {
71            case (MotionEvent.ACTION_DOWN):
72                mTouchCenterX = x;
73                mTouchCenterY = y;
74                mStartAngle = filter.getAngle();
75                break;
76            case (MotionEvent.ACTION_UP):
77            case (MotionEvent.ACTION_MOVE):
78                filter.setAngle(mStartAngle + getCurrentTouchAngle());
79                break;
80        }
81        resetImageCaches(this);
82        invalidate();
83        return true;
84    }
85}
86