Android开发fadingEdge和fadingEdgeLength设置及屏蔽顶部阴影设置

设置fadingEdgeLength及屏蔽顶部阴影

设置fadingEdge和fadingEdgeLength的效果图(上下阴影遮挡,也可设置左右阴影遮挡)
在这里插入图片描述

Android中所有的view都可设置,一般是可滚动的view设置了才有效果,不滚动的一般无效
例如(xml文件设置) : 设置fadingEdge和fadingEdgeLength :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
        android:background="@color/white"
    android:layout_height="match_parent">
    <android.support.v4.widget.NestedScrollView
        android:layout_marginTop="@dimen/lay_20"
        android:layout_marginRight="@dimen/lay_20"
        android:layout_marginLeft="@dimen/lay_20"
        android:id="@+id/activity_shop_terms_scroll"
        android:layout_above="@id/activity_shop_terms_bottom"
        android:layout_width="match_parent"
        
        android:requiresFadingEdge="vertical"
        android:fadingEdge="vertical"
        android:fadingEdgeLength="@dimen/lay_50"
        
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/activity_shop_terms_details"
            android:textSize="@dimen/font_15"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.NestedScrollView>
</RelativeLayout>

屏蔽顶部阴影

现在我想要的效果是只要下面的阴影,想屏蔽掉顶部只留下底部的阴影

在这里插入图片描述

发现这个没有可用的api设置,只能通过我们自己想办法实现
1.先找到源码里哪里使用了,通过NestedScrollView及子类哪里解析了fadingEdgeLength这个属性,发现View.java文件里有解析

这里解析并赋值

(android-28/view.java)

 protected void initializeFadingEdgeInternal(TypedArray a) {	//这里解析值
        initScrollCache();
        mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
                R.styleable.View_fadingEdgeLength,
                ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
    }

在draw方法里找到 根据值设置并画出图形

 public void draw(Canvas canvas) {
      		......	//省略代码
      	final ScrollabilityCache scrollabilityCache = mScrollCache;
        final float fadeHeight = scrollabilityCache.fadingEdgeLength;
        int length = (int) fadeHeight;
        // clip the fade length if top and bottom fades overlap
        // overlapping fades produce odd-looking artifacts
        if (verticalEdges && (top + length > bottom - length)) {
            length = (bottom - top) / 2;
        }

        // also clip horizontal fades if necessary
        if (horizontalEdges && (left + length > right - length)) {
            length = (right - left) / 2;
        }

        if (verticalEdges) {	//(纵向)这里为设置阴影长度代码  
            topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
            drawTop = topFadeStrength * fadeHeight > 1.0f;
            bottomFadeStrength = Math.max(
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过继承 Preference 类来实现自定义布局,并在其中添加一个 Button,然后为该 Button 添加点击事件。下面是一个简单的示例代码: ```java public class CustomPreference extends Preference { private Button mButton; public CustomPreference(Context context, AttributeSet attrs) { super(context, attrs); setWidgetLayoutResource(R.layout.custom_preference); } @Override public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); mButton = (Button) holder.findViewById(R.id.custom_button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 } }); } } ``` 在布局文件 `custom_preference.xml` 中添加一个 Button: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:gravity="center_vertical" android:paddingStart="?android:attr/listPreferredItemPaddingStart" android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"> <TextView android:id="@android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItem" android:singleLine="true" android:ellipsize="marquee" android:fadingEdge="horizontal" android:textColor="@android:color/primary_text_light" /> <Button android:id="@+id/custom_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> ``` 最后,在设置界面的 XML 文件中使用自定义的 Preference: ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> ... <com.example.CustomPreference android:key="custom_preference" android:title="Custom Preference" /> </PreferenceScreen> ``` 这样就可以在设置界面中显示一个自定义布局的 Preference,其中包含一个 Button,并且为该 Button 添加了点击事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值