1

先上效果图
图片描述


准备:1,桌面是一个小视频,要求,符合手机尺寸,最好不要超过100M,我这个只有几M

2,开发环境,由于没后更新。使用的还是Android studio 2.3.3


前言:对于动态的壁纸大部分手机厂商还是保留的,不过发现oppo某一款就没有这个功能了,不过丝毫不影响本应用的开发,它只是在没做动态壁纸的界面而已,系统还是保留的


上代码:
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了。继承WallpaperService

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的engine

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中调用了

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]


最后终极版本已提交企鹅宝求给五星好评点击前往,以后的文章就是一步步拆分这个这应用,包括服务器的搭建,数据添加!谢谢支持!


hosition
82 声望0 粉丝

这个人很懒,没写简介。。