2017-09-17 3 views
4

Das ist mein build.gradleKann nicht AnimatedVectorDrawableCompat in Nougat werfen

defaultConfig { 
    ... 
    minSdkVersion 21 
    targetSdkVersion 26 
    vectorDrawables.useSupportLibrary = true 
} 

und ein Teil des Layouts

<ImageView 
    android:id="@+id/recents" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="?attr/selectableItemBackground" 
    android:clickable="true" 
    android:scaleType="fitCenter" 
    app:srcCompat="@drawable/anim_test"/> 

und die Klasse Besetzung:

val np = convertView.findViewById<ImageView>(R.id.recents) 
val anim = np.drawable as AnimatedVectorDrawableCompat 

Dies funktioniert wie erwartet auf Lolipop (sdk 21), scheitert aber am Nougat-Sprichwort:

android.graphics.drawable.AnimatedVectorDrawable cannot be cast to android.support.graphics.drawable.AnimatedVectorDrawableCompat 

Was ich nicht bekomme ist, warum es eine AnimatedVectorDrawableCompat auf SDK Ebene 21 überhaupt zurückgibt, wenn AnimatedVectorDrawable bereits vom System unterstützt wird. Und warum gibt es den AnimatedVectorDrawable in Nougat trotz Angabe vectorDrawables.useSupportLibrary = true zurück.

+1

gleiche Problem mit Support-Bibliothek 26.0.1. Gelöst durch eine Problemumgehung: Eine Bedingung zur Laufzeit - Sie müssen AnimatedVectorDrawable für Lollipop und höher und AnimatedVectorDrawableCompat für vorherigen Lollipop umwandeln. – Fllo

Antwort

2

beschäftige ich mich es so:

public class MainActivity extends AppCompatActivity { 

    ImageView img; 
    Button show,play,stop; 
    AnimatedVectorDrawableCompat anim_show,anim_play,anim_stop; 
    Object canim_show,canim_play,canim_stop; 



    static { 
     AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     img = findViewById(R.id.img); 

     show = findViewById(R.id.show); 
     play = findViewById(R.id.play); 
     stop = findViewById(R.id.stop); 


     if(Build.VERSION.SDK_INT<21){ 
      anim_show = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_show_animated_vector); 
      anim_play = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_play_animated_vector); 
      anim_stop = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_stop_animated_vector); 

     }else{ 
      canim_show = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_show_animated_vector); 
      canim_play = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_play_animated_vector); 
      canim_stop = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_stop_animated_vector); 

     } 


     show.setOnClickListener(new View.OnClickListener() { 
      @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
      @Override 
      public void onClick(View view) { 
       if(Build.VERSION.SDK_INT<21){ 
        img.setImageDrawable(anim_show); 
        anim_show.start(); 
       }else{ 
        img.setImageDrawable((AnimatedVectorDrawable) canim_show); 
        ((AnimatedVectorDrawable)canim_show).start(); 
       } 

      } 
     }); 





    } 
} 
Verwandte Themen