溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android怎么實(shí)現(xiàn)動(dòng)態(tài)壁紙

發(fā)布時(shí)間:2022-01-12 11:14:51 來源:億速云 閱讀:606 作者:iii 欄目:移動(dòng)開發(fā)

這篇文章主要介紹了Android怎么實(shí)現(xiàn)動(dòng)態(tài)壁紙的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Android怎么實(shí)現(xiàn)動(dòng)態(tài)壁紙文章都會(huì)有所收獲,下面我們一起來看看吧。

一、概述:
壁紙運(yùn)行在一個(gè)Android服務(wù)之中,這個(gè)服務(wù)的名字叫做WallpaperService。當(dāng)用戶選擇了一個(gè)壁紙之后,此壁紙所對(duì)應(yīng)的WallpaperService便會(huì)啟動(dòng)并開始進(jìn)行壁紙的繪制工作。
  
  Engine是WallpaperService中的一個(gè)內(nèi)部類,實(shí)現(xiàn)了壁紙窗口的創(chuàng)建以及Surface的維護(hù)工作。Engine內(nèi)部實(shí)現(xiàn)了SurfaceView,我們只需要在其內(nèi)部利用MediaPlayer + SurfaceView就可以播放動(dòng)態(tài)壁紙了。

二、實(shí)現(xiàn):
WallpaperService需要一個(gè)xml去配置,然后在AndroidManifest.xml中聲明

<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
    android:thumbnail="@mipmap/icon_lacation_black___cm"></wallpaper>

繼承WallpaperService實(shí)現(xiàn)我們自己的壁紙服務(wù)VideoLiveWallpaper

public class VideoLiveWallpaper extends WallpaperService {
    @Override
    public Engine onCreateEngine() {
        return new VideoEngine();
    }
    class VideoEngine extends Engine {
        private MediaPlayer mMediaPlayer;
        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setSurface(holder.getSurface());
            try {
                mMediaPlayer.setDataSource(new File(FileUtil.getDCIMCameraDir(), "hlj_wallpaper").getAbsolutePath());
                mMediaPlayer.setLooping(true);
                mMediaPlayer.setVolume(0, 0);
                mMediaPlayer.prepare();
                mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mMediaPlayer.start();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        @Override
        public void onVisibilityChanged(boolean visible) {
            if (visible) {
                mMediaPlayer.start();
            } else {
                mMediaPlayer.pause();
            }
        }
    }}

接著聲明這個(gè)服務(wù)同時(shí)聲明我們上面寫的xml配置

 <service
            android:name=".VideoLiveWallpaper"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_WALLPAPER"
            android:process=":wallpaper">
            <!-- 配置intent-filter -->
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <!-- 配置meta-data -->
            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/wallpaper" />
        </service>

重點(diǎn)在onSurfaceCreated方法中,這里為了可以動(dòng)態(tài)切換不同的壁紙,我是指定去加載一個(gè)固定目錄下的視頻文件,然后不斷的復(fù)制新文件到這個(gè)目錄,因?yàn)橐坏╅_啟切換壁紙這個(gè)方法就會(huì)調(diào)用,所以當(dāng)調(diào)用后再動(dòng)態(tài)通知去更改路徑不起作用。

所以我在更換壁紙前先清空

 try {
                                WallpaperManager.getInstance(getContext())
                                        .clear();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

再去復(fù)制需要替換的壁紙到指定目錄

 copyFile(file.getAbsolutePath(),
                                    new File(FileUtil.getDCIMCameraDir(),
                                            "hlj_wallpaper").getAbsolutePath());
  /**
     * 復(fù)制單個(gè)文件
     *
     * @param oldPath String 原文件路徑 如:c:/fqf.txt
     * @param newPath String 復(fù)制后路徑 如:f:/fqf.txt
     * @return boolean
     */
    public void copyFile(final String oldPath, final String newPath) {
        progressBar.setVisibility(View.VISIBLE);
        Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                try {
                    int byteSum = 0;
                    int byteRead ;
                    File oldFile = new File(oldPath);
                    if (oldFile.exists()) { //文件存在時(shí)
                        InputStream inStream = new FileInputStream(oldPath); //讀入原文件
                        FileOutputStream fs = new FileOutputStream(newPath);
                        byte[] buffer = new byte[1444];
                        while ((byteRead = inStream.read(buffer)) != -1) {
                            byteSum += byteRead; //字節(jié)數(shù) 文件大小
                            System.out.println(byteSum);
                            fs.write(buffer, 0, byteRead);
                        }
                        inStream.close();
                        subscriber.onNext(true);
                        subscriber.onCompleted();
                    }
                } catch (Exception e) {
                    System.out.println("復(fù)制單個(gè)文件操作出錯(cuò)");
                    e.printStackTrace();
                    subscriber.onCompleted();
                }
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onCompleted() {
                        progressBar.setVisibility(View.GONE);
                    }
                    @Override
                    public void onError(Throwable e) {
                        progressBar.setVisibility(View.GONE);
                    }
                    @Override
                    public void onNext(Boolean aBoolean) {
                        progressBar.setVisibility(View.GONE);
                        setToWallPaper(getContext());
                    }
                });
    }

setToWallPaper方法就是真正的開啟設(shè)置壁紙操作了

  public static void setToWallPaper(Context context) {
        final Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
        intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                new ComponentName(context, VideoLiveWallpaper.class));
        context.startActivity(intent);
    }

關(guān)于“Android怎么實(shí)現(xiàn)動(dòng)態(tài)壁紙”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Android怎么實(shí)現(xiàn)動(dòng)態(tài)壁紙”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI