AirPlaneView
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AirPlaneView">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start / stop"
android:layout_centerHorizontal="true"
android:onClick="startStop"
>
</Button>
<FrameLayout
android:id="@+id/frmMove"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="down">
</Button>
<Button
android:id="@+id/up"
android:layout_above="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="up">
</Button>
<Button
android:id="@+id/leftSide"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/up"
android:onClick="goLeft"
android:text="left">
</Button>
<Button
android:id="@+id/rightSide"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/up"
android:onClick="goLRight"
android:text="right">
</Button>
</RelativeLayout>
JAVA
package com.contineo.mygames;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
public class AirPlaneView extends AppCompatActivity {
FrameLayout frm;
private int width,height;
movmentView movView;
private boolean firstTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_air_plane_view);
frm = findViewById(R.id.frmMove);
movView = null;
firstTime = true;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && firstTime){
firstTime = false;
width = frm.getWidth();
height = frm.getHeight();
Log.d("AirPlaneView", "getWidth: "+width);
Log.d("AirPlaneView", "getHeight: "+height);
if (movView == null) {
movView = new movmentView(this, width, height);
frm.addView(movView);
}
}
}
public void startStop(View view) {
movView.setOnOff(!movView.isOnOff());
}
public void goLeft(View view) {
movView.goLeft();
}
public void goLRight(View view) {
}
}
movmentView
JAVA
package com.contineo.mygames;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class movmentView extends SurfaceView implements Runnable{
private SurfaceHolder holder;
private Canvas canvas;
private int width,height;
private Paint bgPaint;
private Sprite airPlain;
private int interval;
private Thread thread;
private boolean onOff;
private float angle;
public movmentView(Context context,int width,int height) {
super(context);
interval = 50;
angle = 90;
this.width = width;
this.height = height;
bgPaint = new Paint();
bgPaint.setColor(Color.WHITE);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.air_plan);
airPlain = new Sprite(bitmap,width,height);
holder = getHolder();
onOff = true;
thread = new Thread(this);
thread.start();
}
public void drawSurface(){
if (holder.getSurface().isValid()){ //#1
canvas = holder.lockCanvas(); // #2
canvas.drawPaint(bgPaint); // #4.1
airPlain.draw(canvas); // #4.2
holder.unlockCanvasAndPost(canvas); //#3
}
}
public boolean isOnOff() {
return onOff;
}
public void setOnOff(boolean onOff) {
Log.d("movmentView", "setOnOff: "+onOff);
this.onOff = onOff;
}
@Override
public void run() {
while (true){
if (onOff){
drawSurface();
airPlain.move();
}
synchronized (this) {
try {
wait(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void goLeft() {
airPlain.setAngle(180);
}
}
Sprite
package com.contineo.mygames;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.widget.Button;
import java.util.BitSet;
public class Sprite {
private int x,y;
private int screenWidth,screenHeight;
private Bitmap bitmap ,currBitmap;
private float angle;
public Sprite( Bitmap bitmap,int screenWidth, int screenHeight) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.bitmap = bitmap;
currBitmap = Bitmap.createBitmap(bitmap);
x = 0;
angle = 0;
y = screenHeight / 2;
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
public void draw(Canvas canvas){
canvas.drawBitmap(currBitmap,x,y,null);
}
public void move(){
// dir 90 - Right, 0 - Up, 270 - Left, 180 - Down
Matrix matrix = new Matrix();
matrix.postRotate(angle);
currBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
if (angle == 0){
x += 20;
if (x>screenWidth){
x = 0;
}
}
if (angle == 180){
x -= 20;
if (x<0){
x = screenWidth;
}
}
}
}