Android:垂直 3d ListView

标签 android listview 3d imagebutton

我想制作垂直 3d ListView like here ,但对于 ImageButtons 和免费。是否有任何库或示例代码?我是Android开发的新手,所以我不知道如何为这样的东西制作动画

最佳答案

其实很简单。您需要扩展 ListView 并覆盖 onDrawChild()。在那里你可以应用 3d 转换矩阵来获得你想要的效果。 我的 github 上有一个工作示例 或者你可以看看这个 question这非常相似。

为方便起见,这是我对 3d ListView 的实现:

public class ListView3d extends ListView {

    /** Ambient light intensity */
    private static final int AMBIENT_LIGHT = 55;
    /** Diffuse light intensity */
    private static final int DIFFUSE_LIGHT = 200;
    /** Specular light intensity */
    private static final float SPECULAR_LIGHT = 70;
    /** Shininess constant */
    private static final float SHININESS = 200;
    /** The max intensity of the light */
    private static final int MAX_INTENSITY = 0xFF;

    private final Camera mCamera = new Camera();
    private final Matrix mMatrix = new Matrix();
    /** Paint object to draw with */
    private Paint mPaint;

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

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        // get top left coordinates
        final int top = child.getTop();
        final int left = child.getLeft();
        Bitmap bitmap = child.getDrawingCache();
        if (bitmap == null) {
            child.setDrawingCacheEnabled(true);
            child.buildDrawingCache();
            bitmap = child.getDrawingCache();
        }

        final int centerY = child.getHeight() / 2;
        final int centerX = child.getWidth() / 2;
        final int radius = getHeight() / 2;
        final int absParentCenterY = getTop() + getHeight() / 2;
        final int absChildCenterY = child.getTop() + centerX;
        final int distanceY = absParentCenterY - absChildCenterY;
        final int absDistance = Math.min(radius, Math.abs(distanceY));

        final float translateZ = (float) Math.sqrt((radius * radius) - (absDistance * absDistance));

        double radians = Math.acos((float) absDistance / radius);
        double degree = 90 - (180 / Math.PI) * radians;

        mCamera.save();
        mCamera.translate(0, 0, radius - translateZ);
        mCamera.rotateX((float) degree); // remove this line..
        if (distanceY < 0) {
            degree = 360 - degree;
        }
        mCamera.rotateY((float) degree); // and change this to rotateX() to get a
                                            // wheel like effect
        mCamera.getMatrix(mMatrix);
        mCamera.restore();

        // create and initialize the paint object
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setFilterBitmap(true);
        }
        //highlight elements in the middle
        mPaint.setColorFilter(calculateLight((float) degree));

        mMatrix.preTranslate(-centerX, -centerY);
        mMatrix.postTranslate(centerX, centerY);
        mMatrix.postTranslate(left, top);
        canvas.drawBitmap(bitmap, mMatrix, mPaint);
        return false;
    }

    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:垂直 3d ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025270/

相关文章:

android - 如何将项目从 bitbucket 存储库导入到 Android Studio?

.net - 如何将数据绑定(bind)到 ListView 中的多个列?

android - 当在 ListView 中滚动更新数据更改为以前的数据

3d - 如何展平选定的点

python - 如何使用matplotlib根据条形高度绘制具有特定颜色的3D条形图

android - 强制 Android DateUtils.getRelativeDateTimeString() 忽略设备区域设置?

android - Android 是否支持 SNMP?

wpf - 在 WPF 和 WP8 中实现 ISupportIncrementalLoading

Android 3D 曲面图

javascript - 如何从 Android Webview 应用程序中删除拉伸(stretch)效果?