Created
February 26, 2019 03:25
-
-
Save twiceyuan/e6a89e825a4b2ce313d79ea3a14fe22d to your computer and use it in GitHub Desktop.
[全局水波纹 View] #Android #View
This file contains hidden or 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
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.os.Build; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.widget.FrameLayout; | |
import java.util.List; | |
import java.util.concurrent.CopyOnWriteArrayList; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
public class ClickRippleLayout extends FrameLayout { | |
private static final float rippleWidth = 200; | |
private List<Circle> circles = new CopyOnWriteArrayList<>(); | |
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
public ClickRippleLayout(@NonNull Context context) { | |
super(context); | |
init(); | |
} | |
public ClickRippleLayout(@NonNull Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public ClickRippleLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
private void init() { | |
paint.setColor(Color.GRAY); | |
paint.setStyle(Paint.Style.FILL); | |
setBackgroundResource(android.R.color.transparent); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
setElevation(Integer.MAX_VALUE); | |
} | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
for (final Circle circle : circles) { | |
paint.setAlpha((int) (rippleWidth - circle.radius)); | |
canvas.drawCircle(circle.x, circle.y, circle.radius, paint); | |
circle.radius += 1 + circle.radius / 20; | |
if (circle.radius >= rippleWidth) { | |
circles.remove(circle); | |
} | |
postInvalidateDelayed(1); | |
} | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
switch (ev.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
circles.add(new Circle(ev.getX(), ev.getY())); | |
postInvalidate(); | |
return false; | |
} | |
return super.onInterceptTouchEvent(ev); | |
} | |
private static class Circle { | |
final float x; | |
final float y; | |
float radius = 20; | |
Circle(float x, float y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<Button | |
android:id="@+id/tv_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="@string/app_name" /> | |
<com.twiceyuan.globalclickeffect.ClickRippleLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment