没有触摸的涟漪效果动画

时间:2015-09-02 12:40:02

标签: android android-listview android-animation android-canvas rippledrawable

我需要做一些像Listview项目背景更改的涟漪效应。我试着像这样使用ObjectAnimator

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(activity,
                    R.animator.animator_bkg);
            set.setTarget(childLinear);
            set.setInterpolator(new AccelerateInterpolator());
            set.start(); 

R.animator.animator_bkg:

<objectAnimator
   android:propertyName="backgroundColor"
   android:duration="3000"
   android:valueFrom="@color/white"
   android:valueTo="@color/redTrans"
   android:repeatCount="-1"
   android:repeatMode="reverse"/>

它可以流畅地改变背景(完全填充),但是我需要在触摸按钮后逐渐填充ListView项目,如涟漪效果。

我想,也许我可以使用Canvas来覆盖onDraw,但这对应用来说很难,而且可能会有一些滞后。

1 个答案:

答案 0 :(得分:1)

您可以使用自定义视图执行此操作并在onDraw()中实现循环过渡,但它很复杂。

您可以在子视图上使用ViewAnimationUtils.createCircularReveal()来解决复杂问题。但缺点是它只适用于API 21 +。

简而言之,您的单元格根布局必须是FrameLayoutRelativeLayout。 转换开始时,使用开始和结束颜色在单元格下动态添加2个视图,然后使用2之间的圆形显示进行转换。在转换结束时,只需删除2个子视图以保持视图层次结构有点清洁。

结果如下:

Color reveal

在代码中:

单元格布局:

<FrameLayout
    android:id="@+id/cell_root"
    android:layout_width="match_parent"
    android:layout_height="72dp">

    <LinearLayout
        android:id="@+id/cell_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:background="#FF00FF">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textSize="18sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is content"
            android:textSize="14sp" />
    </LinearLayout>

</FrameLayout>

触发后台转换的代码:

private void changeBackgroundColor() {
    final FrameLayout startingColorFrame = new FrameLayout(mCellRoot.getContext());
    final FrameLayout endingColorFrame = new FrameLayout(mCellRoot.getContext());
    startingColorFrame.setBackground(mCellContent.getBackground());
    endingColorFrame.setBackground(mPendingColor);
    mCellContent.setBackground(null);
    endingColorFrame.setVisibility(View.GONE);
    mCellRoot.addView(endingColorFrame, 0, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    mCellRoot.addView(startingColorFrame, 0, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    int finalRadius = (int) Math.sqrt(mCellRoot.getWidth()*mCellRoot.getWidth() + mCellRoot.getHeight()*mCellRoot.getHeight());
    final int sourceX = mCellRoot.getWidth() / 3;
    final int sourceY = mCellRoot.getHeight() / 2;
    // this is API 21 minimum. Add proper checks
    final Animator circularReveal = ViewAnimationUtils.createCircularReveal(endingColorFrame, sourceX, sourceY, 0, finalRadius);
    endingColorFrame.setVisibility(View.VISIBLE);
    circularReveal.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(final Animator animation) {
            super.onAnimationEnd(animation);
            mStartButton.setEnabled(true);
            mCellContent.setBackground(mPendingColor);
            mPendingColor = startingColorFrame.getBackground();
            mCellRoot.removeView(startingColorFrame);
            mCellRoot.removeView(endingColorFrame);
        }
    });
    // customize the animation here
    circularReveal.setDuration(800);
    circularReveal.setInterpolator(new AccelerateInterpolator());
    circularReveal.start();
}