package ca.jvsh.livewallpaper;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;
public class LiveWallpaper extends WallpaperService
{
public static final String SHARED_PREFS_NAME = "livewallpapersettings";
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public Engine onCreateEngine()
{
return new TestPatternEngine();
}
class TestPatternEngine extends Engine implements
SharedPreferences.OnSharedPreferenceChangeListener
{
private final Handler mHandler = new Handler();
private float mTouchX = -1;
private float mTouchY = -1;
private final Paint mPaint = new Paint();
private final Runnable mDrawPattern = new Runnable()
{
public void run()
{
drawFrame();
}
};
private boolean mVisible;
private SharedPreferences mPreferences;
private Rect mRectFrame;
private Rect[] mColorRectangles;
private int[] rectColor;
private int mRectCount;
// private
private Rect mGradientRect;
GradientDrawable mGradient;
private boolean mHorizontal = false;
private int mFrameCounter = 0;
private boolean mMotion = true;
private String mShape = "smpte";
TestPatternEngine()
{
final Paint paint = mPaint;
paint.setColor(0xffffffff);
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.STROKE);
mPreferences = LiveWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
mPreferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(mPreferences, null);
}
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();
}
@Override
public void onCreate(SurfaceHolder surfaceHolder)
{
super.onCreate(surfaceHolder);
setTouchEventsEnabled(true);
}
@Override
public void onDestroy()
{
super.onDestroy();
mHandler.removeCallbacks(mDrawPattern);
}
@Override
public void onVisibilityChanged(boolean visible)
{
mVisible = visible;
if (visible)
{
drawFrame();
}
else
{
mHandler.removeCallbacks(mDrawPattern);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height)
{
super.onSurfaceChanged(holder, format, width, height);
initFrameParams();
drawFrame();
}
@Override
public void onSurfaceCreated(SurfaceHolder holder)
{
super.onSurfaceCreated(holder);
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder)
{
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mDrawPattern);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
float yStep, int xPixels, int yPixels)
{
drawFrame();
}
/*
* Store the position of the touch event so we can use it for drawing
* later
*/
@Override
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);
}
/*
* Draw one frame of the animation. This method gets called repeatedly
* by posting a delayed Runnable. You can do any drawing you want in
* here. This example draws a wireframe cube.
*/
void drawFrame()
{
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try
{
c = holder.lockCanvas();
if (c != null)
{
// draw something
drawPattern(c);
drawTouchPoint(c);
}
}
finally
{
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mDrawPattern);
if (mVisible)
{
mHandler.postDelayed(mDrawPattern, 1000 / 25);
}
}
void drawPattern(Canvas c)
{
c.save();
c.drawColor(0xff000000);
Paint paint = new Paint();
if (mMotion)
{
mFrameCounter++;
if (mHorizontal)
{
int right;
int left;
if (mFrameCounter > mRectFrame.right)
mFrameCounter = 0;
for (int i = 0; i < mRectCount; i++)
{
paint.setColor(rectColor[i]);
right = mColorRectangles[i].right + mFrameCounter;
left = mColorRectangles[i].left + mFrameCounter;
if(right > mRectFrame.right)
{
c.drawRect(left - mRectFrame.right, mColorRectangles[i].top, right - mRectFrame.right, mColorRectangles[i].bottom, paint);
}
if(left < mRectFrame.right)
{
c.drawRect(left, mColorRectangles[i].top, right, mColorRectangles[i].bottom, paint);
}
}
if(mShape.compareToIgnoreCase("smpte") == 0)
{
right =mGradientRect.right + mFrameCounter;
left = mGradientRect.left + mFrameCounter;
if(right > mRectFrame.right)
{
mGradient.setBounds(left - mRectFrame.right, mGradientRect.top, right - mRectFrame.right, mGradientRect.bottom);
mGradient.draw(c);
}
if(left < mRectFrame.right)
{
mGradient.setBounds(left, mGradientRect.top, right, mGradientRect.bottom);
mGradient.draw(c);
}
}
}
else
{
int top;
int bottom;
if (mFrameCounter > mRectFrame.bottom)
mFrameCounter = 0;
for (int i = 0; i < mRectCount; i++)
{
paint.setColor(rectColor[i]);
top = mColorRectangles[i].top + mFrameCounter;
bottom = mColorRectangles[i].bottom + mFrameCounter;
if(bottom > mRectFrame.bottom)
{
c.drawRect(mColorRectangles[i].left, top - mRectFrame.bottom, mColorRectangles[i].right, bottom - mRectFrame.bottom, paint);
}
if(top < mRectFrame.bottom)
{
c.drawRect(mColorRectangles[i].left, top, mColorRectangles[i].right, bottom, paint);
}
}
if(mShape.compareToIgnoreCase("smpte") == 0)
{
top = mGradientRect.top + mFrameCounter;
bottom = mGradientRect.bottom + mFrameCounter;
if(bottom > mRectFrame.bottom)
{
mGradient.setBounds(mGradientRect.left, top - mRectFrame.bottom, mGradientRect.right, bottom - mRectFrame.bottom);
mGradient.draw(c);
}
if(top < mRectFrame.bottom)
{
mGradient.setBounds(mGradientRect.left, top, mGradientRect.right, bottom);
mGradient.draw(c);
}
}
}
}
else
{
for (int i = 0; i < mRectCount; i++)
{
paint.setColor(rectColor[i]);
c.drawRect(mColorRectangles[i], paint);
}
if(mShape.compareToIgnoreCase("smpte") == 0)
{
mGradient.setBounds(mGradientRect);
mGradient.draw(c);
}
}
c.restore();
}
void drawTouchPoint(Canvas c)
{
if (mTouchX >= 0 && mTouchY >= 0)
{
c.drawCircle(mTouchX, mTouchY, 40, mPaint);
}
}
void initFrameParams()
{
DisplayMetrics metrics = new DisplayMetrics();
Display display = ((WindowManager) getSystemSer
- 1
- 2
- 3
- 4
- 5
- 6
前往页