BatchStepSensor / src / com.example.android.batchstepsensor / cardstream /

CardActionButton.java

1
/*
2
* Copyright 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
 
18
 
19
 
20
package com.example.android.batchstepsensor.cardstream;
21
 
22
import android.content.Context;
23
import android.util.AttributeSet;
24
import android.view.MotionEvent;
25
import android.view.animation.BounceInterpolator;
26
import android.view.animation.DecelerateInterpolator;
27
import android.widget.Button;
28
 
29
/**
30
 * Custom Button with a special 'pressed' effect for touch events.
31
 */
32
public class CardActionButton extends Button {
33
 
34
    public CardActionButton(Context context) {
35
        super(context);
36
    }
37
 
38
    public CardActionButton(Context context, AttributeSet attrs) {
39
        super(context, attrs);
40
    }
41
 
42
    public CardActionButton(Context context, AttributeSet attrs, int defStyle) {
43
        super(context, attrs, defStyle);
44
    }
45
 
46
    @Override
47
    public boolean onTouchEvent(MotionEvent event) {
48
 
49
        switch(event.getAction()){
50
            case MotionEvent.ACTION_DOWN:
51
                setPressed(true);
52
                animate().scaleX(0.98f).scaleY(0.98f).alpha(0.8f).setDuration(100).
53
                        setInterpolator(new DecelerateInterpolator());
54
                break;
55
            case MotionEvent.ACTION_UP:
56
                animate().scaleX(1.0f).scaleY(1.f).alpha(1.0f).setDuration(50).
57
                        setInterpolator(new BounceInterpolator());
58
                break;
59
            case MotionEvent.ACTION_CANCEL:
60
                animate().scaleX(1.0f).scaleY(1.f).alpha(1.0f).setDuration(50).
61
                        setInterpolator(new BounceInterpolator());
62
                break;
63
        }
64
 
65
        return super.onTouchEvent(event);
66
    }
67
 
68
}
69