我正在尝试将位图居中对齐,但我一直没有这样做。我正在为不断循环图像的 Android 制作动态壁纸。我有简单的背景和与背景重叠的动画图像。
这是我的代码:
import java.io.IOException;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.SystemClock;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
/*
* This animated wallpaper draws a rotating wireframe cube.
*/
public class CubeWallpaper1 extends WallpaperService {
public static final String SHARED_PREFS_NAME="cube1_settings";
private final Handler mHandler = new Handler();
private static boolean Reverse= false;
private static boolean screenfit = false;
private static String slow;
private static String fast;
private static String medium;
private static String framerate;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public Engine onCreateEngine() {
return new CubeEngine();
}
class CubeEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener {
private final Paint mPaint = new Paint();
private float mOffset;
private float mTouchX = -1;
private float mTouchY = -1;
private long mStartTime;
private float mCenterX;
private float mCenterY;
private float bottom;
private int i=1;
private float screenwidth;
private float halfscreenwidth;
private float screenheight;
private float screenhalfheight;
private boolean plus = false;
private final Runnable mDrawCube = new Runnable() {
public void run() {
drawFrame();
}
};
private boolean mVisible;
private SharedPreferences mPrefs;
CubeEngine() {
final Paint paint = mPaint;
paint.setColor(0xffffffff);
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.STROKE);
mPrefs = CubeWallpaper1.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
mPrefs.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(mPrefs, null);
mStartTime = SystemClock.elapsedRealtime();
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
setTouchEventsEnabled(true);
}
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mDrawCube);
}
@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
drawFrame();
} else {
mHandler.removeCallbacks(mDrawCube);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
screenwidth = width;
halfscreenwidth = width/2.0f;
screenheight = height;
screenhalfheight = height/2.0f;
drawFrame();
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mDrawCube);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xStep, float yStep, int xPixels, int yPixels) {
mOffset = xOffset;
drawFrame();
}
@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);
}
void drawFrame() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
drawCube(c);
drawTouchPoint(c);
}
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mDrawCube);
if (mVisible) {
mHandler.postDelayed(mDrawCube, 1000 / 600);
if(framerate==slow){
Log.v("slow call ","screen height");
mHandler.postDelayed(mDrawCube, 1000 / 100);
}
else if(framerate==medium){
Log.v("medium call ","screen height");
mHandler.postDelayed(mDrawCube, 1000 / 400);
}
else if(framerate==fast){
Log.v("fast call ","screen height");
mHandler.postDelayed(mDrawCube, 1000 / 1000);
}
}
}
void drawCube(Canvas c) {
c.save();
c.drawColor(0xff000000);
if(Reverse)
{
if (i>73 || i<1)
{
plus = !plus;
if(plus )
i+=2;
else
i-=2;
}
}
else
{
if(i>73)
i=1;
plus =true;
}
try {
if(i<=0 || i>73)
i=1;
THIS IS MY BACKROUND
Bitmap holdmap = BitmapFactory.decodeStream(getAssets().open( "hold.png"));
THIS IS MY IMAGE ANIMATION
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open((i/10)+""+(i%10)+".png"));
int imgh = bitmap.getHeight();
int himgh = bitmap.getHeight()/2;
int imgw = bitmap.getWidth();
int himgw = bitmap.getWidth()/2;
if(screenfit==false){
if(imgw>screenwidth){
float w1 = imgw - screenwidth;
float omg = imgw-w1;
float nw1 = w1/imgw*100;
float h1 = imgh - screenheight;
float hmg =imgh-h1;
float nh1 = h1/imgh*100;
RectF rf = new RectF(0,0,omg,hmg);
RectF rf2 = new RectF(0,0,300,300);
c.drawBitmap(holdmap, null, rf, mPaint);
c.drawBitmap(bitmap, null, rf2, mPaint);
}else if (imgw<screenwidth){
float w1 = imgw - screenwidth;
float omg=imgw-w1;
float nw1 = w1/imgw*100;
float h1 = imgh - screenheight;
float hmg =imgh-h1;
float nh1 = h1/imgh*100;
RectF rf = new RectF(0,0,omg,hmg);
RectF rf2 = new RectF(0,0,300,300);
c.drawBitmap(holdmap, null, rf, mPaint);
c.drawBitmap(bitmap, null, rf2, mPaint);
}
}else if (screenfit==true){
if(imgw>screenwidth){
float w1 = imgw - screenwidth;
float omg=imgw-w1;
float nw1 = w1/imgw*100;
float h1 = imgh - screenheight;
float hmg =imgh-h1;
float nh1 = h1/imgh*100;
RectF rf = new RectF(0,0,omg,hmg);
RectF rf2 = new RectF(0,0,300,300);
c.drawBitmap(holdmap, null, rf, mPaint);
c.drawBitmap(bitmap, null, rf2, mPaint);
}else if (imgw<screenwidth){
float w1 = imgw - screenwidth;
float omg=imgw-w1;
float nw1 = w1/imgw*100;
float h1 = imgh - screenheight;
float hmg =imgh-h1;
float nh1 = h1/imgh*100;
RectF rf = new RectF(0,0,omg,hmg);
RectF rf2 = new RectF(0,0,300,300);
c.drawBitmap(holdmap, null, rf, mPaint);
c.drawBitmap(bitmap, null, rf2, mPaint);
}
}
if(plus )
i++;
else
i--;
} catch (IOException e) {
e.printStackTrace();
}
c.restore();
}
void drawTouchPoint(Canvas c) {
if (mTouchX >=0 && mTouchY >= 0) {
c.drawCircle(mTouchX, mTouchY, 0, mPaint);
}
}
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
Reverse = sharedPreferences.getBoolean("reverse", false);
screenfit = sharedPreferences.getBoolean("screenfit", false);
framerate = sharedPreferences.getString("framerate","fast");
}
}
}