【问题标题】:How can I achieve this ListView animation?我怎样才能实现这个 ListView 动画?
【发布时间】:2014-06-09 20:40:54
【问题描述】:

怎么了,伙计们,我需要一点帮助。我正在尝试在正在滚动的列表视图上实现一个简单的(但不是真的)折叠动画。基本上,我试图将列表视图的第一个可见子项向后折叠,就好像一张纸沿着 X 轴向下折叠一样。当用户向上和向下滚动列表时,这种情况会持续进行。这是我第一次从图形 api 玩 Matrix 动画和 Android 的相机,所以我在这里肯定是不合时宜的。

这是我想要达到的效果

这就是我得到的效果。

我希望动画从原点 (0,0) 但左侧和右侧开始,从列表项的顶部而不是左上角开始动画。我对矩阵转换或动画不是很熟悉,所以如果有人比我更有这些技术的经验,可以了解一些知识,将不胜感激。

基本上我重写了 ListView 的 onDrawChild 方法,从绘图缓存中获取孩子的位图,并使用矩阵来执行动画。照明和相机实现是我从另一个示例应用程序中获取的代码,目的是让动画看起来尽可能 3D。

我尝试使用ListView animations 库,但运气不佳。我还尝试使用开发人员指南here 中的代码编写一个解决方案,该解决方案使用对象动画器来实现漂亮的小卡片翻转动画,但它开始感觉有点笨拙,我无法完全按照我想要的方式得到它。

这是我当前的实现。如果有人可以对此有所启发或指导,或者如果有人写了一个我在搜索中没有遇到的很棒的库,请随时分享。谢谢

   @Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {

    View first = getChildAt(0);

    if (child == first) {
        if (child.getTop() < 0) {

            Bitmap bitmap = getChildDrawingCache(child);
            final int top = child.getTop();
            child.getRight();
            child.getBottom();
            child.getLeft();

            final int childCenterY = child.getHeight() / 2;
            // final int childCenterX = child.getWidth() / 2;
            final int parentCenterY = getHeight() / 2; // center point of
            // child relative to list

            final int absChildCenterY = child.getTop() + childCenterY;

            // final int bottom = child.getBottom();

            // distance of child center to the list center final int
            int distanceY = parentCenterY - absChildCenterY;

            final int r = getHeight() / 2;

            if (mAnimate) {

                prepareMatrix(mMatrix, distanceY, r);



                mMatrix.preTranslate(0, top);

                mMatrix.postTranslate(0, -top);



            }
            canvas.drawBitmap(bitmap, mMatrix, mPaint);

        }

        else {
            super.drawChild(canvas, child, drawingTime);
        }
    } else {
        super.drawChild(canvas, child, drawingTime);
    }
    return false;

}

private void prepareMatrix(final Matrix outMatrix, int distanceY, int r) { // clip
                                                                            // the
                                                                            // distance

    final int d = Math.min(r, Math.abs(distanceY)); //
    // circle formula
    final float translateZ = (float) Math.sqrt((r * r) - (d * d));

    double radians = Math.acos((float) d / r);
    double degree = 45 - (180 / Math.PI) * radians;

    // double degree = -180;

    mCamera.save();
    mCamera.translate(0, 0, r - translateZ);
    mCamera.rotateX((float) degree);
    if (distanceY < 0) {
        degree = 360 - degree;
    }
    mCamera.rotateY((float) degree);
    mCamera.getMatrix(outMatrix);
    mCamera.restore();

    // highlight elements in the middle
    mPaint.setColorFilter(calculateLight((float) degree));
}

private Bitmap getChildDrawingCache(final View child) {
    Bitmap bitmap = child.getDrawingCache();
    if (bitmap == null) {
        child.setDrawingCacheEnabled(true);
        child.buildDrawingCache();
        bitmap = child.getDrawingCache();
    }
    return bitmap;
}

private LightingColorFilter calculateLight(final float rotation) {
    final double cosRotation = Math.cos(Math.PI * rotation / 180);
    int intensity = AMBIENT_LIGHT + (int) (DIFFUSE_LIGHT * cosRotation);
    int highlightIntensity = (int) (SPECULAR_LIGHT * Math.pow(cosRotation,
            SHININESS));
    if (intensity > MAX_INTENSITY) {
        intensity = MAX_INTENSITY;
    }
    if (highlightIntensity > MAX_INTENSITY) {
        highlightIntensity = MAX_INTENSITY;
    }
    final int light = Color.rgb(intensity, intensity, intensity);
    final int highlight = Color.rgb(highlightIntensity, highlightIntensity,
            highlightIntensity);
    return new LightingColorFilter(light, highlight);
}

【问题讨论】:

    标签: android listview animation folding


    【解决方案1】:

    JazzyListView

    有很多与你想要的相似的东西,如果不是你想要的。看看它们是如何在爵士效果和混搭下定义的。我认为反向飞行或翻转可能接近你想要的。

    【讨论】:

    • 谢谢伙计。看起来很有希望。我一定会看看这个!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2019-11-10
    • 1970-01-01
    相关资源
    最近更新 更多