Android SetTint不再工作
我有这个密码 在此之前,我曾在评级控件上更改图形的颜色:Android SetTint不再工作,android,drawable,Android,Drawable,我有这个密码 在此之前,我曾在评级控件上更改图形的颜色: vthf.rating.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACT
vthf.rating.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//--
float touchPositionX = event.getX();
float width = vthf.rating.getWidth();
float starsf = (touchPositionX / width);
starsf = starsf * param_final__data.score_max;
int starsint = (int) Math.round(starsf);
byte starsbyte = (byte) starsint;
param_final__data.score_cur = starsbyte;
starsf = starsint;
vthf.rating.setRating(starsf);
//--
int color = Color.BLACK;
switch (starsbyte % 4) {
case 0: color = Color.BLUE; break; // 4 stars
case 3: color = Color.GREEN; break;
case 2: color = Color.YELLOW; break;
case 1: color = Color.RED; break;
}
final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
Drawable myWrap = layerDrawable.getDrawable(2);
//--
//
//--
myWrap = DrawableCompat.wrap(myWrap);
//myWrap = myWrap.mutate();
//--
// myWrapMutate.setColorFilter(color, PorterDuff.Mode.SRC_IN);
// DrawableCompat.setTintList(myWrap, ColorStateList.valueOf(color));
//--
DrawableCompat.setTint(myWrap, color);
//--
vthf.rating.invalidate();
}
else
if (event.getAction() == MotionEvent.ACTION_DOWN) {
param_final__view.setPressed(true);
}
else
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
param_final__view.setPressed(false);
}
return true;
}
});
我相信在我从Android Studio 1.5.1和所有支持库(不确定是哪个版本)升级后,它就停止工作了——我不是100%确定,因为我不确定我是否立即解决了这个问题,或者它在这里的时间是否更长
我正在测试华为P6安卓4.4.2
我的格拉德尔看起来像他的:
compileSdkVersion 23
buildToolsVersion '23'
dependencies {
compile 'com.android.support:support-v4:+'
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:appcompat-v7:+'
}
我的舱单上有这个
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17"
/>
此代码无效:
final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable();
Drawable myWrap = layerDrawable.getDrawable(2);
DrawableCompat.setTint(myWrap, color);
不确定如何理解上述内容。也许问题是由我不理解的其他原因引起的。如果图形变暗,人们会认为它有点工作。。。但无论颜色如何,它都是完全相同的变暗-至少就我的眼睛所知。当你用DrawableCompat包装你的DrawableWrapper时,一个新的DrawableWrapper就诞生了。您需要将新的drawable设置为组件 在代码中,您需要添加如下内容(在setTint之后):
我用这样的东西来设置颜色。可能在这里也可以工作,从视图中继承它的所有对象都应该能够做到这一点。本例使用ContextCompat获得颜色,但是有更多的方法可以获得您想要的颜色
View view = (findViewById(R.id.text_view_location));
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.wantedColor));
AppCompat 23.3中的某些内容打破了我以前的解决方案。无论如何,只要避免使用
DrawableCompat\35; Wrap
,您就可以让您的评级栏恢复工作:
ratingBarB.setOnTouchListener(new View.OnTouchListener() {
private int lastColoredProgress = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
int progress = ratingBarB.getProgress();
if (progress != lastColoredProgress) {
lastColoredProgress = progress;
int color;
switch (lastColoredProgress) {
case 0:
case 1:
color = Color.RED;
break;
case 2:
case 3:
color = Color.YELLOW;
break;
case 4:
default:
color = Color.GREEN;
break;
}
final Drawable drawable = ratingBarB.getProgressDrawable();
if (drawable instanceof LayerDrawable) { // Lollipop and newer
final LayerDrawable layerDrawable = (LayerDrawable) drawable;
DrawableCompat.setTint(layerDrawable.getDrawable(2), color);
} else {
DrawableCompat.setTint(drawable, color);
}
}
return false;
}
});
检查此项我相信我已经尝试了所有这些(wrap、settinlist、settint、invalidateself)或者我遗漏了什么吗?@Krishna你是否建议做类似于“layerDrawable.setDrawable(2,myWrap);”的事情?它崩溃了(之前没有必要),我已经用更多的信息扩展了这个问题,停止使用
:+
版本标识符作为渐变依赖项。新版本将破坏构建,就像在本例中一样。我不确定如何修复vtf.rating.getProgressDrawable()中的第二个可绘制索引;因为当我尝试layerDrawable.setDrawable(2,myWrap)时它崩溃了;此外,Android Studio无法解析layerDrawable.SetCompoundDrawable SwithinInstincBounds。。。
View view = (findViewById(R.id.text_view_location));
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.wantedColor));
ratingBarB.setOnTouchListener(new View.OnTouchListener() {
private int lastColoredProgress = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
int progress = ratingBarB.getProgress();
if (progress != lastColoredProgress) {
lastColoredProgress = progress;
int color;
switch (lastColoredProgress) {
case 0:
case 1:
color = Color.RED;
break;
case 2:
case 3:
color = Color.YELLOW;
break;
case 4:
default:
color = Color.GREEN;
break;
}
final Drawable drawable = ratingBarB.getProgressDrawable();
if (drawable instanceof LayerDrawable) { // Lollipop and newer
final LayerDrawable layerDrawable = (LayerDrawable) drawable;
DrawableCompat.setTint(layerDrawable.getDrawable(2), color);
} else {
DrawableCompat.setTint(drawable, color);
}
}
return false;
}
});