gpt4 book ai didi

android - 使用动画 gif 的动态壁纸(使用 centercrop scaletype)

转载 作者:行者123 更新时间:2023-11-29 01:22:01 28 4
gpt4 key购买 nike

如何使用动画 .gif 图像创建简单的动态壁纸?我得到的 .gif 图像与屏幕不成比例。如何使它居中?

最佳答案

你的 Activity 文件

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

list 文件

<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 文件夹中。要创建 Assets 文件夹,请单击文件->新建->文件夹-> Assets

关于android - 使用动画 gif 的动态壁纸(使用 centercrop scaletype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36283296/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com