[Android] 안드로이드 스튜디오 SurfaceView(서페이스뷰) 활용
- 코딩/Android
- 2020. 4. 4.
xml파일 하나와 세개의 자바파일로 구성
xml파일
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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=".MainActivity">
<com.example.a31customview.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</com.example.a31customview.CustomView>
</android.support.constraint.ConstraintLayout>
자바파일1
package com.example.surfaceview;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
}
자바파일2
package com.example.surfaceview;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Ball {
private Drawable image = null;
private Point point = new Point();
private Point size = new Point();
private Point delta;
public Drawable getImage() {
return image;
}
public void setImage(Drawable image) {
this.image = image;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public Point getSize() {
return size;
}
public void setSize(Point size) {
this.size = size;
}
public void draw(Canvas canvas) {
image.setBounds(point.x, point.y, point.x + size.x, point.y + size.y);
image.draw(canvas);
}
public void setDelta(int dx, int dy) {
delta = new Point(dx, dy);
}
public void move(Rect surfaceFrame) {
// X axis collision
if (point.x + delta.x < 0 || point.x + delta.x + size.x > surfaceFrame.right)
delta.x *= -1;
else
point.x += delta.x;
// Y axis collision
if (point.y + delta.y < 0 || point.y + delta.y + size.y > surfaceFrame.bottom)
delta.y *= -1;
else
point.y += delta.y;
}
}
자바파일 3
package com.example.surfaceview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = GameView.class.getName();
private final SurfaceHolder holder;
private boolean goOnPlay = true;
private Thread renderer = new Thread() {
public void run() {
super.run();
Drawable bg = getResources().getDrawable(R.drawable.billiards);
bg.setBounds(holder.getSurfaceFrame());
ball.setDelta(15, 30);
while (goOnPlay)
{
ball.move(holder.getSurfaceFrame());
Canvas canvas = holder.lockCanvas();
bg.draw(canvas);
ball.draw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
};
private Ball ball;
public GameView(Context context) {
super(context);
Log.i(TAG, "GameView create");
holder = getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
renderer.start();
ball = new Ball();
ball.setImage(getResources().getDrawable(R.drawable.red_ball));
ball.setSize(new Point(100, 100));
ball.setPoint(new Point(0, 0));
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
goOnPlay = false; // kill thread
}
}
구현 영상
'코딩 > Android' 카테고리의 다른 글
[Android] 안드로이드 스튜디오 ListView 만들기 (0) | 2020.04.07 |
---|---|
[Android] 안드로이드 스튜디오 Preference 활용 (0) | 2020.04.05 |
[Android] 안드로이드 스튜디오 쓰레드(thread) 사용 예시 (0) | 2020.04.03 |
[Android] 안드로이드 스튜디오 핸들러 이용한 프로그레스바 구현 (0) | 2020.03.30 |
[Android] 안드로이드 스튜디오intent 이용하여 text 보내기 (0) | 2020.03.28 |