首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在触摸时更改ImageView可绘制矢量颜色

在触摸时更改ImageView可绘制矢量颜色
EN

Stack Overflow用户
提问于 2017-01-23 23:21:04
回答 1查看 188关注 0票数 0

我的计划是当用户触摸行时,在我的RecyclerView适配器中更改ImageView的矢量颜色。我只想更改触摸到的行的颜色。我已经写了一个代码来完成这项工作,但是我得到了一个错误:

代码语言:javascript
复制
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference

我已经尝试了很多,但错误总是一个问题。

这是我的RecyclerView行xml:

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:focusable="true"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">

    <!-- Setting icon -->
    <ImageView
        android:id="@+id/settingImage"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_marginRight="8dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:contentDescription="Icon"
        android:src="@mipmap/ic_launcher" />

    <!-- Setting title -->
    <TextView
        android:id="@+id/settingTitle"
        android:textColor="@color/colorBlack"
        android:layout_width="match_parent"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="14dp"
        android:textSize="16dp"
        android:layout_height="wrap_content" />

    <!-- Setting subtitle -->
    <TextView
        android:id="@+id/settingSubtitle"
        android:layout_below="@id/settingTitle"
        android:layout_marginBottom="14dp"
        android:layout_marginLeft="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是我想要更改填充颜色的矢量图形:

代码语言:javascript
复制
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="@color/iconGray"
        android:pathData="M10.09 15.59L11.5 17l5-5-5-5-1.41 1.41L12.67 11H3v2h9.67l-2.58 2.59zM19 3H5c-1.11 0-2 .9-2 2v4h2V5h14v14H5v-4H3v4c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z" />
</vector>

这是我想要在单击时更改颜色的适配器:

代码语言:javascript
复制
// Get setting holder type
holder.type = setting.getType();

// OnTouchListener for holder/vector color change
holder.itemView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Define setting holder
        MySettingHolder holder = (MySettingHolder) (v.getTag());

        // Set view for getting settingImage
        View view = (View)v.getParent();

        // ImageView for changing color
        ImageView imageView = (ImageView) v.findViewById(R.id.settingImage);

        // Define switch for line
        switch (holder.type) {
            // Case 1 = Logout
            case 1:
                // Change ImageView color
                DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(context, R.color.colorPrimary));
                break;

            case 2:

                break;

            default:

                break;
        }

        return false;
    }
});

EN

回答 1

Stack Overflow用户

发布于 2017-01-23 23:57:06

解决方案是改变上下文:

旧的:

代码语言:javascript
复制
DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(context, R.color.colorPrimary));

新的:

代码语言:javascript
复制
DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(v.getContext(), R.color.iconGray));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41809706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档