Instantly share code, notes, and snippets.
Last active
November 16, 2016 04:19
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save androidovshchik/5d8e58be3d80a62fea577e5d75008783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mrcpp.custombutton; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.BlurMaskFilter; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.RectF; | |
import android.os.Build; | |
import android.support.annotation.Nullable; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.ViewTreeObserver; | |
import android.view.animation.AlphaAnimation; | |
import android.widget.FrameLayout; | |
import android.widget.TextView; | |
public class GlowButton extends FrameLayout implements ViewTreeObserver.OnGlobalLayoutListener { | |
@SuppressWarnings("unused") | |
private static final String TAG = GlowButton.class.getSimpleName(); | |
private static final int ANIMATION_TIME = 500; | |
public static final int GLOWING_COLOR = Color.parseColor("#fdb528"); | |
private static final int MARGIN = dp2px(8); | |
private GlowView glowView; | |
public TextView button; | |
public GlowButton(Context context) { | |
super(context); | |
init(null); | |
} | |
public GlowButton(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(attrs); | |
} | |
public GlowButton(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(attrs); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
@SuppressWarnings("unused") | |
public GlowButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(attrs); | |
} | |
private void init(@Nullable AttributeSet attrs) { | |
setBackgroundResource(0); | |
glowView = new GlowView(getContext()); | |
glowView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
glowView.setVisibility(INVISIBLE); | |
button = new TextView(getContext(), attrs); | |
button.getViewTreeObserver().addOnGlobalLayoutListener(this); | |
button.setVisibility(VISIBLE); | |
addView(button); | |
((MarginLayoutParams) button.getLayoutParams()).setMargins(MARGIN, MARGIN, MARGIN, MARGIN); | |
} | |
private static int dp2px(int dp) { | |
float density = Resources.getSystem().getDisplayMetrics().density; | |
return Math.round(dp * density); | |
} | |
@Override | |
public void onGlobalLayout() { | |
int width = button.getWidth(); | |
int height = button.getHeight(); | |
if (width != 0 || height != 0) { | |
button.getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
LayoutParams params = new LayoutParams( | |
width + 2 * MARGIN, | |
height + 2 * MARGIN | |
); | |
params.gravity = Gravity.CENTER; | |
glowView.setLayoutParams(params); | |
addView(glowView, 0); | |
} | |
} | |
@Override | |
public boolean dispatchTouchEvent(MotionEvent event) { | |
switch (event.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
animateGlowing(0f, 1f); | |
break; | |
case MotionEvent.ACTION_UP: | |
animateGlowing(1f, 0f); | |
break; | |
} | |
return super.dispatchTouchEvent(event); | |
} | |
private void animateGlowing(float from, float to) { | |
AlphaAnimation animation = new AlphaAnimation(from, to); | |
animation.setDuration(ANIMATION_TIME); | |
animation.setFillAfter(true); | |
glowView.startAnimation(animation); | |
if (glowView.getVisibility() != VISIBLE) { | |
glowView.setVisibility(VISIBLE); | |
} | |
} | |
private static class GlowView extends View { | |
private Paint paint; | |
private RectF rect; | |
public GlowView(Context context) { | |
super(context); | |
init(); | |
} | |
public GlowView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public GlowView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
@SuppressWarnings("unused") | |
public GlowView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(); | |
} | |
private void init() { | |
paint = new Paint(); | |
paint.setMaskFilter(new BlurMaskFilter(dp2px(8), BlurMaskFilter.Blur.NORMAL)); | |
paint.setColor(GLOWING_COLOR); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if (rect == null) { | |
rect = new RectF(MARGIN, MARGIN, getWidth() - MARGIN, getHeight() - MARGIN); | |
} | |
canvas.drawRoundRect(rect, dp2px(12), dp2px(12), paint); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment