首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

国外一个经典的动态壁纸项目源码及讲授

2012-09-17 
国外一个经典的动态壁纸项目源码及讲解本文出自:http://www.androidkaifa.com/thread-238-1-1.html www.an

国外一个经典的动态壁纸项目源码及讲解

本文出自:http://www.androidkaifa.com/thread-238-1-1.html

 www.androidkaifa.com在国外的一网站看到一个不错的动态壁纸项目,感觉这个项目对于开发者们在某一些技术点还有一定的帮助的,现在笔者把这项目源码给大家一起分享一下,希望对大家有用,同时也为大家简单的翻译一下这个项目的原文讲解,若哪里有错,还大家多多指点,谢谢
         自Android 2.1之后开发者就可以自行为自己的手机主屏幕创建一个自己的动态壁纸,动态壁纸类似于一个普通的动画应用,你可以其中创建菜单,或是用OpenGl和SGL绘图等等,
        在这篇文章的中我将一步步演示了如何从头开始创建动态壁纸,我们将创建的动态壁纸,有点类似于将电视机在没有信号时屏幕的闪屏。
        本文将重点介绍以下几个方面:
1:如何利用grahpic的Canvas类来绘制一些圆,长方形
2:如何处理屏幕分辨率和方向不同的应用程序开发
3:为动态壁纸创建一下设置对话框
4:访问项目的XML资源文件
本应用平台必需是android2.1以上,首先我们得删除项目自行创建的main.xml文件,因为这文件在我们的项目没有用的,取而代之

的是Livewallpaper.xml文件,Livewallpaper.xml文件中的内容:
<?xml version="1.0" encoding="utf-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
    android:settingsActivity="ca.jvsh.livewallpaper.LiveWallpaperSettings"
    android:thumbnail="@drawable/icon"/>
下再来看功能配置文件:Livewallpaper_settings.xm,该文件用于描述项目可用的设置
<?xml version="1.0" encoding="utf-8"?>
<国外一个经典的动态壁纸项目源码及讲授referenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/livewallpaper_settings"
    android:key="livewallpaper_settings">

    <ListPreference
        android:key="livewallpaper_testpattern"
        android:title="@string/livewallpaper_settings_title"
        android:summary="@string/livewallpaper_settings_summary"
        android:entries="@array/livewallpaper_testpattern_names"
        android:entryValues="@array/livewallpaper_testpattern_prefix"/>
    <CheckBoxPreference android:key="livewallpaper_movement"
        android:summary="@string/livewallpaper_movement_summary"
        android:title="@string/livewallpaper_movement_title"
        android:summaryOn="Moving test pattern"
        android:summaryOff="Still test pattern"/>
</PreferenceScreen>
其中ListPreference控件是用于显示多个用户选项,而CheckBoxPreference 控件则是对某一功能项选中或取消
接下来是strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn国外一个经典的动态壁纸项目源码及讲授asis:names:tc:xliff:document:1.2">
    <!-- General -->
    <skip />
    <!-- Application name -->
    <string name="app_name">Live Wallpaper</string>

    <string name="livewallpaper_settings">Settings</string>
    <string name="livewallpaper_settings_title">Select test pattern</string>
    <string name="livewallpaper_settings_summary">
        Choose which test pattern to display</string>
    <string name="livewallpaper_movement_title">Motion</string>
    <string name="livewallpaper_movement_summary">
        Apply movement to test pattern</string>
</resources>
对部分代码块的解决:
用于检测设备的屏幕大小的布局方向:
DisplayMetrics metrics = new DisplayMetrics();
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
display.getMetrics(metrics);

mRectFrame = new Rect(0, 0, metrics.widthPixels, metrics.heightPixels);


int rotation = display.getOrientation();
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
    mHorizontal = false;
else
    mHorizontal = true;
用GradientDrawable 类绘制一个gradient:
private Rect                mGradientRect;
GradientDrawable            mGradient;

mGradientRect = new Rect(10,10, 40, 40);
mGradient = new GradientDrawable(Orientation.LEFT_RIGHT, new int[]
        { 0xff050505, 0xfffdfdfd });
mGradient.setBounds(mGradientRect);
mGradient.draw(c);
当用户改变软件设置时:
public void onSharedPreferenceChanged(SharedPreferences prefs,
        String key)
{
    mShape = prefs.getString("livewallpaper_testpattern", "smpte");
    mMotion = prefs.getBoolean("livewallpaper_movement", true);
    readColors();
}

private void readColors()
{
    int pid = getResources().getIdentifier(mShape + "colors", "array", getPackageName());

    rectColor = getResources().getIntArray(pid);
    mRectCount = rectColor.length;
    mColorRectangles = new Rect[mRectCount];

    System.out.println("mRectCount "+mRectCount);
    initFrameParams();
}
最后我们再来看一下 AndroidManifest.xml文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.jvsh.livewallpaper"
    android:versionName="1.0.20100908.1"
    android:versionCode="1">

    <uses-sdk android:minSdkVersion="7" />
    <uses-feature android:name="android.software.live_wallpaper" />//www.andridkaifa.com提醒大家一定记得加上这个权



    <application android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_WALLPAPER">

        <service android:name=".LiveWallpaper"
            android:label="@string/app_name"
            android:icon="@drawable/icon">

            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data android:name="android.service.wallpaper"
                android:resource="@xml/livewallpaper" />

        </service>

        <activity android:label="@string/livewallpaper_settings"
            android:name=".LiveWallpaperSettings"
            android:theme="@android:style/Theme.Light.WallpaperSettings"
            android:exported="true"
            android:icon="@drawable/icon">
        </activity>

    </application>
</manifest>
分别注册一个acitivty类和service类
运行结果如下图:
国外一个经典的动态壁纸项目源码及讲授国外一个经典的动态壁纸项目源码及讲授LiveWallpaper_src.zip