如何更改过度滚动边缘的颜色和过度滚动发光

时间:2014-12-07 13:13:33

标签: android android-5.0-lollipop overscroll

如何更改过度滚动边缘的颜色和过度滚动发光或如何更改android release 5.0 lollipop的白色(默认颜色)?

4 个答案:

答案 0 :(得分:53)

过度滚动发光颜色继承了整个应用中android:colorPrimary设置的主要颜色值。但是如果您需要指定不同的值,只需使用android:colorEdgeEffect

<style name="MyAppTheme" parent="***">
   <item name="android:colorEdgeEffect">@color/my_color</item>
</style>

答案 1 :(得分:24)

LOLLIPOP上,边缘光晕继承自colorPrimary。创建视图后,只能通过反射更改边缘发光颜色。当您使用Palette动态加载颜色时,此功能非常有用。

编辑:TL; DR:从这里下载全班: https://github.com/consp1racy/android-commons/blob/master/commons/src/main/java/net/xpece/android/widget/XpEdgeEffect.java

PROGUARD SETUP:如果您要在支持库的小部件上使用此功能,则需要保留字段名称。最快的方法是以下(虽然仍然浪费):

-keepclassmembers class * extends android.view.View {
    <fields>;    
}

-keepclassmembers class android.support.v4.widget.EdgeEffectCompat {
    <fields>;    
}

使用以下代码创建实用程序类:

private static final Class<?> CLASS_SCROLL_VIEW = ScrollView.class;
private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;

private static final Class<?> CLASS_LIST_VIEW = AbsListView.class;
private static final Field LIST_VIEW_FIELD_EDGE_GLOW_TOP;
private static final Field LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM;

static {
  Field edgeGlowTop = null, edgeGlowBottom = null;

  for (Field f : CLASS_SCROLL_VIEW.getDeclaredFields()) {
    switch (f.getName()) {
      case "mEdgeGlowTop":
        f.setAccessible(true);
        edgeGlowTop = f;
        break;
      case "mEdgeGlowBottom":
        f.setAccessible(true);
        edgeGlowBottom = f;
        break;
    }
  }

  SCROLL_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
  SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;

  for (Field f : CLASS_LIST_VIEW.getDeclaredFields()) {
    switch (f.getName()) {
      case "mEdgeGlowTop":
        f.setAccessible(true);
        edgeGlowTop = f;
        break;
      case "mEdgeGlowBottom":
        f.setAccessible(true);
        edgeGlowBottom = f;
        break;
    }
  }

  LIST_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
  LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setEdgeGlowColor(AbsListView listView, int color) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    try {
      EdgeEffect ee;
      ee = (EdgeEffect) LIST_VIEW_FIELD_EDGE_GLOW_TOP.get(listView);
      ee.setColor(color);
      ee = (EdgeEffect) LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(listView);
      ee.setColor(color);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setEdgeGlowColor(ScrollView scrollView, int color) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    try {
      EdgeEffect ee;
      ee = (EdgeEffect) SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
      ee.setColor(color);
      ee = (EdgeEffect) SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
      ee.setColor(color);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

答案 2 :(得分:8)

如果您正在使用最新的RecyclerView实现,则以编程方式更改溢出颜色非常简单。使用以下代码(Kotlin实现):

recyclerView.edgeEffectFactory = object : RecyclerView.EdgeEffectFactory() {
    override fun createEdgeEffect(view: RecyclerView, direction: Int): EdgeEffect {
        return EdgeEffect(view.context).apply { setColor(color) }
    }
}

请注意,这仅适用于API级别21(Lollipop)及更高版本。如果您在编译时知道该值,请使用Ahmed指出的colorEdgeEffect

答案 3 :(得分:1)

在棒棒糖之前,发光效果实际上是嵌入在操作系统资源中的Drawable,您可以在其上应用ColorFilter:

public static void changeOverScrollGlowColor(Resources res, int colorID ) {
    try {
        final int glowDrawableId = res.getIdentifier("overscroll_glow", "drawable", "android");
        final Drawable overscrollGlow = res.getDrawable(glowDrawableId);
        overscrollGlow.setColorFilter(res.getColor(colorID), android.graphics.PorterDuff.Mode.SRC_ATOP);

        final int edgeDrawableId = res.getIdentifier("overscroll_edge", "drawable", "android");
        final Drawable overscrollEdge = res.getDrawable(edgeDrawableId);
        overscrollEdge.setColorFilter(res.getColor(colorID), android.graphics.PorterDuff.Mode.SRC_ATOP);
    } catch (Exception ignored) {
    }
}

在onCreate中调用一次就足够了。

changeOverScrollGlowColor(getResources(), R.color.colorPrimary);