2016-05-16 88 views
3

我從RatingBar android - custom draw runtime的Android SetTint不再工作

此代碼它的工作這之前,我用來改變圖形的顏色在我的等級控制:

   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%確定,但因爲我不知道我是否立即發現問題或者是否已經在這裏更長。

我對華爲的Android P6 4.4.2 tesing

的gradle我正在尋找像他:

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

這就是爲什麼我最好的鉛該代碼不再有效 - 但如果我遺漏了某些可能導致與API版本無關的明顯內容,我會接受各種建議。

---注意---

此代碼出現着色圖形稍深:

     final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable(); 
        Drawable myWrap = layerDrawable.getDrawable(2); 
        myWrap = DrawableCompat.wrap(myWrap); 
        DrawableCompat.setTint(myWrap, color); 

此代碼沒有任何影響:

     final LayerDrawable layerDrawable = (LayerDrawable) vthf.rating.getProgressDrawable(); 
        Drawable myWrap = layerDrawable.getDrawable(2); 
        DrawableCompat.setTint(myWrap, color); 

不知道是什麼以上所述。也許這個問題是由我不明白的其他東西引起的。如果圖片變暗,人們會認爲它有點不錯...但是不管他的顏色如何,它都是完全一樣的變黑 - 至少據我的眼睛可以看出。

+0

[http://stackoverflow.com/a/30876871/4049612](http://stackoverflow.com/a/30876871/4049612)檢查此 – Krishna

+0

我相信我已經嘗試了所有這些(包裝,settintlist,settint,invalidateself)還是我錯過了什麼? – Tom

+0

@Krishna你是否建議像這樣做「layerDrawable.setDrawable(2,myWrap);」 ?它崩潰了(並且之前沒有必要) – Tom

回答

2

AppCompat 23.3中的某些內容打破了我之前的解決方案。無論如何,你可以讓你的等級吧回到剛剛通過避免DrawableCompat#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; 
    } 
}); 

3

當您用DrawableCompat包裝Drawable時,會創建一個新的DrawableWrapper。您需要將新的drawable設置爲您的組件。

在你的代碼,你需要添加這樣somethink(只是setTint後):

layerDrawable.setCompoundDrawablesWithIntrinsicBounds(myWrap, null, null, null); 
+0

我不知道如何解決vthf.rating.getProgressDrawable()中的第二個索引drawable。因爲它崩潰時,我嘗試layerDrawable.setDrawable(2,myWrap); – Tom

+0

另外,Android Studio無法解析layerDrawable.setCompoundDrawablesWithIntrinsicBounds ... – Tom

3

我使用這樣的設置顏色。也可能在這裏工作,從視圖繼承的所有對象應該能夠做到這一點。這種情況下使用ContextCompat來獲取顏色,但有更多的方法來獲得你想要的顏色。

View view = (findViewById(R.id.text_view_location)); 
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.wantedColor));