Android 实现Ripple效果。

这篇是网上代码整理。(小白只会改皮毛,核心代码网上拿来)
首先:

public class RippleBackground extends RelativeLayout {

    private static final int DEFAULT_RIPPLE_COUNT = 6;
    private static final int DEFAULT_DURATION_TIME = 3000;
    private static final float DEFAULT_SCALE = 6.0f;
    private static final int DEFAULT_FILL_TYPE = 0;

    private int rippleColor;
    private float rippleStrokeWidth;
    private float rippleRadius;
    private int rippleDurationTime;
    private int rippleAmount;
    private int rippleDelay;
    private float rippleScale;
    private int rippleType;
    private Paint paint;
    private boolean animationRunning = false;
    private AnimatorSet animatorSet;
    private ArrayList<Animator> animatorList;
    private LayoutParams rippleParams;
    private ArrayList<RippleView> rippleViewList = new ArrayList<RippleView>();

    public RippleBackground(Context context) {
        super(context);
    }

    public RippleBackground(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RippleBackground(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(final Context context, final AttributeSet attrs) {
        if (isInEditMode())
            return;

        if (null == attrs) {
            throw new IllegalArgumentException("Attributes should be provided to this view,");
        }

        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RippleBackground);
        rippleColor = typedArray.getColor(R.styleable.RippleBackground_rb_color, getResources().getColor(R.color.rippelColor));
        rippleStrokeWidth = typedArray.getDimension(R.styleable.RippleBackground_rb_strokeWidth, getResources().getDimension(R.dimen.rippleStrokeWidth));
        rippleRadius = typedArray.getDimension(R.styleable.RippleBackground_rb_radius, getResources().getDimension(R.dimen.rippleRadius));
        rippleDurationTime = typedArray.getInt(R.styleable.RippleBackground_rb_duration, DEFAULT_DURATION_TIME);
        rippleAmount = typedArray.getInt(R.styleable.RippleBackground_rb_rippleAmount, DEFAULT_RIPPLE_COUNT);
        rippleScale = typedArray.getFloat(R.styleable.RippleBackground_rb_scale, DEFAULT_SCALE);
        rippleType = typedArray.getInt(R.styleable.RippleBackground_rb_type, DEFAULT_FILL_TYPE);
        typedArray.recycle();

        rippleDelay = rippleDurationTime / rippleAmount;

        paint = new Paint();
        paint.setAntiAlias(true);
        if (rippleType == DEFAULT_FILL_TYPE) {
            rippleStrokeWidth = 0;
            paint.setStyle(Paint.Style.FILL);
        } else
            paint.setStyle(Paint.Style.STROKE);
        paint.setColor(rippleColor);

        rippleParams = new LayoutParams((int) (2 * (rippleRadius + rippleStrokeWidth)), (int) (2 * (rippleRadius + rippleStrokeWidth)));
        rippleParams.addRule(CENTER_IN_PARENT, TRUE);

        animatorSet = new AnimatorSet();
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animatorList = new ArrayList<Animator>();

        for (int i = 0; i < rippleAmount; i++) {
            RippleView rippleView = new RippleView(getContext());
            addView(rippleView, rippleParams);
            rippleViewList.add(rippleView);
            final ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(rippleView, "ScaleX", 1.0f, rippleScale);
            scaleXAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            scaleXAnimator.setRepeatMode(ObjectAnimator.RESTART);
            scaleXAnimator.setStartDelay(i * rippleDelay);
            scaleXAnimator.setDuration(rippleDurationTime);
            animatorList.add(scaleXAnimator);
            final ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(rippleView, "ScaleY", 1.0f, rippleScale);
            scaleYAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            scaleYAnimator.setRepeatMode(ObjectAnimator.RESTART);
            scaleYAnimator.setStartDelay(i * rippleDelay);
            scaleYAnimator.setDuration(rippleDurationTime);
            animatorList.add(scaleYAnimator);
            final ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(rippleView, "Alpha", 1.0f, 0f);
            alphaAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            alphaAnimator.setRepeatMode(ObjectAnimator.RESTART);
            alphaAnimator.setStartDelay(i * rippleDelay);
            alphaAnimator.setDuration(rippleDurationTime);
            animatorList.add(alphaAnimator);
        }

        animatorSet.playTogether(animatorList);
    }

    private class RippleView extends View {

        public RippleView(Context context) {
            super(context);
            this.setVisibility(View.INVISIBLE);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            int radius = (Math.min(getWidth(), getHeight())) / 2;
            canvas.drawCircle(radius, radius, radius - rippleStrokeWidth, paint);
        }
    }

    public void startRippleAnimation() {
        if (!isRippleAnimationRunning()) {
            for (RippleView rippleView : rippleViewList) {
                rippleView.setVisibility(VISIBLE);
            }
            animatorSet.start();
            animationRunning = true;
        }
    }
    public void stopRippleAnimation(){
        if(isRippleAnimationRunning()){
            animatorSet.end();
            animationRunning=false;
        }
    }

    public boolean isRippleAnimationRunning(){
        return animationRunning;
    }
}

在attrs.xml下:

<declare-styleable name="RippleBackground">
    <attr name="rb_color" format="color" />
    <attr name="rb_strokeWidth" format="dimension"/>
    <attr name="rb_radius" format="dimension"/>
    <attr name="rb_duration" format="integer"/>
    <attr name="rb_rippleAmount" format="integer"/>
    <attr name="rb_scale" format="float"/>
    <attr name="rb_type" format="enum">
        <enum name="fillRipple" value="0"/>
        <enum name="strokeRipple" value="1"/>
    </attr>
</declare-styleable>

在colors.xml下:(配置需要的颜色)

<resources>
    <color name="rippelColor">#fe6362</color>
</resources>

新建dimens.xml:

<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

布局文件中:

<FrameLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
<com.包名.ripple.RippleBackground android:id="@+id/content"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    app:rb_color="#fe6362"
    app:rb_duration="3000"
    app:rb_radius="32dp"
    android:layout_gravity="center"
    app:rb_rippleAmount="6"
    app:rb_scale="6"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/centerImage"
        android:layout_centerInParent="true"
        android:layout_height="64dp"
        android:layout_width="64dp"
        android:visibility="gone"
        android:src="@mipmap/phone1" />

