如何在Android Studio中设置全屏幕背景gif

时间:2016-07-20 12:19:16

标签: java android android-studio android-fullscreen

我的应用程序中有一个GIF图像,希望它成为我的全屏幕背景。我使用宽度和高度=匹配父,但它不会成为全屏。这是我的代码:

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

这是我的班级:

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

}

和xml:

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

3 个答案:

答案 0 :(得分:2)

您可以借助glide

来完成此操作

这是Android的图像加载和缓存库,专注于平滑滚动

Glide支持提取,解码和显示视频静止图像,图像和动画GIF

你可以使用这个

来使用Glide
var colorInterval = setInterval(function() {

    if (pass > 0) {
      clearInterval(colorInterval);
      return; //stop here so that it doesn't continue to execute below code
    }

    $($colorContent).fadeOut(0);
    $(($colorContent + '-' + counter)).fadeIn(animationSpeed);
    ++counter

    if (counter === $($colorContent).length + 1) {
      counter = 1;
    }

  }, 3000);

的xml:

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

的java

<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>

ps:我没有测试代码

答案 1 :(得分:0)

尝试使用 WebView ,使用简单的 html 将您的视图布局为gif,然后将其放在资源目录中并使用 WebView 。我在我的另一个项目中试过这个,可能不是最好的解决方案,但它很有效。

答案 2 :(得分:0)

在文件build.gradle(module)中添加依赖项:

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

简单来说:

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

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

设置图像周围的所有布局:

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