我制作了一个动态壁纸,当手机刷新时显示来自可绘制目录的图片如果打开一个应用程序,我们锁定手机时图像更改比解锁图像会改变,一切都很好这是我的代码
public class CustomWallpaper extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
class WallpaperEngine extends Engine {
private int[] mImagesArray;
private int mImagesArrayIndex = 0;
private boolean mVisible = true;
private String mImageScale = "Stretch to screen";
private CustomWallpaperHelper customWallpaperHelper;
public WallpaperEngine() {
customWallpaperHelper = new CustomWallpaperHelper(getApplicationContext(), getResources());
mImagesArray = new int[] {R.drawable.image_1,R.drawable.image_2,R.drawable.image_3,R.drawable.image_4,R.drawable.image_5,R.drawable.image_6,R.drawable.image_7,R.drawable.image_8,R.drawable.image_9,R.drawable.image_10,R.drawable.image_11,R.drawable.image_12,R.drawable.image_13,R.drawable.image_14,R.drawable.image_15,R.drawable.image_16,R.drawable.image_17,R.drawable.image_18,R.drawable.image_19,R.drawable.image_20,R.drawable.image_21,R.drawable.image_22,R.drawable.image_23,R.drawable.image_24,R.drawable.image_25,R.drawable.image_26,R.drawable.image_27,R.drawable.image_28,R.drawable.image_29,R.drawable.image_30,R.drawable.image_31,R.drawable.image_32};
}
@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible && mVisible != visible) {
drawFrame();
}
mVisible = visible;
}
private void incrementCounter() {
mImagesArrayIndex++;
if (mImagesArrayIndex >= mImagesArray.length) {
mImagesArrayIndex = 0;
}
}
private void drawFrame() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
drawImage(canvas);
incrementCounter();
}
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
}
private void drawImage(Canvas canvas) {
//Get the image and resize it
Bitmap image = BitmapFactory.decodeResource(getResources(),
mImagesArray[mImagesArrayIndex]);
//Draw background
customWallpaperHelper.setBackground(canvas);
//Scale the canvas
PointF mScale = customWallpaperHelper.getCanvasScale(mImageScale, image.getWidth(), image.getHeight());
canvas.scale(mScale.x, mScale.y);
//Draw the image on screen
Point mPos = customWallpaperHelper.getImagePos(mScale, image.getWidth(), image.getHeight());
canvas.drawBitmap(image, mPos.x, mPos.y, null);
}
}
}
我的问题是如何随机显示图像
不像1.2.3.4.5.6.7 ......
但是例如9.15.20.4.35.1 ...
答案 0 :(得分:1)
无需额外的方法来增加计数和检查。只需使用a 随机数介于 0和(size-1)之间。
Random rand=new Random();
private void drawImage(Canvas canvas) {
//rand.nextInt(n) Returns a pseudo-random uniformly distributed int in
//the half-open range [0, n).
//Get the image and resize it
Bitmap image = BitmapFactory.decodeResource(getResources(),
mImagesArray[rand.nextInt(mImagesArray.length)]);
//Draw background
customWallpaperHelper.setBackground(canvas);
//Scale the canvas
PointF mScale = customWallpaperHelper.getCanvasScale(mImageScale, image.getWidth(), image.getHeight());
canvas.scale(mScale.x, mScale.y);
//Draw the image on screen
Point mPos = customWallpaperHelper.getImagePos(mScale, image.getWidth(), image.getHeight());
canvas.drawBitmap(image, mPos.x, mPos.y, null);
}
这也可以帮助你..
答案 1 :(得分:0)
更改
private void incrementCounter()
{
mImagesArrayIndex++;
if (mImagesArrayIndex >= mImagesArray.length)
{
mImagesArrayIndex = 0;
}
}
到
private void incrementCounter()
{
mImagesArrayIndex = new Random().nextInt(mImagesArray.length)
}
这将为您提供mImagesArray