android byte[] 转string 好多问号_Android 仿抖音实现动态壁纸
code小生,一個專注 Android 領域的技術平臺
公眾號回復 Android 加入我的安卓技術群
作者:7_px
鏈接:https://www.jianshu.com/p/fc5cf284abbb
聲明:本文已獲7_px授權發表,轉發等請聯系原作者授權
一、概述:
壁紙運行在一個Android服務之中,這個服務的名字叫做WallpaperService。當用戶選擇了一個壁紙之后,此壁紙所對應的WallpaperService便會啟動并開始進行壁紙的繪制工作。
Engine是WallpaperService中的一個內部類,實現了壁紙窗口的創建以及Surface的維護工作。Engine內部實現了SurfaceView,我們只需要在其內部利用MediaPlayer + SurfaceView就可以播放動態壁紙了。
二、實現:
WallpaperService需要一個xml去配置,然后在AndroidManifest.xml中聲明
<wallpaper?xmlns:android="http://schemas.android.com/apk/res/android"android:thumbnail="@mipmap/icon_lacation_black___cm">wallpaper>
繼承WallpaperService實現我們自己的壁紙服務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();
????????????}
????????}
????}
}
接著聲明這個服務同時聲明我們上面寫的xml配置
?<serviceandroid:name=".VideoLiveWallpaper"android:label="@string/app_name"android:permission="android.permission.BIND_WALLPAPER"android:process=":wallpaper">????????????
????????????<intent-filter>
????????????????<action?android:name="android.service.wallpaper.WallpaperService"?/>
????????????intent-filter>
????????????
????????????<meta-dataandroid:name="android.service.wallpaper"android:resource="@xml/wallpaper"?/>
????????service>
重點在onSurfaceCreated方法中,這里為了可以動態切換不同的壁紙,我是指定去加載一個固定目錄下的視頻文件,然后不斷的復制新文件到這個目錄,因為一旦開啟切換壁紙這個方法就會調用,所以當調用后再動態通知去更改路徑不起作用。
所以我在更換壁紙前先清空
?try?{????????????????????????????????WallpaperManager.getInstance(getContext())
????????????????????????????????????????.clear();
????????????????????????????}?catch?(IOException?e)?{
????????????????????????????????e.printStackTrace();
????????????????????????????}
再去復制需要替換的壁紙到指定目錄
?copyFile(file.getAbsolutePath(),????????????????????????????????????new?File(FileUtil.getDCIMCameraDir(),
????????????????????????????????????????????"hlj_wallpaper").getAbsolutePath());
??/**
?????*?復制單個文件
?????*
?????*?@param?oldPath?String?原文件路徑?如:c:/fqf.txt
?????*?@param?newPath?String?復制后路徑?如:f:/fqf.txt
?????*?@return?boolean
?????*/
????public?void?copyFile(final?String?oldPath,?final?String?newPath)?{
????????progressBar.setVisibility(View.VISIBLE);
????????Observable.create(new?Observable.OnSubscribe()?{@Overridepublic?void?call(Subscriber?super?Boolean>?subscriber)?{try?{int?byteSum?=?0;int?byteRead?;
????????????????????File?oldFile?=?new?File(oldPath);if?(oldFile.exists())?{?//文件存在時
????????????????????????InputStream?inStream?=?new?FileInputStream(oldPath);?//讀入原文件
????????????????????????FileOutputStream?fs?=?new?FileOutputStream(newPath);byte[]?buffer?=?new?byte[1444];while?((byteRead?=?inStream.read(buffer))?!=?-1)?{
????????????????????????????byteSum?+=?byteRead;?//字節數?文件大小
????????????????????????????System.out.println(byteSum);
????????????????????????????fs.write(buffer,?0,?byteRead);
????????????????????????}
????????????????????????inStream.close();
????????????????????????subscriber.onNext(true);
????????????????????????subscriber.onCompleted();
????????????????????}
????????????????}?catch?(Exception?e)?{
????????????????????System.out.println("復制單個文件操作出錯");
????????????????????e.printStackTrace();
????????????????????subscriber.onCompleted();
????????????????}
????????????}
????????})
????????????????.subscribeOn(Schedulers.io())
????????????????.observeOn(AndroidSchedulers.mainThread())
????????????????.subscribe(new?Observer()?{@Overridepublic?void?onCompleted()?{
????????????????????????progressBar.setVisibility(View.GONE);
????????????????????}@Overridepublic?void?onError(Throwable?e)?{
????????????????????????progressBar.setVisibility(View.GONE);
????????????????????}@Overridepublic?void?onNext(Boolean?aBoolean)?{
????????????????????????progressBar.setVisibility(View.GONE);
????????????????????????setToWallPaper(getContext());
????????????????????}
????????????????});
????}
setToWallPaper 方法就是真正的開啟設置壁紙操作了
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);
????}
至此,一個簡單的動態壁紙就搞定了。
推薦閱讀
該用路由來管理你的界面跳轉了
Android:手把手教你如何優雅的實現APP啟動速度優化
登不上高峰,看到的風景始終有限
總結
以上是生活随笔為你收集整理的android byte[] 转string 好多问号_Android 仿抖音实现动态壁纸的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html 实现格子效果,div+css实
- 下一篇: linux 设置dns缓存周期,如何解决