在onClick中为View添加涟漪效果

问题描述 投票:15回答:5

您好我正在尝试为View添加一个涟漪效果onClick方法,但这个没有用。我的所有物品都有身份证,但我不知道怎么称呼它

这是一个代码。

 @Override
public void onClick(View v) {
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    v.setBackgroundResource(backgroundResource);

    switch (v.getId()) {
        case ACTION_PLAY_ID:
            Log.d(MainActivity.TAG, getString(R.string.detail_action_play));
            v.setBackgroundResource(backgroundResource);
            Intent intent = new Intent(getActivity(), PlayerActivity.class);
            intent.putExtra(Video.VIDEO_TAG, videoModel);
            startActivity(intent);

            break;
        case ACTION_BOOKMARK_ID:
            if (bookmarked) {
                v.setBackgroundResource(backgroundResource);
                deleteFromBookmarks();
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star_outline));
            } else {
                v.setBackgroundResource(backgroundResource);
                addToBookmarks();
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star));
            }
            break;
        case ACTION_REMINDER_ID:
            if (!isReminderSet) {
                createReminderDialog((ImageView) v);
            } else {
                cancelReminder(liveTvProgram.getProgramId());
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.alarm));
            }
            break;
    }
}

对于Lubomir

我有类似的东西,但也没有工作:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
        savedInstanceState) {
    View view = inflater.inflate(R.layout.item_detail, container, false);
    ButterKnife.bind(this, view);

    View myView = view.findViewById(R.id.actions_container);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    myView.setBackgroundResource(backgroundResource);

    loadImage();
    init();

    return view;
}

ImageViews(actionbuttons)正在java中为LinearLayout actions_container创建

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <ImageView
        android:id="@+id/header_image"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="@dimen/detail_image_1_state"
        android:elevation="8dp"/>

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/detail_bottom_margin"
        android:layout_marginTop="@dimen/detail_top_margin"
        android:background="@color/primary_color">

        <LinearLayout
            android:id="@+id/actions_container"
            android:layout_width="match_parent"
            android:layout_height="@dimen/detail_actions_height"
            android:layout_alignParentTop="true"
            android:background="@drawable/ripple_effect_image"
            android:elevation="2dp"
            android:orientation="horizontal"
            android:paddingLeft="300dp"
            android:paddingStart="300dp"/>

        <LinearLayout
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/actions_container"
            android:orientation="vertical"
            android:paddingLeft="300dp"
            android:paddingStart="300dp">

            <TextView
                android:id="@+id/title"
                style="@style/TextTitleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/subtitle"
                style="@style/TextSubtitleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/duration"
                style="@style/TextSubtitleStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/season"
                style="@style/TextDescriptionStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/episode"
                style="@style/TextDescriptionStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/description"
                style="@style/TextDescriptionStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="7"/>

        </LinearLayout>

        <FrameLayout
            android:id="@+id/recommended_frame"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true">

            <android.support.v17.leanback.widget.HorizontalGridView
                android:id="@+id/recommendation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"/>
        </FrameLayout>

        <TextView
            android:id="@+id/recommended_text"
            style="@style/TextHeaderStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/recommended_frame"
            android:text="@string/related_programs"/>

    </RelativeLayout>


</RelativeLayout>

我的xml涟漪效果文件也是这样的:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/dark_primary_color">
    <item>
        <color android:color="@color/dark_primary_color" />
    </item>
    <item android:id="@android:id/mask">
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>
java android television
5个回答
45
投票

可点击的视图

一般情况下,常规按钮的涟漪效果默认在API 21中有效,对于其他可触摸视图,可以通过指定

android:background="?android:attr/selectableItemBackground"

在代码中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    View myView = findViewById(R.id.myView);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    myView.setBackgroundResource(backgroundResource);
}

3
投票

创建涟漪背景

view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/blue" >
    <item android:drawable="@drawable/view_normal">
    </item>
</ripple>

view_noraml.xml //这是您正常查看的方式

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:radius="@dimen/button_corner"/>
    <solid
        android:color="@android:color/transparent"/>
    <stroke
        android:width="0.5dp"
        android:color="@color/white"/>

</shape>

现在将view_background设置为您的视图

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:foreground="@drawable/view_background"
    android:clickable="true"
    android:focusable="true"
    >
    <ImageView
        android:id="@+id/grid_item_imageView"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_gravity="center"
        android:scaleType="centerInside"
        />
</FrameLayout>

2
投票

你可以加:

 <ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:clickable="true"
  android:background="@drawable/ripple"/>   

1
投票

解决方案对我来说很简单。

这是涟漪效应:

    <?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#BFB3F7">
    <item android:id="@android:id/mask">
        <shape android:shape="oval">
            <solid android:color="@color/button_background_color" />
        </shape>
    </item>
</ripple>

然后在类上我需要搜索函数setBackground

然后我需要声明一个drawable项目。这样的事情:

 @Override
public void onFocusChange(View v, boolean hasFocus) {

    if (hasFocus) {
        v.setBackground(res.getDrawable(R.drawable.ripple_effect_for_buttons));
        scrollContainer(false);
    } else {
        v.setBackground(null);
        if (recommendation.getFocusedChild() != null) {
            scrollContainer(true);
        }

    }
}

和YUPII一起工作


1
投票

Lubomir Babev's answer所述,添加android:background="?android:attr/selectableItemBackground"就可以了。

但是,如果您的视图已有背景,则可以在android:foreground属性上使用相同的内容:

android:background="@color/anyColor"
android:foreground="?android:attr/selectableItemBackground"

android:foreground仅受API 23+支持。

© www.soinside.com 2019 - 2024. All rights reserved.