日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 动态壁纸仿抖音,Android 仿抖音实现动态壁纸

發布時間:2023/12/20 Android 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 动态壁纸仿抖音,Android 仿抖音实现动态壁纸 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[好文分享:www.pp00.com] [轉載出處:www.pp00.com]Linux編程點擊右側存眷,免費入門到精曉!

作者丨7_pxhttps://www.jianshu.com/p/fc5cf284abbb

一、概述:

壁紙運行在一個Android辦事之中,這個辦事的名字叫做WallpaperService。當用戶選擇了一個壁紙之后,此壁紙所對應的WallpaperService便會啟動并起頭進行壁紙的繪制工作。

Engine是WallpaperService中的一個內部類,實現了壁紙窗口的建立以及Surface的維護工作。Engine內部實現了SurfaceView,我們只需要在其內部行使MediaPlayer + SurfaceView就能夠播放動態壁紙了。

二、實現:

WallpaperService需要一個xml去設置,然后在AndroidManifest.xml中聲明

android:thumbnail="@mipmap/icon_lacation_black___cm">

繼續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設置

android:name=".VideoLiveWallpaper"

android:label="@string/app_name"

android:permission="android.permission.BIND_WALLPAPER"

android:process=":wallpaper">

android:name="android.service.wallpaper"

android:resource="@xml/wallpaper"?/>

重點在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()?{

@Override

public?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()?{

@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 方式就是真正的開啟設置壁紙把持了

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 动态壁纸仿抖音,Android 仿抖音实现动态壁纸的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。