如何在Android中以编程方式将GIF文件设置为Wallpaper

时间:2018-02-13 12:47:14

标签: android

我做了很多R& D但没有得到正确答案但仍然没有找到答案。

1 个答案:

答案 0 :(得分:0)

此视图类可以帮助您设置GIF文件

import java.io.InputStream;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;



public class GIFView extends View {
public Movie mMovie;
public long movieStart;

public GIFView(Context context) {
    super(context);
    initializeView();
}

public GIFView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeView();
}

public GIFView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initializeView();
}

private void initializeView() {
    InputStream is = getContext().getResources().openRawResource(R.raw.animated);
    mMovie = Movie.decodeStream(is);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    super.onDraw(canvas);
    long now = android.os.SystemClock.uptimeMillis();
    if (movieStart == 0) {
        movieStart = now;
    }
    if (mMovie != null) {
        int relTime = (int) ((now - movieStart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
        this.invalidate();
    }
}
private int gifId;

public void setGIFResource(int resId) {
    this.gifId = resId;
    initializeView();
}

public int getGIFResource() {
    return this.gifId;
}
}
你的xml文件中的

使用它:

<com.example.YourPakageName.GIFView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
components:src="@drawable/animated" />