繁体   English   中英

更改ImageView可绘制矢量的颜色

[英]Change ImageView drawable vector color on touch

我的计划是当用户触摸该行时,从RecyclerView适配器中的ImageView更改矢量颜色。 我只想更改触摸行的颜色。 我写了一个代码可以完成这项工作,但出现错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference

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

这是我的RecyclerView行xml:

 <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> 

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

 <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> 

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

 // 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; } }); 

解决方案是更改上下文:

旧:

 DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(context, R.color.colorPrimary)); 

新:

 DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(v.getContext(), R.color.iconGray)); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM