Wednesday, October 9, 2019

How to create a wallpaper service in Android

Today I will introduce and share to you how to create a wallpaper service in Android, to perform it you should do the following steps:
wallpaper service in Android

Also you can see how to create a live wallpaper Android at here.
Step 1: Create a class with name ColorWallpaperService.java
public class ColoredLiveWallPaperService extends WallpaperService {
private Handler mHandler = new Handler();
private int mRectWidth=50;
    private int mRectHeight=50;
int hue;
    float saturation;
    float black;
int nextStartingHue;

  @Override
    public void onCreate(){
        super.onCreate();
    }
@Override
    public void onDestroy(){
        super.onDestroy();
    }
@Override
    public Engine onCreateEngine(){
        return new ColoredLiveWallpaperEngine();
    }
private class ColoredLiveWallpaperEngine extends Engine{
        private boolean mVisible = true;
private Runnable mDrawRunner = new Runnable() {
@Override
            public void run() {
                drawColoPallet();
            }
        };
        public ColoredLiveWallpaperEngine(){
            // Specify the initial value of HSV color components
            hue = 0;
            saturation = 0.5f;
            black = 1.0f;
            // Define the initial hue for next canvas drawing
            nextStartingHue = 0;
        }
@Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
        }
@Override
        public void onDestroy() {
            super.onDestroy();
            mHandler.removeCallbacks(mDrawRunner);
        }
  @Override
        public void onVisibilityChanged(boolean visible){
            mVisible = visible;
            if (visible) {
                // If the wallpaper is visible the start drawing on the canvas
                drawColoPallet();
            } else {
mHandler.removeCallbacks(mDrawRunner);
            }
        }
@Override
        public void onSurfaceDestroyed(SurfaceHolder holder){
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mHandler.removeCallbacks(mDrawRunner);
        }
        @Override
        public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
                                     float yStep, int xPixels, int yPixels) {
        }
@Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height){
            super.onSurfaceChanged(holder, format, width, height);
        }
        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
        }
@Override
        public void onTouchEvent(MotionEvent event){
            super.onTouchEvent(event);
        }

        // Custom method to draw a color pallet
        void drawColoPallet(){
SurfaceHolder holder = getSurfaceHolder();
  Canvas canvas = holder.lockCanvas();
            try{
                // Get the canvas width and height
                int canvasWidth = canvas.getWidth();
                int canvasHeight = canvas.getHeight();
  Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                // Generate the color pallet
                for(int left=0;left<=canvasWidth;left+=mRectWidth){
                    for(int top=0;top<=canvasHeight;top+=mRectHeight){
                        // Generate next HSV color
                        if(hue>=360){
                            hue = 0;
                        }
                        if(saturation >=1.0f){
                            saturation = 0.5f;
                        }
                        hue +=1;
                        saturation +=0.1f;
                        paint.setColor(createHSVColor(hue, saturation,black));
                        if(canvas != null){
Rect rectangle = new Rect(left, top, left + mRectWidth, top + mRectHeight);
canvas.drawRect(rectangle, paint);
                        }
                    }
                }
nextStartingHue +=1;
                // Specify the next canvas starting hue
                hue = nextStartingHue;
            }finally{
                if(canvas != null){
holder.unlockCanvasAndPost(canvas);
   }
            }
mHandler.removeCallbacks(mDrawRunner);
            // If visible continue the animation
            if(mVisible){
mHandler.postDelayed(mDrawRunner, 1000);
            }
        }
        // Create HSV color from values
        private int createHSVColor(float hue, float saturation, float black){
int color = Color.HSVToColor(255, new float[]{hue, saturation, black});
            return color;
        }
    }
}
Step 2: Creata xml xml/colored_live_wallpaper.xml
<?xml version="1.0" encoding="utf-8"?>
<wallpaper
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="Colored Live Wallpaper"
    android:thumbnail="@drawable/preview"
    />

Step 3: Config AndroidManifest.xml
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxxx.coloredlivewallpaper"
    >
    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".ColoredLiveWallPaperService"
            android:label="Colored Live Wallpaper"
            android:enabled="true"
            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/colored_live_wallpaper"
                >
            </meta-data>
        </service>
    </application>
    <uses-feature
        android:name="android.software.live_wallpaper"
        android:required="true"
        />
</manifest>
Result  how to create a wallpaper service android below

How to create a wallpaper service in Android

No comments:

Post a Comment