1、静态壁纸
1)获取壁纸实例,实际上是一个服务提供
WallpaperManager wallpaperManager=WallpaperManager.getInstance(this);
WallpaperManager.getInstance()方法内容如下:
public static WallpaperManager getInstance(Context context) {
return (WallpaperManager)context.getSystemService(
Context.WALLPAPER_SERVICE);
}
2、使用下列三个方法之一设置壁纸内容
wallpaperManager.setBitmap(bitmap)
wallpaperManager.setStream(data);
wallpaperManager.setResource(R.drawable.a);
2、动态壁纸
1)、在res目录下建xml目录,并建一个xml文件,如aa.xml
android:thumbnail="@drawable/c"/>
2)、写一个类继承WallpaperService类。并重写 public Engine onCreateEngine()方法。
该方法的返回值Engine即为绘制壁纸的方法。重写该类中的方法,并不强制。
当壁纸的显示、状态或大小变化是,会调用Engine的onCreate,
onDestroy,onVisibilityChanged, onSurfaceChanged,
onSurfaceCreated 和onSurfaceDestroyed方法。
有了这些方法,动态壁纸才能展现出动画效果。
而通过设置setTouchEventsEnabled(true),
并且调用onTouchEvent(MotionEvent event)方法,
来激活触摸事件。
3)、在清单文件里面注册该服务
android:permission="android.permission.BIND_WALLPAPER">
4)、使用方法
长按屏幕选择动态壁纸进行选择。
5)、实例
public class LiveGralley extends WallpaperService{
// 实现WallpaperService必须实现的抽象方法
public Engine onCreateEngine()
{
// 返回自定义的Engine
return new MyEngine();
}
class MyEngine extends Engine
{
// 记录程序界面是否可见
private boolean mVisible;
// 记录当前当前用户动作事件的发生位置
private float mTouchX = -1;
private float mTouchY = -1;
// 记录当前圆圈的绘制位置
//左上角坐标
private float cx1 = 15;
private float cy1 = 20;
//右下角坐标
private float cx2 = 300;
private float cy2 = 380;
//右上角坐标
private float cx3 = 300;
private float cy3 = 20;
//左下角坐标
private float cx4 = 15;
private float cy4 = 380;
// 定义画笔
private Paint mPaint = new Paint();
// 定义一个Handler
Handler mHandler = new Handler();
// 定义一个周期性执行的任务
private final Runnable drawTarget = new Runnable()
{
public void run()
{
// 动态地绘制图形
drawFrame();
}
};
@Override
public void onCreate(SurfaceHolder surfaceHolder)
{
super.onCreate(surfaceHolder);
// 初始化画笔
mPaint.setColor(0xffffffff);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(2);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStyle(Paint.Style.STROKE);
// 设置处理触摸事件
//setTouchEventsEnabled(true);
}
@Override
public void onDestroy()
{
super.onDestroy();
// 删除回调
mHandler.removeCallbacks(drawTarget);
}
@Override
public void onVisibilityChanged(boolean visible)
{
mVisible = visible;
// 当界面可见时候,执行drawFrame()方法。
if (visible)
{
// 动态地绘制图形
drawFrame();
}
else
{
// 如果界面不可见,删除回调
mHandler.removeCallbacks(drawTarget);
}
}
/* public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
float yStep, int xPixels, int yPixels)
{
drawFrame();
}*/
/*public void onTouchEvent(MotionEvent event)
{
// 如果检测到滑动操作
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
mTouchX = event.getX();
mTouchY = event.getY();
}
else
{
mTouchX = -1;
mTouchY = -1;
}
super.onTouchEvent(event);
} */
private Integer[] mImageIds = {
R.drawable.a,
R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e,
R.drawable.f, R.drawable.g,
R.drawable.h,
};
// 定义绘制图形的工具方法
private void drawFrame()
{
// 获取该壁纸的SurfaceHolder
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try
{
// 对画布加锁
c = holder.lockCanvas();
if (c != null)
{
c.save();
// 绘制背景色
int i = new Random().nextInt(8);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mImageIds[i]);
c.drawBitmap(bitmap, 0, 0, mPaint);
// 在触碰点绘制圆圈
// drawTouchPoint(c);
// 绘制圆圈
c.drawCircle(cx1, cy1, 80, mPaint);
c.drawCircle(cx2, cy2, 40, mPaint);
c.drawCircle(cx3, cy3, 50, mPaint);
c.drawCircle(cx4, cy4, 60, mPaint);
c.restore();
}
}
finally
{
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(drawTarget);
// 调度下一次重绘
if (mVisible)
{
cx1 += 6;
cy1 += 8;
// 如果cx1、cy1移出屏幕后从左上角重新开始
if (cx1 > 320)
cx1 = 15;
if (cy1 > 400)
cy1 = 20;
cx2 -= 6;
cy2 -= 8;
// 如果cx2、cy2移出屏幕后从右下角重新开始
if (cx2 <15)
cx2 = 300;
if (cy2 <20)
cy2 = 380;
cx3 -= 6;
cy3 += 8;
// 如果cx3、cy3移出屏幕后从右上角重新开始
if (cx3 <0)
cx3 = 300;
if (cy3 >400)
cy3 = 20;
cx4 += 6;
cy4 -= 8;
// 如果cx4、cy4移出屏幕后从左下角重新开始
if (cx4 >320)
cx4 = 15;
if (cy4 <0)
cy4 = 380;
// 指定0.1秒后重新执行mDrawCube一次
mHandler.postDelayed(drawTarget, 3000);
}
}
/*// 在屏幕触碰点绘制圆圈
private void drawTouchPoint(Canvas c)
{
if (mTouchX >= 0 && mTouchY >= 0)
{
c.drawCircle(mTouchX, mTouchY, 40, mPaint);
}
}*/
}
}