com.skydoves.elasticviewsexample.ElasticVIews.ElasticButton.java Source code

Java tutorial

Introduction

Here is the source code for com.skydoves.elasticviewsexample.ElasticVIews.ElasticButton.java

Source

/*
 * Copyright (C) 2017 skydoves
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.skydoves.elasticviewsexample.ElasticVIews;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.CycleInterpolator;
import android.widget.Button;

import com.skydoves.elasticviewsexample.R;

public class ElasticButton extends AppCompatButton {

    private Button view;
    private OnClickListener listener;

    private int round = 20;
    private float scale = 0.9f;
    private int color = R.color.colorPrimary;
    private int duration = 500;

    private String labelText = "";
    private int labelColor = Color.WHITE;
    private int labelSize = 10;
    private int labelStyle = 0;

    public ElasticButton(Context context) {
        super(context);
        onCreate();
    }

    public ElasticButton(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        onCreate();
        getAttrs(attributeSet);
    }

    public ElasticButton(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        onCreate();
        getAttrs(attributeSet, defStyle);
    }

    private void onCreate() {
        view = this;
        view.setAllCaps(false);
        view.setBackgroundResource(R.drawable.rectangle_button);
    }

    private void getAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ElasticButton);
        setTypeArray(typedArray);
    }

    private void getAttrs(AttributeSet attrs, int defStyle) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ElasticButton, defStyle, 0);
        setTypeArray(typedArray);
    }

    private void setTypeArray(TypedArray typedArray) {
        GradientDrawable bgShape = (GradientDrawable) view.getBackground();

        round = typedArray.getInt(R.styleable.ElasticButton_button_round, round);
        bgShape.setCornerRadius(round);

        color = typedArray.getInt(R.styleable.ElasticButton_button_backgroundColor, color);
        bgShape.setColor(color);

        scale = typedArray.getFloat(R.styleable.ElasticButton_button_scale, scale);

        duration = typedArray.getInt(R.styleable.ElasticButton_button_duration, duration);

        labelText = typedArray.getString(R.styleable.ElasticButton_button_labelText);
        view.setText(labelText);

        labelColor = typedArray.getInt(R.styleable.ElasticButton_button_labelColor, labelColor);
        view.setTextColor(labelColor);

        labelSize = typedArray.getInt(R.styleable.ElasticButton_button_labelSize, labelSize);
        view.setTextSize(labelSize);

        labelStyle = typedArray.getInt(R.styleable.ElasticButton_button_labelStyle, labelStyle);

        if (labelStyle == 0)
            view.setTypeface(null, Typeface.NORMAL);
        else if (labelStyle == 1)
            view.setTypeface(null, Typeface.BOLD);
        else if (labelStyle == 2)
            view.setTypeface(null, Typeface.ITALIC);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (listener != null) {
                if (view.getScaleX() == 1) {
                    ViewCompat.animate(view).setDuration(duration).scaleX(scale).scaleY(scale)
                            .setInterpolator(new CycleInterpolator(0.5f))
                            .setListener(new ViewPropertyAnimatorListener() {

                                @Override
                                public void onAnimationStart(final View view) {
                                }

                                @Override
                                public void onAnimationEnd(final View v) {
                                    onClick();
                                }

                                @Override
                                public void onAnimationCancel(final View view) {
                                }
                            }).withLayer().start();
                }
            }
        }
        return super.dispatchTouchEvent(event);
    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    private void onClick() {
        listener.onClick(this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }
}