使用gif动画的动态壁纸(带有centercrop scaletype)

时间:2016-03-29 11:42:30

标签: android live-wallpaper

如何使用动画.gif图片制作简单的 动态壁纸 ?我的.gif图像与屏幕不成比例。如何让它成为中心?

2 个答案:

答案 0 :(得分:1)

您的活动文件

public class GIFWallpaperService extends WallpaperService {

@Override
public WallpaperService.Engine onCreateEngine() {
    try {
        Movie movie = Movie.decodeStream(
                getResources().getAssets().open("owlinsnow.gif"));

        return new GIFWallpaperEngine(movie);
    }catch(IOException e){
        Log.d("GIF", "Could not load asset");
        return null;
    }
}

private class GIFWallpaperEngine extends WallpaperService.Engine {

    private final int frameDuration = 20;

    private SurfaceHolder holder;
    private Movie movie;
    private boolean visible;
    private Handler handler;

    public GIFWallpaperEngine(Movie movie) {
        this.movie = movie;
        handler = new Handler();
    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);
        this.holder = surfaceHolder;
    }

    private Runnable drawGIF = new Runnable() {
        public void run() {
            draw();
        }
    };


    private void draw() {
        if (visible) {
            Canvas canvas = holder.lockCanvas();
            canvas.save();
            // Adjust size and position so that
            // the image looks good on your screen
            canvas.scale(2f, 2f);
            movie.draw(canvas, -100, 0);
            canvas.restore();
            holder.unlockCanvasAndPost(canvas);
            movie.setTime((int) (System.currentTimeMillis() % movie.duration()));

            handler.removeCallbacks(drawGIF);
            handler.postDelayed(drawGIF, frameDuration);
        }
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        this.visible = visible;
        if (visible) {
            handler.post(drawGIF);
        } else {
            handler.removeCallbacks(drawGIF);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(drawGIF);
    }
}
}

清单文件

<application android:allowBackup="true" android:label="@string/app_name"
    android:icon="@drawable/owl" android:theme="@style/AppTheme">

    <service
        android:name=".GIFWallpaperService"
        android:enabled="true"
        android:label="Owl in Snow"
        android:permission="android.permission.BIND_WALLPAPER" >
        <intent-filter>
            <action android:name="android.service.wallpaper.WallpaperService"/>
        </intent-filter>
        <meta-data
            android:name="android.service.wallpaper"
            android:resource="@xml/wallpaper" >
        </meta-data>
    </service>

</application>

<uses-feature
    android:name="android.software.live_wallpaper"
    android:required="true" >
</uses-feature>

在res下创建xml目录,在其中创建wallpaper.xml。

<?xml version="1.0" encoding="UTF-8"?>
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="GIF Wallpaper"
android:thumbnail="@drawable/owl">
</wallpaper>

确保将gif图像放在assets文件夹中。要创建资产文件夹,请单击file-&gt; New-&gt; Folder-&gt; assets

答案 1 :(得分:0)

在我看来,所选答案有点不完整,并不是通用的,因为它提到了硬编码值,并没有记住不同的屏幕尺寸。获取onSurfaceChanged方法发送的值并使用它们来缩放Canvas对象会容易得多。

基本上,我们采用屏幕宽度与电影(GIF)宽度的比率以及屏幕高度与电影高度的比率。然后使用这两个变量来缩放画布以完美地填充整个屏幕。

首先,全局存储onSurfaceChanged方法获得的宽度和高度。

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    super.onSurfaceChanged(holder, format, width, height);
    this.width = width;
    this.height = height;
    //GLOBALLY DECLARED
}

onSurfaceChanged在onCreate运行后立即运行,所以不用担心,这些值将在使用之前存储在变量 width height 中。

稍后初始化画布时,请使用屏幕大小与影片大小的比例进行缩放。 PS - movie是与我们正在使用的GIF相关联的Movie对象。

float x = (float)width / (float)movie.width();
float y = (float)height / (float)movie.height();
canvas.scale(x,y);