Demo
구성
- activity
- view
- gif resource
- [MainActivity.java]
package com.example.recyclerview;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private PlainAnim pa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pa = new PlainAnim(this);
setContentView(pa);
}
}
- [PlaneAnim.java]
package com.example.recyclerview;
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.graphics.Rect;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.View;
import java.util.Random;
public class PlainAnim extends View {
private Bitmap background;
private Bitmap plane[] = new Bitmap[8];
private int planeX, planeY;
private Rect dest;
private int dWidth, dHeight;
private int planeFrame = 0;
private Random random;
private int planeVelocity = 20;
private int planeWidth, planeHight;
private Handler handler;
private Runnable runnable;
private final int UPDATE_MILLIS = 30;
public PlainAnim(Context context) {
super(context);
handler = new Handler();
random = new Random();
background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
plane[0] = BitmapFactory.decodeResource(getResources(), R.drawable.plain0);
plane[1] = BitmapFactory.decodeResource(getResources(), R.drawable.plain1);
plane[2] = BitmapFactory.decodeResource(getResources(), R.drawable.plain2);
plane[3] = BitmapFactory.decodeResource(getResources(), R.drawable.plain3);
plane[4] = BitmapFactory.decodeResource(getResources(), R.drawable.plain4);
plane[5] = BitmapFactory.decodeResource(getResources(), R.drawable.plain5);
plane[6] = BitmapFactory.decodeResource(getResources(), R.drawable.plain6);
plane[7] = BitmapFactory.decodeResource(getResources(), R.drawable.plain7);
// 화면크기구하기
Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
dWidth = size.x;
dHeight = size.y;
planeX = -plane[0].getWidth();
planeY = random.nextInt(10) * 100;
dest = new Rect(0, 0, dWidth, dHeight);
runnable = new Runnable() {
@Override
public void run() {
invalidate();
}
};
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(background, null, dest, null);//화면크기에 맞게 백그라운드 이미지 그리기
planeFrame++;
if(planeFrame == 8){
planeFrame = 0;
}
planeX += planeVelocity; // x 변화값 달리는 속도
planeWidth = plane[0].getWidth();
planeHight = plane[0].getHeight();
if(planeX > dWidth) {
planeX = -plane[0].getWidth();
planeY = random.nextInt(10) * 100;
Log.d("planeY : ", planeY + "");
}
canvas.drawBitmap(plane[planeFrame], planeX, planeY, null);// 위치값 설정해서 비행기 그리기
handler.postDelayed(runnable, UPDATE_MILLIS);
}
}
'System Programming > Android' 카테고리의 다른 글
[Android/Java] naver map api (0) | 2021.06.19 |
---|---|
[Android/Firebase] Firebase 연동 (0) | 2021.06.16 |
[Android/Java] 터치하면 위로올라가는 게임 (0) | 2021.06.14 |
[Android/Java] video view 가로 모드 (0) | 2021.06.14 |
[Android/Java] 날씨 api (0) | 2021.06.14 |