MainActivity.java
package com.example.runnighorse;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new HorseView(this));
}
}
HorseView.java
package com.example.runnighorse;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Handler;
import android.view.Display;
import android.view.View;
class HorseView extends View {
private int screenWidth, screenHeight, newWidth, newHeight;
private int cloudX = 0, mountainX = 0, grassX = 0;
private int horseX, horseY, horseFrame = 0;
private Bitmap cloud, mountain, grass;
private Bitmap horse[] = new Bitmap[12];
private Handler handler;
private Runnable runnable;
public final long UPDATE_MILLIS = 30;
public HorseView(Context context) {
super(context);
mountain = BitmapFactory.decodeResource(getResources(), R.drawable.bg_grassland_2);
cloud = BitmapFactory.decodeResource(getResources(), R.drawable.bg_cloud6);
grass = BitmapFactory.decodeResource(getResources(), R.drawable.bg_grassland_3);
horse[0] = BitmapFactory.decodeResource(getResources(), R.drawable.mob1);
horse[1] = BitmapFactory.decodeResource(getResources(), R.drawable.mob2);
horse[2] = BitmapFactory.decodeResource(getResources(), R.drawable.mob3);
horse[3] = BitmapFactory.decodeResource(getResources(), R.drawable.mob4);
horse[4] = BitmapFactory.decodeResource(getResources(), R.drawable.mob5);
horse[5] = BitmapFactory.decodeResource(getResources(), R.drawable.mob6);
horse[6] = BitmapFactory.decodeResource(getResources(), R.drawable.mob7);
horse[7] = BitmapFactory.decodeResource(getResources(), R.drawable.mob8);
horse[8] = BitmapFactory.decodeResource(getResources(), R.drawable.mob1);
horse[9] = BitmapFactory.decodeResource(getResources(), R.drawable.mob2);
horse[10] = BitmapFactory.decodeResource(getResources(), R.drawable.mob3);
horse[11] = BitmapFactory.decodeResource(getResources(), R.drawable.mob4);
Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
float height = cloud.getHeight();
float width = cloud.getWidth();
float ratio = width / height;
newHeight = screenHeight;
newWidth = (int) (ratio * screenHeight);
cloud = Bitmap.createScaledBitmap(cloud, newWidth, newHeight, false);
mountain = Bitmap.createScaledBitmap(mountain, newWidth, newHeight, false);
grass = Bitmap.createScaledBitmap(grass, newWidth, newHeight, false);
horseX = screenWidth / 2 - 200;
horseY = screenHeight - 300;
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
invalidate();
}
};
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
cloudX -= 3;
if(cloudX < -newWidth) {
cloudX = 0;
}
canvas.drawBitmap(cloud, cloudX, 0, null);
if(cloudX < screenWidth - newWidth){
canvas.drawBitmap(cloud, cloudX + newWidth, 0, null);
}
mountainX -= 1;
if(mountainX < newWidth){
mountainX = 0;
}
canvas.drawBitmap(mountain, mountainX, 0, null);
if(mountainX < screenWidth - newWidth)
canvas.drawBitmap(mountain, mountainX + newWidth, 0, null);
grassX -= 10;
if(grassX < -newWidth){
mountainX = 0;
}
canvas.drawBitmap(grass, grassX, 0, null);
if(grassX < screenWidth - newWidth)
canvas.drawBitmap(grass, grassX + newWidth, 0, null);
horseFrame++;
if(horseFrame == 12)
horseFrame = 0;
canvas.drawBitmap(horse[horseFrame], horseX, horseY, null);
handler.postDelayed(runnable, UPDATE_MILLIS);
}
}
'Application Programming > Android' 카테고리의 다른 글
[Android/Java] Sdcard에 이미지 다운로드 (0) | 2021.06.14 |
---|---|
[Android/Java] Image download using AsyncTask (0) | 2021.06.14 |
[Android/Java] Media player(local storage) (0) | 2021.06.13 |
[Android/Java] Video view (0) | 2021.06.13 |
[Android/Java] 체크박스 (0) | 2021.06.13 |