就是一个按钮的特效:可以根据自己的需求修改我们的类颜色效果都可以改变的
图一:
图二:
图三:
第一步:写你的Button样式
package com.example.xiaona.mycompany.attention;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import com.example.xiaona.mycompany.R;
/**
* Created by tong.zhang on 2017/12/1.
*/
public class NbButton extends android.support.v7.widget.AppCompatButton {
private int width;
private int heigh;
public GradientDrawable backDrawable;
private boolean isMorphing;
private int startAngle;
private Paint paint;
private ValueAnimator arcValueAnimator;
public NbButton(Context context) {
super(context);
init(context);
}
public NbButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public NbButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
isMorphing=false;
backDrawable=new GradientDrawable();
int colorDrawable=context.getResources().getColor(R.color.blue);
backDrawable.setColor(colorDrawable);
backDrawable.setCornerRadius(120);
setBackground(backDrawable);
paint=new Paint();
paint.setColor(getResources().getColor(R.color.white));
paint.setStrokeWidth(4);
paint.setStyle(Paint.Style.STROKE);
paint.setTextSize(2);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int heighMode=MeasureSpec.getMode(heightMeasureSpec);
int heighSize=MeasureSpec.getSize(heightMeasureSpec);
if (widthMode==MeasureSpec.EXACTLY){
width=widthSize;
}
if (heighMode==MeasureSpec.EXACTLY){
heigh=heighSize;
}
}
public void startAnim(){
isMorphing=true;
setTag(getText());
setText("");
ValueAnimator valueAnimator=ValueAnimator.ofInt(width,heigh);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value= (int) animation.getAnimatedValue();
int leftOffset=(width-value)/2;
int rightOffset=width-leftOffset;
backDrawable.setBounds(leftOffset,0,rightOffset,heigh);
}
});
ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(backDrawable,"cornerRadius",120,heigh/2);
AnimatorSet animatorSet=new AnimatorSet();
animatorSet.setDuration(500);
animatorSet.playTogether(valueAnimator,objectAnimator);
animatorSet.start();
//画中间的白色圆圈
showArc();
}
public void gotoNew(){
isMorphing=false;
arcValueAnimator.cancel();
setVisibility(GONE);
}
public void regainBackground(){
//setVisibility(VISIBLE);
backDrawable.setBounds(0,0,width,heigh);
backDrawable.setCornerRadius(24000);
ValueAnimator valueAnimator=ValueAnimator.ofInt(heigh,width);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value= (int) animation.getAnimatedValue();
int leftOffset=(width-value)/2;
int rightOffset=width-leftOffset;
backDrawable.setBounds(leftOffset,0,rightOffset,heigh);
}
});
ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(backDrawable,"cornerRadius",120,heigh/2);
AnimatorSet animatorSet=new AnimatorSet();
animatorSet.setDuration(500);
animatorSet.playTogether(valueAnimator,objectAnimator);
animatorSet.start();
//setBackground(backDrawable);
setText((String) getTag());
isMorphing=false;
}
private void showArc() {
arcValueAnimator=ValueAnimator.ofInt(0,1080);
arcValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
startAngle= (int) animation.getAnimatedValue();
invalidate();
}
});
arcValueAnimator.setInterpolator(new LinearInterpolator());
arcValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
arcValueAnimator.setDuration(3000);
arcValueAnimator.start();
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
if (isMorphing==true){
final RectF rectF=new RectF(getWidth()*5/12,getHeight()/2-getWidth()/12,getWidth()*7/12,getHeight()/2+getWidth()/12);
canvas.drawArc(rectF,startAngle,270,false,paint);
}
}
}
第二步:你的xml按钮引用你包下面的写好的按钮类
<com.example.xiaona.mycompany.attention.NbButton
android:id="@+id/attention"
android:textSize="28px"
android:layout_alignParentRight="true"
android:layout_width="122px"
android:layout_height="50px"
android:padding="2px"
android:text="+关注"
android:textColor="@color/white"
android:layout_centerVertical="true"
/>
第三步:在你的Activity里面调用 声明按钮,获取findViewById(R.id.attention);
private NbButton attention;
attention = itemView.findViewById(R.id.attention);
第3.1步:点击的时候调用样式
holder.attention.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
holder.attention.startAnim();
holder.attention.postDelayed(new Runnable() {
@SuppressLint("ResourceAsColor")
@Override
public void run() {
holder.attention.backDrawable.setColor(R.color.white);
holder.attention.setTag("已关注");
holder.attention.regainBackground();
}
},2500);
}
});