android动态壁纸学习


思路其实很简单:继承WallpaperService服务类,然后完成engine类,来进行屏幕的画图(消息传递和线程)。如果有什么不懂的,就去看SDK吧,那上面比较详细的



首先是主配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ljz.wallpaper.demo1"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyLiveWallPaperDemo1Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            
            <service 
                android:name="com.ljz.wallpaper.demo1.LiveWallPaperService"
                android:permission="android.permission.BIND_WALLPAPER"
                android:label="壁纸">
                <intent-filter >
                    <action  android:name="android.service.wallpaper.WallpaperService"/>
                </intent-filter>
                 <meta-data android:name="android.service.wallpaper" 
          android:resource="@xml/livewallpaper" />   
            </service>
            
    </application>

</manifest>


主要动态壁纸代码:

package com.ljz.wallpaper.demo1;


import java.util.ArrayList;
import java.util.Random;


import android.R.integer;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.WindowManager;


public class LiveWallPaperService extends WallpaperService
{
static int width;
static int height;


//背景图片
Bitmap bitmapBKG ;

//叶子
int iLeftCount = 2;
ArrayList<Left> lefts = new ArrayList<Left>();

//玫瑰
int iRoseCount = 3;
ArrayList<Rose> roes = new ArrayList<Rose>();


@Override
public Engine onCreateEngine()
{
//初始化屏幕参数
DisplayMetrics dp = new DisplayMetrics();
((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dp);
width = dp.widthPixels;
height = dp.heightPixels;
return new MyEngine(getResources());
}


//界面引擎
public class MyEngine extends Engine
{
//消息传递
Handler handler = new Handler();

//是够可见
Boolean visiable=true;

//画图线程
Runnable runnable = new Runnable()
{
@Override
public void run()
{
drawFrame();
}
};

//画笔
Paint paint = new Paint();

public MyEngine( Resources r  )  
{
//初始化叶子和玫瑰的图片
Bitmap bitmap1 = BitmapFactory.decodeResource(r, R.drawable.floewr1);
Bitmap bitmap2 = BitmapFactory.decodeResource(r, R.drawable.leaf1);
bitmapBKG = BitmapFactory.decodeResource(r, R.drawable.background);
bitmapBKG = Bitmap.createScaledBitmap(bitmapBKG, width, height, false);

//初始化叶子和玫瑰的数据
for( int i = 0 ; i < iLeftCount ;i++ )
{
Left left = new Left();
left.setPos_x( new Random().nextInt(width) );
left.setPos_y( 0 );
left.setBitmap(bitmap2);
lefts.add(left);
}
for( int i = 0 ; i < iRoseCount ;i++ )
{
Rose rose = new Rose();
rose.setPos_x( new Random().nextInt(width) );
rose.setPos_y( 0 );
rose.setBitmap(bitmap1);
roes.add(rose);
}
}


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


@Override
public void onDestroy()
{
super.onDestroy();
handler.removeCallbacks(runnable);
}


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


@Override
public void onVisibilityChanged(boolean visible)
{
this.visiable = visible;
if( visible )
{
drawFrame();
}
else
{
handler.removeCallbacks(runnable);
}
}



@Override
public void onSurfaceDestroyed(SurfaceHolder holder)
{
super.onSurfaceDestroyed(holder);
visiable = false;
handler.removeCallbacks(runnable);
}


//画图函数
public void drawFrame()
{
SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = holder.lockCanvas();
//设置画笔
paint.setStrokeWidth(4);
paint.setTextSize(16);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.create(Typeface.MONOSPACE, Typeface.BOLD));
//画背景图片
canvas.drawBitmap(bitmapBKG, 0,0, paint);
//画叶子和玫瑰
drawImage(canvas,paint);
holder.unlockCanvasAndPost(canvas);
handler.removeCallbacks(runnable);
if( visiable )
handler.postDelayed(runnable, 200);
}


//画叶子和玫瑰
public void drawImage( Canvas canvas , Paint p )
{
//叶子
if( lefts.size() == 0 || roes.size() == 0 )
{
return ;
}
for( int i = 0 ; i < iLeftCount ; i++ )
{
Left aLeft = lefts.get(i);
canvas.drawBitmap(aLeft.getBitmap(), aLeft.getPos_x() , aLeft.getPos_y(), p);
int leaf1temp = aLeft.getPos_y();
leaf1temp++;
if (3 * leaf1temp > height)
{
aLeft.setPos_y(0);
aLeft.setPos_x(new Random().nextInt() * width);
}
else
{
aLeft.setPos_y(leaf1temp);
}
lefts.set(i, aLeft);
}
//画玫瑰
for( int i = 0; i < iRoseCount ; i++ )
{
Rose rose = roes.get(i);
canvas.drawBitmap(rose.getBitmap(), rose.pos_x, rose.pos_y, p);
int h = rose.getPos_y();
h++;
if( h > height  )
{
rose.setPos_y(0);
rose.setPos_x( new Random().nextInt() * width );
}
else
{
rose.setPos_y(h);
}
roes.set(i, rose);
}
}

}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值