安卓實戰項目-動態桌面-簡單實現

先上效果圖
圖片描述android


準備:1,桌面是一個小視頻,要求,符合手機尺寸,最好不要超過100M,我這個只有幾Mgit

2,開發環境,因爲沒後更新。使用的仍是Android studio 2.3.3

前言:對於動態的壁紙大部分手機廠商仍是保留的,不過發現oppo某一款就沒有這個功能了,不過絲絕不影響本應用的開發,它只是在沒作動態壁紙的界面而已,系統仍是保留的api


上代碼:
1,簡單的佈局:服務器

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zxyoyo.hosion.dynamicdesk.MainActivity">

    <Button
        android:id="@+id/btn_set"
        android:text="設置動態桌面"
        android:layout_centerInParent="true"
        android:onClick="setDynamicDesk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:text="是否開啓聲音"
        android:layout_centerInParent="true"
        android:id="@+id/cb_sound"
        android:layout_toRightOf="@+id/btn_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

2,動態桌面一直在後臺跑,確定就須要service了。繼承WallpaperServiceapp

public class MyWallpaper extends WallpaperService {

    public static  final String MYWALLPAPER_ACTION="com.zxyoyo.hosion.dynamicdesk";
    public static  final String KEY_ACTION="key_action";
    public static  final int SOUND_OPEN=1;
    public static  final int SOUND_CLOSE=0;

    @Override
    public Engine onCreateEngine() {
        return new MyEngine();
    }

    public static void openSound(Context context) {
        Intent intent = new Intent(MYWALLPAPER_ACTION);
        intent.putExtra(KEY_ACTION, SOUND_OPEN);
        context.sendBroadcast(intent);
    }
    public static void closeSound(Context context) {
        Intent intent = new Intent(MYWALLPAPER_ACTION);
        intent.putExtra(KEY_ACTION, SOUND_CLOSE);
        context.sendBroadcast(intent);
    }

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

    }
 }

其中的MyEngine是內部類,繼承wallpaper的engineide

class MyEngine extends Engine {

        private MediaPlayer mediaPlayer;

        private BroadcastReceiver broadcastReceiver;

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);

            IntentFilter intentFilter = new IntentFilter(MYWALLPAPER_ACTION);
            registerReceiver(broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    int action = intent.getIntExtra(KEY_ACTION, -1);
                    switch (action) {
                        case SOUND_OPEN:
                            mediaPlayer.setVolume(1.0f, 1.0f);
                            break;
                        case SOUND_CLOSE:
                            mediaPlayer.setVolume(0, 0);
                            break;

                    }
                }
            }, intentFilter);


        }

        @Override
        public void onDestroy() {
            unregisterReceiver(broadcastReceiver);
            super.onDestroy();

        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            if (visible) {
                mediaPlayer.start();
            } else {
                mediaPlayer.pause();
            }
        }


        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setSurface(holder.getSurface());
            try{
                AssetManager aManager = getApplicationContext().getAssets();
                AssetFileDescriptor fileDescriptor = aManager.openFd("3sheng3shi.mp4");
                mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
                //循環播放咱們的視頻
                mediaPlayer.setLooping(true);
                //默認將音量設置成最小
                mediaPlayer.setVolume(0,0);
                mediaPlayer.prepare();
                mediaPlayer.start();
            }catch (IOException e){
                e.printStackTrace();
            }

        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            //釋放,置空
            mediaPlayer.release();
            mediaPlayer = null;

        }
    }

3,最後就是在MainActivity中調用了oop

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cbSound = (CheckBox) findViewById(R.id.cb_sound);
        //監聽是否開啓聲音
        cbSound.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    //開啓聲音
                    MyWallpaper.openSound(MainActivity.this);
                }else {
                    //關閉聲音
                    MyWallpaper.closeSound(MainActivity.this);
                }
            }
        });
    }

    //按鈕的點擊事件
    public void setDynamicDesk(View view){
        MyWallpaper.setDynamicWallPaper(this);
    }

總結:兩個類就實現了動態桌面效果,很簡單吧,本身能夠動手試試了,仍是挺實用的;佈局

本質其實就是系統的一個api,有關wallpaper的更多內容我就不詳細說明,下篇文章繼續寫這個項目的升級版!

[附上git源碼][2]

最後終極版本已提交企鵝寶求給五星好評點擊前往,之後的文章就是一步步拆分這個這應用,包括服務器的搭建,數據添加!謝謝支持!this

相關文章
相關標籤/搜索