룰렛 2

android 2015. 11. 11. 10:52 |
package com.test.iunote.rotate_v1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

CircleManager mCircleManager; //원을 그리는 도구

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 원 유틸리티를 초기화한다.
mCircleManager = new CircleManager(this);

// 레이아웃을 불러온다.
LinearLayout linear = (LinearLayout) findViewById(R.id.rotate_circle);
// 버튼 이벤트를 지정한다.
Button btn = (Button) findViewById(R.id.btn_rotate);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 회전할 각도를 지정한다.
mCircleManager.onRotateCircle(2400); //회전하는각도 (랜덤으로변경)
// onDraw를 호출한다.
mCircleManager.invalidate();

}
});
// 레이아웃에 뷰를 추가한다.
linear.addView(mCircleManager);
}

/* 원 유틸리티 클래스 */
public class CircleManager extends View {

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 페인트
private float[] value_degree;

private int[] COLORS = { Color.YELLOW, Color.GREEN, Color.WHITE, Color.CYAN, Color.RED };
private String[] STRINGS = { "문화상품권", "100", "200", "300","" }; //
private float values[] = { 1, 1, 3, 5, 8}; // 원의 비율 값이 클수록 커진다.

int temp = 0;
int rotate = 0;
boolean isRotate = false; //제어변수

private Animation anim;

// 기본 생성자
public CircleManager(Context context) {
super(context); //view 클래스에 context 정보 보냄
value_degree = calculateData(values); //각 부채꼴 각도
}

// 비율을 계산해서 원을 그린다.
private float[] calculateData(float[] data) {
float total = 0;
for (int i = 0; i < data.length; i++) {
total += data[i]; //총확률
}
for (int i = 0; i < data.length; i++) {
data[i] = 360 * (data[i] / total); //각 부채골 각도구하기
}
return data;
}

/**
*
* 회전에 대한 옵션을 지정한다.
* @param degree 각도
*/
private void onRotateCircle(int degree) {
// 각도를 지정한다.
this.rotate = degree;
Toast.makeText(getContext(), "회전을 시킨다.", Toast.LENGTH_SHORT).show();
// 회전을 제어한다.
this.isRotate = true;
}

/**
*
* 에니메이션을 만든다.
*
*/
private void createAnimation() {
// anim = new RotateAnimation(0, rotate, getWidth()/2, getHeight()/2);
anim = new RotateAnimation(0, rotate, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(3000); //
anim.setFillEnabled(true);
anim.setFillAfter(true);
startAnimation(anim);
}

/**
*
* 이미지를 그릴때 호출된다.
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Log.d("test", "onDraw 호출 ");
// 도형의 크기를 지정한다. 원을 그리기위해 view의 크기를 layout에서 지정해야 한다.
final int width = getWidth();
final int height = getHeight();
temp = 0;
// float 형 사각형 클레스 - 도형을 그리기 위해 쓰임
RectF rectf = new RectF(10, 10, width, height);
// 정수형 사각형 클래스 - 글자를 쓰기위해 쓰임
Rect rect = new Rect(10, 10, width, height);

int centerX = (rect.left + rect.right) / 2;
int centerY = (rect.top + rect.bottom) / 2;
int radius = (rect.right - rect.left) / 2;

radius *= 0.5; // 1 will put the text in the border, 0 will put the text in the center. Play with this to set the distance of your text.

for (int i = 0; i < value_degree.length; i++)
{
if (i > 0)
temp += (int) value_degree[i - 1]; // rewrote your code here a bit, to avoid duplicate code.

// 부채골을 그린다.
paint.setColor(COLORS[i]); // 칼리저정
paint.setAntiAlias(true); // 화면 부드럽게
paint.setTextAlign(Paint.Align.CENTER); // 텍스트를 가운데 정렬한다.
canvas.drawArc(rectf, temp, value_degree[i], true, paint); // 캔버스에 paint를 입힌다.
// text를 입힌다.
float medianAngle = (temp + (value_degree[i] / 2f)) * (float)Math.PI / 180f; // this angle will place the text in the center of the arc.
paint.setColor(Color.BLACK);
paint.setTextSize(64); // 텍스트의 크기를 지정한다. DP
canvas.drawText(STRINGS[i], (float) (centerX + (radius * Math.cos(medianAngle))), (float) (centerY + (radius * Math.sin(medianAngle))), paint);

// 회전 버튼을 클릭하면 회전 이벤트를 호출한다.
if (isRotate){
createAnimation();
canvas.drawCircle(width, height, 0, paint); // 넓이, 높이, 반지름, 그릴 페인트
isRotate = false;
}

}
}

}
}


'android' 카테고리의 다른 글

안드로이드 룰렛 4 작업중  (0) 2015.11.19
룰렛3  (0) 2015.11.13
생명주기(작성중)  (0) 2015.10.28
안드로이드 스터디 내용 정리  (0) 2015.10.28
canvas (원)  (0) 2015.10.16
Posted by 양승아
: