【问题标题】:How to create ripple with corner radius programmatically?如何以编程方式创建具有圆角半径的波纹?
【发布时间】:2018-11-15 11:11:41
【问题描述】:

有一些类似的问题。但他们没有解决!

Set RippleDrawable corner radius programmatically

我知道这种行为可以做到using xml file。但我必须以编程方式进行。

我的代码如下:

    GradientDrawable gd = new GradientDrawable();
    gd.setColor(Color.YELLOW);
    gd.setStroke(2, Color.RED);
    gd.setCornerRadius(45.0f);
    view.setBackground(gd);

    int[] colors = new int[]{Color.GRAY};
    int[][] states = new int[][]{ new int[]{}};
    ColorStateList stateList = new ColorStateList(states, colors);

    Drawable mask = getResources().getDrawable(R.drawable.icon1);
    RippleDrawable rippleDrawableBackgorund = new RippleDrawable(stateList, view.getBackground(), mask);

    view.setBackground(rippleDrawableBackgorund);

按下状态的截图如下:

如何为 RippleDrawable 设置边框半径?此外,rippleDrawableBackgorund.setRadius 将这种效果更改为悬停状态。

【问题讨论】:

标签: android-layout ripple android-background rippledrawable


【解决方案1】:

本指南可以节省我的时间 :) http://blog.blundellapps.co.uk/tut-programmatically-create-a-rippledrawable-of-any-color/

使用 RippleDrawable 构造函数的 mask 参数。

private static Drawable getRippleColor(int color) {
    float[] outerRadii = new float[8];
    Arrays.fill(outerRadii, 3);
    RoundRectShape r = new RoundRectShape(outerRadii, null, null);
    ShapeDrawable shapeDrawable = new ShapeDrawable(r);
    shapeDrawable.getPaint().setColor(color);
    return shapeDrawable;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    相关资源
    最近更新 更多