2016-01-21 46 views
-1

所以我在我的應用程序中使用GIF。但是當我在其他設備上安裝應用程序時,GIF不適合屏幕,或者它不在應該放置的位置。任何人都有解決方案嗎?問題與GIF圖像

+3

請發表您的代碼 –

+0

XML或Class? – Cyril

+1

他們倆。元素的位置可以直接在xml中設置,但也可以編程 –

回答

0

類:

package com.example.cyril.tryapp; 

    import android.content.Context; 
    import android.graphics.Canvas; 
    import android.graphics.Movie; 
    import android.util.AttributeSet; 
    import android.view.View; 

import java.io.InputStream; 

public class GifView extends View { 

    private InputStream gifInputStream; 
    private Movie gifMovie; 
    private int movieWidth = 150, movieHeight = 500; 
    private long movieDuration; 
    private long mMovieStart; 

    public GifView(Context context) { 
     super(context); 
     init(context); 
    } 

    public GifView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public GifView(Context context, AttributeSet attrs, 
        int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context){ 
     setFocusable(true); 
     gifInputStream = context.getResources() 
       .openRawResource(R.drawable.maestragif); 

     gifMovie = Movie.decodeStream(gifInputStream); 
     movieWidth = gifMovie.width(); 
     movieHeight = gifMovie.height(); 
     movieDuration = gifMovie.duration(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, 
      int heightMeasureSpec) { 
     setMeasuredDimension(movieWidth, movieHeight); 
    } 

    public int getMovieWidth(){ 
     return movieWidth; 
    } 

    public int getMovieHeight(){ 
     return movieHeight; 
    } 

    public long getMovieDuration(){ 
     return movieDuration; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     long now = android.os.SystemClock.uptimeMillis(); 
     if (mMovieStart == 0) { // first time 
      mMovieStart = now; 
     } 

     if (gifMovie != null) { 

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

      int relTime = (int)((now - mMovieStart) % dur); 

      gifMovie.setTime(relTime); 

      gifMovie.draw(canvas, 0, 0); 
      invalidate(); 

     } 

    } 

} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg_mainplain" 
    android:layout_gravity="center"> 



<com.example.cyril.tryapp.GifView 
     android:id="@+id/gifview" 
     android:layout_width="130dp" 
     android:layout_height="230dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginBottom="30dp" /> 

    <com.example.cyril.tryapp.GifView2 
     android:id="@+id/gifview2" 
     android:layout_width="350dp" 
     android:layout_height="70dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 


</RelativeLayout> 
0

GIF動畫,因爲你設置寬度和heignt具有特定值的屬性不適合所有屏幕:

android:layout_width="130dp" 
android:layout_height="230dp" 

DP值是密度獨立的,但它們在每個屏幕上都不是相同的大小,應用縮放因子來轉換D P值爲PX值

+0

uhm,所以我應該放在那裏,以便它適合其他屏幕? – Cyril

+0

你想讓整個屏幕適合圖像,或者你想要一些邊距? –

+0

沒有沒有整個屏幕。我想要在屏幕左側的第一個GIF,另一個在頂部。 – Cyril