網(wǎng)上動態(tài)壁紙的實現(xiàn)教程有很多苗缩,但是用openGL實現(xiàn)動態(tài)壁紙的教程卻是寥寥無幾饵蒂,那么今天就帶領大家學習用openGL實現(xiàn)動態(tài)壁紙,起一個拋磚引玉吧酱讶。
在之前曾寫過一篇 ffmpeg/camera實現(xiàn)最近很火的視頻壁紙退盯,相機壁紙 ,動態(tài)壁紙的實現(xiàn)套路已經(jīng)寫了泻肯,今天就不追述了渊迁。
老規(guī)矩,先看效果圖讓大家有個概念:
大家也看出來了灶挟,沒錯宫纬,是基于上篇openGL ES進階教程(二)之全景圖片 實現(xiàn)的。
下面就具體說說實現(xiàn)套路膏萧,真的是套路,你看~
1.首先要實現(xiàn)一個openGL動態(tài)圖,什么樣的圖完全由自己的需求決定榛泛,比如我的就是基于我上篇的圖
2.編寫一個繼承自WallpaperService的動態(tài)壁紙類
public class GLWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new GLEngine();
}
...
3.清單文件中聲明它蝌蹂,在定義一個圖標的xml,如下:
xml的內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" android:thumbnail="@drawable/back" />
以上幾個步驟即創(chuàng)建了一個動態(tài)壁紙,只不過沒有渲染圖像而已曹锨。孤个。。當WallpaperService被系統(tǒng)創(chuàng)建時沛简,它會創(chuàng)建它自己的渲染表面(Surface),讓應用程序在上面繪制齐鲤,即通過調(diào)用Engine這個渲染引擎。下面一個步驟即最重要的一環(huán)椒楣,如何用openGL渲染的圖像在WallpaperService中顯現(xiàn)出來
4.完成壁紙的引擎
正常情況下给郊,GLSurfaceView是被加入到Activity視圖層的,GLSurfaceView會調(diào)用getHolder()存取其Activity內(nèi)的表面捧灰。在動態(tài)壁紙中我們需要改變這個行為淆九,因此我們只需重載getHolder(),讓其返回動態(tài)壁紙的渲染表面即可毛俏。
class WallpaperGLSurfaceView extends GLSurfaceView {
private static final String TAG = "WallpaperGLSurfaceView";
WallpaperGLSurfaceView(Context context) {
super(context);
}
@Override
public SurfaceHolder getHolder() {
return getSurfaceHolder();
}
public void onWallpaperDestroy() {
super.onDetachedFromWindow();
}
}
onWallpaperDestroy()方法銷毀GLSurfaceView 炭庙。當調(diào)用onDetachedFromWindow()時,GLSurfaceView 就會自動的銷毀自己煌寇。
GLWallpaperService 的全部實現(xiàn)代碼如下:
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.Toast;
public class GLWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new GLEngine();
}
public class GLEngine extends Engine {
private static final String TAG = "GLEngine";
private WallpaperGLSurfaceView glSurfaceView;
private ParticlesRenderer particlesRenderer;
private boolean rendererSet;
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
glSurfaceView = new WallpaperGLSurfaceView(GLWallpaperService.this);
particlesRenderer = new ParticlesRenderer(GLWallpaperService.this);
glSurfaceView.setEGLContextClientVersion(2);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
glSurfaceView.setPreserveEGLContextOnPause(true);
}
glSurfaceView.setRenderer(particlesRenderer);
rendererSet = true;
}
@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (rendererSet) {
if (visible) {
glSurfaceView.onResume();
} else {
glSurfaceView.onPause();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
glSurfaceView.onWallpaperDestroy();
}
class WallpaperGLSurfaceView extends GLSurfaceView {
private static final String TAG = "WallpaperGLSurfaceView";
WallpaperGLSurfaceView(Context context) {
super(context);
}
@Override
public SurfaceHolder getHolder() {
return getSurfaceHolder();
}
public void onWallpaperDestroy() {
super.onDetachedFromWindow();
}
}
}
}
ps:
Kotlin可以編譯成Java字節(jié)碼焕蹄,也可以編譯成JavaScript,方便在沒有JVM的設備上運行阀溶,最近發(fā)布了Kotlin/Native能把Kotlin編譯成機器碼腻脏,也就是C/C++一樣的能力。本專題專注Kotlin淌哟,Kotlin/Native迹卢,KotlinJS與Kotlin_Android的那些事,讓我們共同學習Kotlin壯大Kotlin~
加入專題吧Kotlin-Android-KotlinJS-Kotlin/Native:http://www.reibang.com/c/e88f0f9356a8
相信各位客官已經(jīng)看的很明白了……