我试图将我的“播放器”旋转到我在手机上触摸的位置,但似乎不起作用。例子
下面是我如何尝试的:
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
int dx = (int)player.x - x;
if(dx<0)
dx*=-1;
int dy = (int)player.y - y;
if(dy<0)
dy*=-1;
double df = dy/dx;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if (joystick.isPressed((double) event.getX(),(double) event.getY())){
joystick.setIsPressed(true);
}
RotateBitmap(player.getMovement(),(float) Math.toDegrees(Math.asin(df)));
return true;
case MotionEvent.ACTION_MOVE:
if (joystick.getIsPressed()){
joystick.setActuator((double) event.getX(),(double) event.getY());
}
return true;
case MotionEvent.ACTION_UP:
joystick.setIsPressed(false);
joystick.resetActuator();
return true;
}
return super.onTouchEvent(event);
}
这是我的全部代码:
package com.example.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements Runnable {
public static final double MAX_UPS = 30.0;
private final Joystick joystick = new Joystick(275,700,70,40);;
private Thread thread;
private boolean isPlaying;
private int screenX, screenY;
public static float screenRatioX,screenRatioY;
private Paint paint;
private final Movement player;
//private Movement movement;
private Background background1, background2;
public GameView(Context context, int screenX, int screenY) {
super(context);
this.screenX=screenX;
this.screenY=screenY;
screenRatioX=1920f/screenX;
screenRatioY=1080f/screenY;
background1 = new Background(screenX,screenY,getResources());
background2 = new Background(screenX,screenY,getResources());
player = new Movement(screenY,getResources(),getContext(),2*500,500,30);
background2.x=screenX;
paint = new Paint();
}
@Override
public void run() {
while (isPlaying){
update();
draw();
sleep();
}
}
private void update(){
background1.x-=10*screenRatioX;
background2.x-=10*screenRatioX;
if (background1.x + background1.backgorund.getWidth()<0){
background1.x = screenX;
}
if (background2.x + background2.backgorund.getWidth()<0){
background2.x = screenX;
}
joystick.update();
player.update(joystick);
}
private void draw(){
if (getHolder().getSurface().isValid()){
Canvas canvas = getHolder().lockCanvas();
canvas.drawBitmap(background1.backgorund,background1.x,background1.y,paint);
canvas.drawBitmap(background2.backgorund,background2.x,background2.y,paint);
canvas.drawBitmap(player.getMovement(),(int)player.x,(int)player.y,paint);
joystick.draw(canvas);
getHolder().unlockCanvasAndPost(canvas );
}
}
private void sleep(){
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void resume() {
isPlaying = true;
thread = new Thread(this);
thread.start();
}
public void pause(){
try{
isPlaying = false;
thread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
}
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
int dx = (int)player.x - x;
if(dx<0)
dx*=-1;
int dy = (int)player.y - y;
if(dy<0)
dy*=-1;
double df = dy/dx;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if (joystick.isPressed((double) event.getX(),(double) event.getY())){
joystick.setIsPressed(true);
}
RotateBitmap(player.getMovement(),(float) Math.toDegrees(Math.asin(df)));
return true;
case MotionEvent.ACTION_MOVE:
if (joystick.getIsPressed()){
joystick.setActuator((double) event.getX(),(double) event.getY());
}
return true;
case MotionEvent.ACTION_UP:
joystick.setIsPressed(false);
joystick.resetActuator();
return true;
}
return super.onTouchEvent(event);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!