欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android笔记------自定义互动屏保

程序员文章站 2024-03-16 20:50:52
...

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.xxx.dream">

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application android:supportsRtl="true">

        <service
            android:name=".DayDreamService"
            android:exported="true"
            android:label="屏保名字"
            android:permission="android.permission.BIND_DREAM_SERVICE">
            <intent-filter>
                <action android:name="android.service.dreams.DreamService"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>
    </application>

</manifest>

其实主要逻辑就是轮询播放,以达到banner的效果

package com.xxx.dream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Handler;
import android.service.dreams.DreamService;
import android.support.annotation.RequiresApi;
import android.widget.ImageView;

import java.io.InputStream;
import java.util.ArrayList;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public class DayDreamService extends DreamService {


    private int currentItem = 0;
    private ImageView mImageView;
    private InputStream mInputStream000;
    private InputStream mInputStream001;
    private int time = 5 * 1000;// 切图时间
    private Handler handler = new Handler();

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        setInteractive(false);
        setFullscreen(true);
        setContentView(R.layout.dream);
        init();
    }


    /***
     *  初始化
     */
    private void init() {
        mImageView = (ImageView) findViewById(R.id.iv);

        //将图片放在raw目录下
        mInputStream000 = getResources().openRawResource(R.raw.images000);
        mInputStream001 = getResources().openRawResource(R.raw.images001);

        setimage();
        handler.postDelayed(task, time);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        System.exit(0);
        android.os.Process.killProcess(android.os.Process.myPid());
    }



    @Override
    public void onDreamingStarted() {
        super.onDreamingStarted();
    }


    /***
     *
     * 设置图片
     *
     */
    private void setimage() {
        // 把历史的ImageView 图片对象(imageView)释放
        BitmapDrawable bitmapDrawable = (BitmapDrawable) mImageView.getBackground();
        if (bitmapDrawable != null) {
            Bitmap hisBitmap = bitmapDrawable.getBitmap();
            if (hisBitmap.isRecycled() == false) {
                hisBitmap.recycle();
            }
        }

        BitmapFactory.Options mOptions = new BitmapFactory.Options();
        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
        mOptions.inPurgeable = true;
        mOptions.inInputShareable = true;


        Bitmap bmp = null;
        Bitmap oldbmp = null;
        ArrayList<InputStream> inputStreams = new ArrayList<>();
        inputStreams.add(mInputStream000);
        inputStreams.add(mInputStream001);


        InputStream in = inputStreams.get(currentItem % inputStreams.size());

        bmp = BitmapFactory.decodeStream(in,null, mOptions);
        // bmp=decodeSampledBitmapFromResource(in, 960,540);

        if (oldbmp == null) {
            oldbmp = bmp;
        }

        Drawable[] layers = new Drawable[2];
        layers[0] = new BitmapDrawable(oldbmp);
        layers[1] = new BitmapDrawable(bmp);
        oldbmp = bmp;

        if (currentItem == 0) {
            mImageView.setImageBitmap(bmp);
        } else {
            TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
            mImageView.setImageDrawable(transitionDrawable);
            transitionDrawable.startTransition(400);
        }
        System.gc();
    }


    /****
     * 轮询
     */
    private Runnable task = new Runnable() {
        public void run() {
            // TODOAuto-generated method stub
            handler.postDelayed(this, time);// 设置延迟时间
            // 需要执行的代码
            currentItem++;

            setimage();
        }
    };

}
修改默认互动屏保,请将以下目录文件中config_dreamsDefaultComponent修改为需要的屏保:
frameworks/base/core/res/res/values/config.xml

<string name="config_dreamsDefaultComponent" translatable="false">com.google.android.deskclock/com.android.deskclock.Screensaver</string>
\frameworks\base\core\res\res\values\config.xml中,如下两个改为true即可

//表示默认是否插入基座时启动
<bool name="config_dreamsActivatedOnDockByDefault">true</bool>

//表示默认是否充电时启动
<bool name="config_dreamsActivatedOnSleepByDefault">true</bool>
如何默认关闭 设置-显示中的互动屏保?

修改frameworks/base/core/res/res/values/config.xml下的

<bool name="config_dreamsEnabledByDefault">false</bool>将值改为false

本文借鉴了 http://blog.csdn.net/a2700419/article/details/75212929