Android 5.0のRippleEffectを4.4以下で同じような動きで作ってみた

使う場合はいろいろなものを適正読み替えてくださいー。



public class RippleLinearLayout extends LinearLayoutCompat {

private static final int DURATION = 2000;
private static final int RIPPLE_ALPHA = 150;
private static final int TOUCH_ALPHA = 50;
private static final int ACTIVATE_ALPHA = 150;

@Setter
private float mDownX;
@Setter
private float mDownY;

private float mRadius;

private Paint mPaint;
private boolean mIsActivatedAnimate = false;
private int mColor = Color.BLACK;
private int mActivateAlpha = ACTIVATE_ALPHA;
private int mDuration = DURATION;
private int mRippleAlpha = RIPPLE_ALPHA;

private boolean mPressed = false;
private boolean mActivated = false;
private AnimatorSet mAnimator;


private void init(Context context, AttributeSet attrs) {
mPaint = new Paint();
mPaint.setAlpha(mRippleAlpha);

mColor = getResources().getColor(R.color.ColorGray);

if (attrs != null) {
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RippleView);
mRippleAlpha = a.getInteger(R.styleable.RippleView_rippleAlpha, RIPPLE_ALPHA);
mIsActivatedAnimate = a.getBoolean(R.styleable.RippleView_isActivateAnimate,false);
mDuration = a.getInteger(R.styleable.RippleView_rippleDuration, DURATION);
a.recycle();
}

}

/**
* コンストラクタ
*
* @param context コンテキスト
*/
public RippleLinearLayout(Context context) {
this(context, null);
setBackgroundResource(R.color.Transparent);
setOnTouchListener(new ontouchListener());
init(context, null);
}

/**
* コンストラクタ2
*
* @param context コンテキスト
* @param attrs アトリビュート
*/
public RippleLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
setBackgroundResource(R.color.Transparent);
setOnTouchListener(new ontouchListener());
init(context, attrs);
}
}

private class ontouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = event.getX();
mDownY = event.getY();
case MotionEvent.ACTION_UP:
break;
}
return false;
}
}

/**
* コンストラクタ3
*
* @param context コンテキスト
* @param attrs アトリビュート
*/
public RippleLinearLayout(Context context, AttributeSet attrs, int Defstyle) {
super(context, attrs, Defstyle);
setBackgroundResource(R.color.Transparent);
setOnTouchListener(new ontouchListener());
}

public void setRadius(final float radius) {
mRadius = radius;
if (mRadius > 0) {
RadialGradient radialGradient = new RadialGradient(
mDownX,
mDownY,
mRadius * 3,
mColor,
mColor,
Shader.TileMode.MIRROR
);
mPaint.setAlpha(mRippleAlpha);
mPaint.setShader(radialGradient);
}
invalidate();
}

@Override
public void setPressed(boolean pressed) {

if (pressed != mPressed) {
mPressed = pressed;
if (mPressed) {
mAnimator = new AnimatorSet();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(RippleLinearLayout.this, “radius”, getWidth() / 10.0f, getWidth() * 3.0f);
animator1.setInterpolator(new AccelerateDecelerateInterpolator());
animator1.setDuration(mDuration);
mAnimator.playTogether(animator1);
mAnimator.start();
setBackgroundColor(Color.argb(TOUCH_ALPHA, Color.red(mColor), Color.green(mColor), Color.blue(mColor)));
} else {
if (mAnimator != null)
mAnimator.cancel();
setRadius(0);
// if(!mIsActivatedAnimate && !isActivated())
setBackgroundColor(getResources().getColor(R.color.Transparent));
}
}
if (!pressed) {

}

super.setPressed(pressed);
}

@Override
public void setActivated(boolean activated) {
super.setActivated(activated);

if (mActivated != activated) {
mActivated = activated;
if (activated) {
if (mIsActivatedAnimate)
setBackgroundColor(Color.argb(mActivateAlpha, Color.red(mColor), Color.green(mColor), Color.blue(mColor)));
} else {
setBackgroundColor(getResources().getColor(R.color.Transparent));
}
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.drawCircle(mDownX, mDownY, mRadius, mPaint);

}


}

関連記事

コメントする

メモ - 下記に表示されているHTML属性は利用できます HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*