如何在全屏android

时间:2016-10-25 07:34:11

标签: android animation animated-gif

我想在启动画面中实现.Gif文件,我这样做了,     但它不会全屏显示     它显示在屏幕的左上角,请给出解决方案

我的代码:

  <com.App.app.PlayGifView
    android:id="@+id/viewGif"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   />

不活动

   PlayGifView pGif = (PlayGifView) findViewById(R.id.viewGif);
    pGif.setImageResource(R.drawable.transit);

GifActivity:

   i get this sample code from google 

  public class PlayGifView 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 PlayGifView(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);
    }
   }

//此处传递我的gif文件

 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());
    }
}

@覆盖

 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();
}
}

2 个答案:

答案 0 :(得分:3)

尝试放

   android:scaleType="fitXY"

int layout.xml文件

答案 1 :(得分:1)

<SurfaceView
android:id="@+id/mygif"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:scaleType="fitXY"/>

surfaceView = (SurfaceView)findViewById(R.id.mygif);
GifRun gifRun = new GifRun();
gifRun.LoadGiff(surfaceView, this, R.drawable.splashgif);