    <ImageView
        android:id="@+id/foundDevice"
        android:layout_above="@id/centerImage"
        android:layout_height="64dp"
        android:layout_marginBottom="32dp"
        android:layout_marginRight="6dp"
        android:layout_toLeftOf="@id/centerImage"
        android:layout_width="64dp"
        android:src="@mipmap/phone2"
        android:visibility="invisible" />
</com.xx.ripple.RippleBackground>
    <ImageView
        android:id="@+id/main_img1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"
        />
</FrameLayout>

在MainActivity中:

public class MainActivity extends Activity {
    private ImageView foundDevice,showView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RippleBackground hideView=(RippleBackground)findViewById(R.id.content);
 // 可以用线程实现

//        final Handler handler=new Handler();

//        foundDevice=(ImageView)findViewById(R.id.foundDevice);
        showView=(ImageView)findViewById(R.id.main_img1);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
        hideView.startRippleAnimation();
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        buttonimg();
                        rippleBackground.setVisibility(rippleBackground.GONE);
                        buttonimg.setVisibility(buttonimg.VISIBLE);
//                        rippleBackground.stopRippleAnimation();
//                    }
//                },3000);

//        rippleBackground.setVisibility(rippleBackground.GONE);

        showView.setAlpha(0f);
        showView.setVisibility(View.VISIBLE);

        showView.animate()
                .alpha(1f)
                .setDuration(5000)
                .setListener(null);

        hideView.animate()
                .alpha(0f)
                .setDuration(5000)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        hideView.setVisibility(View.GONE);
                    }
                });

            }
}

这样就实现了类似于水波涟漪的效果。如图:
涟漪效果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 点击效果通常可以通过设置 View 的属性来实现。常见的点击效果包括:背景色变化、描边变化、阴影效果、不透明度变化等。 以下是一些实现点击效果的示例: 1. Selector 状态选择器 使用 Selector 状态选择器可以实现在不同的状态下显示不同的效果。可以通过在 drawable 目录下创建 selector 文件来实现。 例如,在 res/drawable 目录下创建一个 selector_mybutton.xml 文件,内容如下: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/mybutton_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/mybutton_focused" android:state_focused="true" /> <item android:drawable="@drawable/mybutton_normal" /> </selector> ``` 在布局文件中使用该 selector 文件作为 Button 的背景,例如: ```xml <Button android:id="@+id/mybutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_mybutton" android:text="My Button" /> ``` 这样,当按钮被按下或者获得焦点时,就会显示不同的背景。 2. Ripple 涟漪效果 使用 Ripple 涟漪效果可以实现点击时产生类似水波纹的效果。可以通过在 drawable 目录下创建 ripple 文件来实现。 例如,在 res/drawable 目录下创建一个 ripple_mybutton.xml 文件,内容如下: ```xml <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:attr/colorControlHighlight"> <item android:drawable="@drawable/mybutton_normal" /> </ripple> ``` 在布局文件中使用该 ripple 文件作为 Button 的背景,例如: ```xml <Button android:id="@+id/mybutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/ripple_mybutton" android:text="My Button" /> ``` 这样,当按钮被点击时,就会产生涟漪效果。 还可以通过设置 View 的 elevation 属性来实现阴影效果,或者通过设置 View 的 alpha 属性来实现不透明度变化效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值