上面的字母是从上面掉下来的,那个文字是另外的,会一直移动,碰到屏幕边缘时会几何反弹。。
下落的字母只有一个y速度,移动的字符串有一个x速度和一个Y速度。
下面是代码:
1、继承WallPaper的类
package com.wu.wallpaper; import java.util.ArrayList; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Typeface; import android.os.Handler; import android.service.wallpaper.WallpaperService; import android.util.Log; import android.view.SurfaceHolder; public class MyWallpaper extends WallpaperService implements Contants{ @Override public Engine onCreateEngine() { return new MyEngine(); } class MyEngine extends Engine { private ArrayList<Word> list; private Paint mPaint; private SurfaceHolder holder; private int width; private int height; Word word; int num = 0; private int mTextSpeedX = 5;//文字的x速度 private int mTextSpeedY = 2;//文字的y速度 private int mTextX = TEXT_X; private int mTextY = TEXT_Y; private int mTextWidth; private int mTextHeight; Handler mHandler = new Handler(); private final Runnable ra = new Runnable() { @Override public void run() { draw(); } }; private void initList() { list = new ArrayList<Word>(); for (int i = 0; i < WORD_NUMBER; i++) { addListItem(); } } @Override public void onCreate(SurfaceHolder surfaceHolder) { super.onCreate(surfaceHolder); //设置text的字体 Typeface mTypeface = Typeface.createFromAsset(getAssets(), "fanta.ttf"); mPaint = new Paint(); mPaint.setTypeface(mTypeface); mTextWidth = (int) mPaint.measureText(TEXT); width = getDesiredMinimumWidth()/2; height = getDesiredMinimumHeight(); Log.i("test", "textWidth = " + mTextWidth + " width = " + width); //mPaint.setShadowLayer(30, 0, 0, WORD_COLOR); holder = getSurfaceHolder(); initList(); } @Override public void onDestroy() { mHandler.removeCallbacks(ra); super.onDestroy(); } @Override public void onVisibilityChanged(boolean visible) { if (visible) { draw(); } else { mHandler.removeCallbacks(ra); } } @Override public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { draw(); } private void draw() { num = 0; //锁定画布 Canvas canvas = holder.lockCanvas(); if (canvas != null) { //清空画布 mPaint.setColor(BACKGROUND_COLOR); canvas.drawRect(0, 0, 1024, 600, mPaint); mPaint.setColor(WORD_COLOR); mPaint.setTextSize(WORD_SIZE); //画从上面掉下的字母 for (int i = 0; i < list.size(); i++) { word = list.get(i); canvas.drawText(word.getText(), word.getxOffset(), word .getCurrentY(), mPaint); } //画一直移动的文字 mPaint.setTextSize(TEXT_SIZE); mTextWidth = (int) mPaint.measureText(TEXT); mTextHeight = (int) mPaint.getFontSpacing(); mTextX += mTextSpeedX; mTextY += mTextSpeedY; if (mTextX+mTextWidth > width || mTextX < 0) { mTextSpeedX = -mTextSpeedX; } if (mTextY > height || mTextY < mTextHeight) { mTextSpeedY = -mTextSpeedY; } canvas.drawText(TEXT, mTextX, mTextY, mPaint); //解锁画布 holder.unlockCanvasAndPost(canvas); } //移除回调 mHandler.removeCallbacks(ra); //调整字母的位置 for (int i = 0; i < list.size(); i++) { word = list.get(i); int currentY = word.getCurrentY() + word.getSpeed(); if (currentY > height) { //如果有字母出了屏幕,则删除 list.remove(i); num++; continue; } word.setCurrentY(currentY); } //如果有字母被删除了,则随机添加新的字母 for (int i = 0; i < num; i++) { addListItem(); } //WORD_TIME_UPDATE时间后将线程加入线程队列,即执行线程 mHandler.postDelayed(ra, WORD_TIME_UPDATE); } private void addListItem() { while (!addWord(new Word(width, height))); } private boolean addWord(Word word) { if (list == null) { return false; } //防止两个太近 for (int i = 0; i < list.size(); i++) { if (Math.abs(word.getxOffset() - list.get(i).getxOffset()) < 20) { return false; } } list.add(word); return true; } } }
package com.wu.wallpaper; import android.graphics.Color; public interface Contants { static final String[] WORD = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "C", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; //字母的大小 static final int WORD_SIZE = 30; //字母的颜色 static final int WORD_COLOR = Color.GREEN; //背景颜色 static final int BACKGROUND_COLOR = Color.BLACK; //字母的个数,不能太大 static final int WORD_NUMBER = 8; //刷新的时间间隔ms static final int WORD_TIME_UPDATE = 10; //滚动的字符串 static final String TEXT = "吴述军";// //字符串的起始X、y速度 static final int TEXT_SPEED_X = 5; static final int TEXT_SPEED_Y = 2; //字符串的初始显示位置 static final int TEXT_X = 700; static final int TEXT_Y = 300; //字符串的字体大小 static final int TEXT_SIZE = 40; }
<?xml version="1.0" encoding="utf-8"?> <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"/>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wu.wallpaper" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <!-- 配置实时壁纸Service --> <service android:label="@string/app_name" android:name=".MyWallpaper" android:permission="android.permission.BIND_WALLPAPER"> <!-- 为实时壁纸配置intent-filter --> <intent-filter> <action android:name="android.service.wallpaper.WallpaperService" /> </intent-filter> <!-- 为实时壁纸配置meta-data --> <meta-data android:name="android.service.wallpaper" android:resource="@xml/livewallpaper" /> </service> </application> </manifest>