即时制作一个中心位图正在旋转的游戏我已成功居中图像,但旋转位于屏幕的左上角。您可以在中心旋转图像,但它位于屏幕的左上角..它只是一个小圆圈。远离形象。谁能解释为什么会发生这种情况?谢谢〜我试着
这是塔类:
public class Tower{
private float angle = 0f;
private float theta_old = 0f;
private int width;
private int height;
private float x, y;
private float thetaX = 0f;
private float theta = 0f;
private TankListener listener;
private Bitmap towerBitmap;
private Matrix matrix;
private float towerPosition;
private int towerHP = 100;
public interface TankListener {
public void onTankChanged(float delta, float angle);
}
public void setTankListener(TankListener l) {
listener = l;
}
public Tower(Context context, Bitmap towerBitmap) {
this.towerBitmap = towerBitmap;
width = towerBitmap.getWidth();
height = towerBitmap.getHeight();
matrix = new Matrix();
}
private float getTheta(float x, float y) {
float sx = x - (width / 2.0f);
float sy = y - (height / 2.0f);
float length = (float) Math.sqrt(sx * sx + sy * sy);
float nx = sx / length;
float ny = sy / length;
float theta = (float) Math.atan2(ny, nx);
final float rad2deg = (float) (180.0 / Math.PI);
float theta2 = theta * rad2deg;
return (theta2 < 0) ? theta2 + 360.0f : theta2;
}
public void setPosition(float x, float y){
this.x = x;
this.y = y;
}
public float getTowerX(){
return x;
}
public float getTowerY(){
return y;
}
public int getTowerWidth(){
return width;
}
public int getTowerHeight(){
return height;
}
public Bitmap getBitmap(){
return towerBitmap;
}
private void notifyListener(float delta, float angle) {
if (null != listener) {
listener.onTankChanged(delta, angle);
}
}
public void rotateTower(float x , float y){
angle = (getTheta(x, y) + 90) % 360;
}
public void draw(Canvas c) {
matrix.reset();
matrix.postRotate(angle, width/2, height/2);
matrix.postTranslate(x, y);
Log.v("here", "TranslateX : " + x + " TranslateY : " + y);
c.drawBitmap(towerBitmap, matrix, null);
}
}
这是游戏线程:
private SurfaceHolder mSurfaceHolder;
public DisplayMetrics metrics = new DisplayMetrics();
private static Bitmap mBackgroundImage;
public static Bitmap towerImg;
private Handler mHandler;
private Context mContext;
private Object inputProccessorMutex = new Object();
private long spawnEnemyTimer;
/** Holds the input event*/
Input input;
/** Indicate whether the surface has been created & is ready to draw */
public static boolean mRun = false;
private static Tower tower;
public static Resources res;
private Vibrator vibrator;
public static DisplayMetrics displayMetrics;
private static int screenWidth, screenHeight;
public GameThread(SurfaceHolder surfaceHolder, Context context,
Handler handler) {
// get handles to some important objects
mSurfaceHolder = surfaceHolder;
mContext = context;
lastUpdateTime = 0;
Resources res = context.getResources();
// we don't need to transform it and it's faster to draw this way
mBackgroundImage = BitmapFactory.decodeResource(res, R.drawable.background_intersection);
towerImg = BitmapFactory.decodeResource(res, R.drawable.tower_sprite);
tower = new Tower(context, towerImg);
input = new Input();
}
/**
* Starts the game
*/
public void doStart() {
synchronized (mSurfaceHolder) {
}
}
/**
* Resumes from a pause.
*/
public void unpause() {
// Move the real time clock up to now
synchronized (mSurfaceHolder) {
lastUpdateTime = System.currentTimeMillis() + 100;
}
}
@Override
public void run() {
while (mRun) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
long currentTime = System.currentTimeMillis();
long delta = (long) (currentTime - lastUpdateTime);
lastUpdateTime = currentTime;
processInput();
updatePhysics(delta);
// SoundManager.update(delta);
doDraw(c);
}
} finally {
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
/**
* Used to signal the thread whether it should be running or not.
* Passing true allows the thread to run; passing false will shut it
* down if it's already running. Calling start() after this was most
* recently called with false will result in an immediate shutdown.
*
* @param b true to run, false to shut down
*/
public void setRunning(boolean b) {
mRun = b;
}
private void doDraw(Canvas canvas) {
canvas.drawBitmap(mBackgroundImage, 0, 0, null);
//Draw the tower
tower.draw(canvas);
}
/**
* Figures the lander state (x, y, fuel, ...) based on the passage of
* realtime. Does not invalidate(). Called at the start of draw().
* Detects the end-of-game and sets the UI to the next state.
*/
private void updatePhysics(long delta) {
delta=(long) (delta);
}
}
}
public void feedInput(int eventAction, float x, float y) {
synchronized(inputProccessorMutex) {
input.eventAction = eventAction;
input.x = x;
input.y = y;
}
}
private void processInput() {
synchronized(inputProccessorMutex) {
if (input.eventAction == MotionEvent.ACTION_DOWN) {
// TODO Add bullet
} else if (input.eventAction == MotionEvent.ACTION_MOVE) {
tower.rotateTower(input.x, input.y);
Log.v("teka", "x "+ input.x + " y " + input.y);
//tower.rotateTower(tower.getTowerX(), tower.getTowerY());
}
}
}
public void pause() {
// TODO Auto-generated method stub
}
public void setDisplayMetrics(DisplayMetrics m) {
// TODO Auto-generated method stub
displayMetrics = m;
mBackgroundImage=mBackgroundImage.createScaledBitmap(mBackgroundImage,
displayMetrics.widthPixels, displayMetrics.heightPixels, true);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels;
tower.setPosition(screenWidth/2 - tower.getTowerWidth()/2,
screenHeight/2 - tower.getTowerHeight()/2);
}
public static int getScreenWidth() {
return screenWidth;
}
public static int getScreenHeight() {
return screenHeight;
}
public static float towerPositionX(){
return tower.getTowerX();
}
public static float towerPositionY(){
return tower.getTowerY();
}
private class Input{
int eventAction;
float x, y;
}
}
据说旋转在塔图像中
我尝试了不同的方法,但仍然没有运气。