2017-03-20 1 views
2

Je suis en train d'ajouter un GIF en XML studio androidGIF en studio Android

Il fonctionne très bien au début, mais quand je ferme en studio Android et rouvrez à nouveau il me donne l'erreur!

En outre, j'ai essayé d'abaisser la qualité GIF et son espace, il fonctionne bien au début mais donne l'erreur après le redémarrage!

Voici mon code XML:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#fff" 
    android:orientation="vertical"> 

    <pl.droidsonroids.gif.GifImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/myimg"/> 
</LinearLayout> 

Erreur

enter image description here

+0

d'essayer de reconstruire votre projet son problème de rendu –

Répondre

0

essayer cela, travailler pour moi

Mettez votre fichier GIF dans Drawable folde r

Créer une classe GifImageView.java

public class GifImageView extends View { 

     private InputStream mInputStream; 
     private Movie mMovie; 
     private int mWidth, mHeight; 
     private long mStart; 
     private Context mContext; 

     public GifImageView(Context context) { 
     super(context); 
     this.mContext = context; 
     } 

     public GifImageView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
     } 

     public GifImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     this.mContext = context; 
     if (attrs.getAttributeName(1).equals("background")) { 
      int id = Integer.parseInt(attrs.getAttributeValue(1).substring(1)); 
      setGifImageResource(id); 
     } 
     } 


     private void init() { 
     setFocusable(true); 
     mMovie = Movie.decodeStream(mInputStream); 
     mWidth = mMovie.width(); 
     mHeight = mMovie.height(); 

     requestLayout(); 
     } 

     @Override 
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     setMeasuredDimension(mWidth, mHeight); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 

     long now = SystemClock.uptimeMillis(); 

     if (mStart == 0) { 
      mStart = now; 
     } 

     if (mMovie != null) { 

      int duration = mMovie.duration(); 
      if (duration == 0) { 
      duration = 1000; 
      } 

      int relTime = (int) ((now - mStart) % duration); 

      mMovie.setTime(relTime); 

      mMovie.draw(canvas, 0, 0); 
      invalidate(); 
     } 
     } 

     public void setGifImageResource(int id) { 
     mInputStream = mContext.getResources().openRawResource(id); 
     init(); 
     } 

     public void setGifImageUri(Uri uri) { 
     try { 
      mInputStream = mContext.getContentResolver().openInputStream(uri); 
      init(); 
     } catch (FileNotFoundException e) { 
      Log.e("GIfImageView", "File not found"); 
     } 
     } 
    } 

dans votre fichier .xml

<com.gifLoader.GifImageView // **of your GifImageView.java** 
     android:id="@+id/GifImageView" 
     android:layout_height="@dimen/txt_size_1" 
     android:layout_width="@dimen/txt_size_1" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="@dimen/sb_progrees_margin_lr" 
     android:layout_centerInParent="true" /> 

et vous activité r

GifImageView gifImageView = (GifImageView) findViewById(R.id.GifImageView); 
gifImageView.setGifImageResource(R.drawable.ring); 
+0

ce "setGifImageResource" fait référence? –

+0

Salut, que signifie dans votre xml? –

+0

votre fichier .xml où vous voulez afficher @VitaliPetrov –