2016-07-20 18 views
1

einstellen Ich habe ein GIF-Bild in meiner App und möchte dies zu meinem Vollbild-Hintergrund. Ich benutze width und height = passe Eltern an, aber es wird nicht Vollbild. Hier ist mein Code:Wie kann man Vollbild Hintergrund Gif in Android Studio

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_page); 
    GifView pGif = (GifView) findViewById(R.id.gif); 
    pGif.setImageResource(R.drawable.bbbb); 
}} 

und dies ist meine Klasse:

public class GifView extends View { 


    private static final int DEFAULT_MOVIEW_DURATION = 1000; 

    private int mMovieResourceId; 
    private Movie mMovie; 

    private long mMovieStart = 0; 
    private int mCurrentAnimationTime = 0; 

    @SuppressLint("NewApi") 
    public GifView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     /** 
     * Starting from HONEYCOMB have to turn off HardWare acceleration to draw 
     * Movie on Canvas. 
     */ 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
     } 
    } 

    public void setImageResource(int mvId){ 
     this.mMovieResourceId = mvId; 
     mMovie = Movie.decodeStream(getResources().openRawResource(mMovieResourceId)); 
     requestLayout(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     if(mMovie != null){ 
      setMeasuredDimension(mMovie.width(), mMovie.height()); 
     }else{ 
      setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight()); 
     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     if (mMovie != null){ 
      updateAnimtionTime(); 
      drawGif(canvas); 
      invalidate(); 
     }else{ 
      drawGif(canvas); 
     } 
    } 

    private void updateAnimtionTime() { 
     long now = android.os.SystemClock.uptimeMillis(); 

     if (mMovieStart == 0) { 
      mMovieStart = now; 
     } 
     int dur = mMovie.duration(); 
     if (dur == 0) { 
      dur = DEFAULT_MOVIEW_DURATION; 
     } 
     mCurrentAnimationTime = (int) ((now - mMovieStart) % dur); 
    } 

    private void drawGif(Canvas canvas) { 
     mMovie.setTime(mCurrentAnimationTime); 
     mMovie.draw(canvas, 0, 0); 
     canvas.restore(); 
    } 

} 

und xml:

<com.example.fabulous.comic.GifView 
     android:fitsSystemWindows="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/gif"/> 
+0

Bitte posten Sie Ihre XML-Code auch! –

+0

für den ganzen Bildschirm müssen Sie unterschiedliche Auflösung GIF-Bilder für es, weil GifView hat keine Methode für fixXY wie Bildansicht – iAndroid

+0

Seien Sie auch vorsichtig mit der Bildgröße! Wenn die Größe zu groß ist, kann die Aktivität abstürzen. Erwägen Sie die Verwendung von ** BitmapFactory ** oder etwas Gleiches für die Komprimierung des Bildes –

Antwort

0

Sie können dies mit Hilfe von glide

Es ist ein ist Bildladen und Caching-Bibliothek für Android konzentrierte sich auf reibungslose Scrolling

Glide unterstützt fetching, Dekodierung und Anzeige von Videostills, Bilder und animierte GIFs

Sie Glide mit diesen

compile 'com.github.bumptech.glide:glide:3.7.0' 
compile 'com.android.support:support-v4:19.1.0' 

xml verwenden können:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/relative" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" /> 

    <!--Rest of your coding--> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 
</FrameLayout> 

java

ImageView img=(ImageView)findViewById(R.id.img); 

Glide.with(Gif.this).load(R.drawable.giphy).asGif().into(img); 

ps: Ich habe den Code nicht getestet

+0

Danke für die Beantwortung. aber kann Glide nicht erkennen .... – erfan

+0

sieh dir das an :(: – erfan

+0

https://postimg.org/image/5ozh45cj5/ – erfan

0

Versuchen Sie es mit WebView, Layout Ihrer Ansicht mit einfachen html mit Ihrem Hintergrund als die gif setzen sie dann auf Ihr Vermögen Verzeichnis und laden Sie es mit WebView . Ich habe das auf meinem anderen Projekt versucht, wahrscheinlich nicht die beste Lösung, aber es funktioniert und ganz einfach.

0

In Datei build.gradle (Modul) in Abhängigkeiten:

dependencies { 
    compile 'com.github.bumptech.glide:glide:3.7.0' 
    compile 'com.android.support:support-v4:19.1.0' 
} 

Für eine einfache Ansicht:

@Override public void onCreate(Bundle savedInstanceState) { 
    ... 
    ImageView imageView = (ImageView) findViewById(R.id.my_image_view); 

    Glide.with(this).load("*link to image*").into(imageView); 
} 

Set in allen Layouts um Bild:

android:layout_width="match_parent" 
android:layout_height="match_parent"