Wednesday, October 9, 2019

How to create a live wallpaper Android Example

Android has a number of personalization features to help users customize many aspects of their device user experience. One of these features is live wallpaper example. Live wallpapers in android are supposed to be static wallpapers, but instead have interactive features. Learn how to create a live wallpaper in this tutorial!
A live wallpaper on Android, is often used as an animated home screen wallpaper or changes over time in some way. If you have an Android device, you've probably seen a few built-in live wallpapers, like the one where leaves fall into rippling water.
How to create a live wallpaper Android Example

As a developer, you can create and publish live wallpapers. The process is not particularly difficult. However, creating an attractive and desirable live wallpaper while not depleting your device's battery is a challenge. In this tutorial, we will guide you through the process of creating live wallpapers. :)

Step 1: Create wallpaper.xml in folder xml
<?xml version="1.0" encoding="utf-8"?>
<wallpaper
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/app_name"
    android:settingsActivity="com.anhttvn.roselivewallpaper.ui.SettingLiveWallpaper"
    android:thumbnail="@drawable/logo"/>
Step 2: Add uses-feature for android.software.live_wallpaper in manifest.xml:

<uses-feature
        android:name="android.software.live_wallpaper"
        android:required="true" >
    </uses-feature>
Step 3:  add service in Manifest.xml
 <service
            android:name="xxx.util.RoseLiveWallpaperService"
            android:enabled="true"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_WALLPAPER" >
Step 4: Create a class RoseLiveWallpaperService extend WallpaperService
@Override
    public Engine onCreateEngine() {
        return new MyWallpaperEngine();
    }
    private class MyWallpaperEngine extends Engine {
        private final int frameDuration = 20;
        private SurfaceHolder holder;
        private Movie movie;
        private boolean visible;
        private Handler handler;
        private Bitmap background;
        private String [] images;
        private ArrayList<String> listImages;
        private SharePrenFile mSharePrenFile;
        private int mPosition;
        public MyWallpaperEngine() {
            //this.movie = movie;
            handler = new Handler();
        }
        private void getData(){
            DisplayMetrics metrics = getApplication().getResources().getDisplayMetrics();
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;
            mSharePrenFile = new SharePrenFile(getApplicationContext());
            mPosition = mSharePrenFile.getBg("bg");
            try {
                images =getAssets().list("image");
            } catch (IOException e) {
                e.printStackTrace();
            }
            listImages = new ArrayList<String>(Arrays.asList(images));
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPurgeable = true;
            BufferedInputStream bufferedInputStream;
            InputStream inputstream = null;
            try {
                inputstream = getApplicationContext().getAssets().open("image/"
                        + listImages.get(mPosition));
            } catch (IOException e) {
                e.printStackTrace();
            }
            bufferedInputStream = new BufferedInputStream(inputstream);
            background = BitmapFactory.decodeStream(bufferedInputStream);
            background = Bitmap.createScaledBitmap(background, width, height, false);
        }
        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            this.holder = surfaceHolder;
        }
        private Runnable drawGIF = new Runnable() {
            public void run() {
                draw();
            }
        };
        private void draw() {
            getData();
            if (visible) {
                Canvas canvas = holder.lockCanvas();
                canvas.save();
                // Adjust size and position so that
                // the image looks good on your screen
                //movie.draw(canvas, -100, 0);
                canvas.drawBitmap(background, 0, 0, null);
                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);
        }
        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            this.visible = false;
            handler.removeCallbacks(drawGIF);
        }
        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
        }
        @Override
        public void onTouchEvent(MotionEvent event){
            super.onTouchEvent(event);
        }
    }

No comments:

Post a Comment