【问题标题】:How to add ripple effect to preferences in android?如何在android中为首选项添加涟漪效果?
【发布时间】:2015-11-14 09:51:36
【问题描述】:

我正在努力在触摸(选中)首选项时添加涟漪效果。我通过扩展ListPreference 自定义了我的偏好。我尝试使用RippleDrawable 以编程方式设置涟漪效果,但我没有看到动画。

这是我自定义的偏好类

public class CustomListPreference extends ListPreference {

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

        public CustomListPreference(Context context) {
            super(context);
        }

        @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            setCustomStyle(view);
        }

        private void setCustomStyle(View view) {
            TextView titleView = (TextView) view.findViewById(android.R.id.title);
            titleView.setTypeface(InitActivity.TYPEFACE_REGULAR);
            TextView summary = (TextView) view.findViewById(android.R.id.summary);
            summary.setTypeface(InitActivity.TYPEFACE_REGULAR);

            //Setting the drawable here, but it doesn't work.        
            RippleDrawable drawable = (RippleDrawable) getContext().getResources().getDrawable(R.drawable.my_ripple_background);
            view.setBackGround(drawable);
        }

} 

我的偏好布局

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- opens a subscreen of settings -->
    <com.abc.app.CustomListPreference
            android:defaultValue="1"
            android:entries="@array/sampleEntries"
            android:entryValues="@array/SampleEntryValues"
            android:key="some_preference"
            android:title="@string/some_preferences" />

    <com.abc.app.CustomCheckboxPreference
           android... />


</PreferenceScreen>

我的波纹 xml

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/light_black_overlay"> <!--#22000000-->
    <item>
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/background_light" />
        </shape>
    </item>
</ripple>

我是否为正确的视图设置动画?任何想法表示赞赏。谢谢。

【问题讨论】:

    标签: android android-preferences listpreference rippledrawable


    【解决方案1】:

    这是一个最小的完整示例,用于向扩展 ListPreference 的类添加自定义波纹效果。我刚刚使用 API 21 (5.0) 制作并测试了它。

    SettingsActivity(启动活动)

    public class SettingsActivity extends PreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            addPreferencesFromResource(R.xml.pref_general);
        }
    }
    

    pref_general.xml

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="example_checkbox"
            android:summary="a checkbox"
            android:title="Checkbox test" />
    
        <!-- replace with com.abc.app.CustomListPreference in your case-->
        <com.timcastelijns.rippletest.CustomListPreference
            android:defaultValue="1"
            android:entries="@array/sampleEntries"
            android:entryValues="@array/SampleEntryValues"
            android:key="some_preference"
            android:title="test" />
    
    </PreferenceScreen>
    

    arrays.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="sampleEntries">
            <item>1</item>
            <item>2</item>
            <item>3</item>
        </string-array>
    
        <string-array name="SampleEntryValues">
            <item>4</item>
            <item>5</item>
            <item>6</item>
        </string-array>
    </resources>
    

    CustomListPreference

    public class CustomListPreference extends ListPreference {
    
        private Context ctx;
    
        public CustomListPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
            ctx = context;
        }
    
        public CustomListPreference(Context context) {
            super(context);
            ctx = context;
        }
    
        @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            setCustomStyle(view);
        }
    
        private void setCustomStyle(View view) {
            RippleDrawable drawable = (RippleDrawable) ctx.getDrawable(R.drawable.my_ripple_background);
            view.setBackground(drawable);
        }
    }
    

    my_ripple_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@android:color/holo_blue_light">
        <item android:id="@android:id/mask">
            <color android:color="@android:color/white" />
        </item>
    </ripple>
    

    按下时会显示出淡蓝色的涟漪效果,如xml中所指定:


    我根据您的代码以及 android SDK 示例中的示例 SettingsActivity 中的代码构建了此示例。


    编辑:
    经过一段时间的聊天和尝试各种事情,我们得出的结论是问题是由OP的手机(三星S5)或它的设置引起的。当OP在模拟器中尝试代码时,一切正常。

    供参考 - 这是它在 OPs 手机中的外观:

    【讨论】:

    • 我不小心在我的代码中添加了 .xml 文件扩展名。我想我在没有文件扩展名的情况下执行了我的代码。不过我会尝试其他的 sn-ps。
    • 试过你的代码,我可以在触摸时看到蓝色背景,但它不像波纹。波纹应该从触摸点扩展到视图的侧面。
    • @prudhvi 对我来说确实如此。你用的是什么sdk?您能否包含更多代码以便我检查?尤其是你的涟漪 xml
    • 我在其他活动(不是首选项)中使用了我的波纹 xml,它运行良好,所以我认为它没有问题。我正在使用 minSdkVersion 18 和 targetSdkVersion 21。我只在 drawable-v21 文件夹中包含了波纹 xml。
    • @prudhvi 你的手机兼容 sdk 21 是吗?你能在你使用涟漪的地方包含活动代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2023-03-26
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多