作者:mm2525888 | 来源:互联网 | 2023-09-24 12:20
本文是一个Android动态壁纸的例子,利用android_ndk调用底层的C++代码,使用OpenGLES来绘制动态壁纸。仅作参考。
首先是定义我们自己的Renderer类,FireWallpaperRenderer实现了GLWallpaperService.Renderer接口(GLWallpaperService的代码在《Android利用OpenGLES开发动态壁纸用到的GLWallpaperService类》的那篇文章里):
- import java.io.IOException;
- import java.io.InputStream;
-
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.opengl.GLUtils;
- import android.util.Log;
-
- public class FireWallpaperRenderer implements GLWallpaperService.Renderer {
-
- private int[] texture = new int[2];
-
- private Context mContext;
-
-
- public FireWallpaperRenderer(Context context){
- mContext = context;
- }
-
-
-
-
-
- @Override
- public void onDrawFrame(GL10 gl) {
-
- FireNativeMethod.onDrawFrame(gl);
- }
-
-
-
-
-
-
-
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
-
- FireNativeMethod.onSurfaceChanged(gl, width, height);
- }
-
-
-
-
-
-
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
-
- bindTexture(gl, mContext);
-
- FireNativeMethod.setTexture(texture);
-
- FireNativeMethod.onSurfaceCreated(gl, config);
-
- }
-
-
-
-
-
-
-
- private void bindTexture(GL10 gl, Context context) {
-
- gl.glGenTextures(2, texture, 0);
-
- Bitmap bitmap = loadBitmap(context, R.drawable.floor);
- if (bitmap != null) {
- Log.i("firewallpaperrenderer", "bind the floor texture");
-
- gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
-
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
- GL10.GL_NEAREST);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
- GL10.GL_NEAREST);
-
- GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
-
- bitmap.recycle();
- }
- bitmap = loadBitmap(context, R.drawable.fire);
- if (bitmap != null) {
-
- Log.i("firewallpaperrenderer", "bind the fire texture");
- gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
- GL10.GL_NEAREST);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
- GL10.GL_NEAREST);
- GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
- bitmap.recycle();
- }
-
- }
-
-
-
-
-
- private Bitmap loadBitmap(Context context, int resourceId) {
- InputStream is = context.getResources().openRawResource(resourceId);
- Bitmap bitmap = null;
- try {
-
-
- bitmap = BitmapFactory.decodeStream(is);
- } finally {
- try {
-
-
- is.close();
- is = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- return bitmap;
-
- }
-
- }
|
然后定义我们的WallpaperService,FireWallpaperService继承自GLWallpaperService:
- public class FireWallpaperService extends GLWallpaperService {
-
- private FireWallpaperRenderer mRenderer;
-
-
- public Engine onCreateEngine() {
- if (mRenderer == null) {
- mRenderer = new FireWallpaperRenderer(this);
- }
- return new FireWallpaperEngine();
- }
-
- class FireWallpaperEngine extends GLWallpaperService.GLEngine {
-
- public FireWallpaperEngine() {
-
- setRenderer(mRenderer);
- setRenderMode(RENDERMODE_CONTINUOUSLY);
- }
-
- }
-
- }
|
完成后编辑Manifest.xml文件,如下:
- application android:icon="@drawable/icon" android:label="@string/app_name">
- <service android:name=".FireWallpaperService" android:label="@string/firewallpaper"
- 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" />
- service>
-
- application>
|
Manifest.xml文件中,FireWallpaperService使用到的wallpaper文件
(wallpaper" />)在res/xml目录下定义,内容如下:
- <span style="color:#000000;">xml version="1.0" encoding="utf-8"?>
- <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
- android:description="@string/description" android:thumbnail="@drawable/firelivewallpaper" />span>
|
然后是我们的本地方法类——FireNativeMethod:
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
-
- public class FireNativeMethod {
-
- static{
- System.loadLibrary("fire_native_method");
- }
-
-
- public static native void onDrawFrame(GL10 gl);
-
- public static native void onSurfaceChanged(GL10 gl, int width, int height);
-
- public static native void onSurfaceCreated(GL10 gl, EGLConfig config);
-
- public static native void setTexture(int [] textureString);
-
- }
|
之后就是我们的本地C++代码部分了。