|
|
| |
[°³¹ß Tip] ¸ÖƼÅÍÄ¡ (Multi Touch) ±¸Çö ¼Ò½º |
|
|
|
8³â Àü |
package kr.pe.miksnug;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
/**
* ¾Èµå·ÎÀÌµå ¸ÖƼÅÍÄ¡ ÀÔ´Ï´Ù.<br/>
* <br/>
* ÇÁ·ÎÁ§Æ® ½ÇÇà Main Activity Ŭ·¡½º <br/>
* Android 2.0 ÀÌ»ó Áö¿ø <br/>
* <br/>
* 1. ¸ÖƼÅÍÄ¡ °¨Áö (±¸Çö»ó ¹«ÇÑ´ë)<br/>
* 2. ÅÍÄ¡Á¡¿¡ ¿øÇü Ç¥½Ã<br/>
* 3. ´©¸£´Â Èû¿¡ µû¶ó »ö±ò ¹× Å©±â º¯°æ<br/>
* 4. ´©¸£´Â Èû¿¡ µû¶ó Áøµ¿ º¯°æ<br/>
* <br/>
*
* @author yeongeon
*
*/
public class MainActivity extends Activity {
/**
* ¸ÞÀÎ ·¹À̾ƿô ÀνºÅϽº
*/
private FrameLayout m_mainLayout = null;
/**
* µð¹ö±× ÅؽºÆ® Ãâ·Â¿ë ºä
*/
TextView m_tvDebugText = null;
TextView m_tvDebugText2 = null;
/**
* ÀÔ·ÂµÈ À̺¥Æ® ¼ö
*/
private int m_eventCnt = 0;
/**
* ¿ø µµÇü View Ŭ·¡½º
*
* @author yeongeon
*/
class Ball extends View {
/**
* X ÁÂÇ¥
*/
public float m_x;
/**
* Y ÁÂÇ¥
*/
public float m_y;
/**
* ¹ÝÁö¸§
*/
private int m_r;
/**
* ÆäÀÎÆ® ÀνºÅϽº
*/
private Paint m_paint = new Paint(Paint.LINEAR_TEXT_FLAG);
/**
* ¿ø µµÇü »ý¼ºÀÚ
*
* @param context
* @param x
* @param y
* @param r
* @param color
*/
public Ball(Context context, float x, float y, int r, int color) {
super(context);
m_paint.setColor(color);
this.m_x = x;
this.m_y = y;
this.m_r = r;
}
/**
* µµÇü ±×¸®´Â ¸Þ¼Òµå
*
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(m_x, m_y, m_r, m_paint);
super.onDraw(canvas);
}
}
/**
* MyText View Ŭ·¡½º
*
* @author yeongeon
*/
class FloatingText extends View {
/**
* X ÁÂÇ¥
*/
public float m_x;
/**
* Y ÁÂÇ¥
*/
public float m_y;
/**
* ¹ÝÁö¸§
*/
private String m_t;
/**
* ÆäÀÎÆ® ÀνºÅϽº
*/
private Paint m_paint = new Paint(Paint.LINEAR_TEXT_FLAG);
/**
* ¿ø µµÇü »ý¼ºÀÚ
*
* @param context
* @param x
* @param y
* @param r
* @param color
*/
public FloatingText(Context context, float x, float y, String t, int color) {
super(context);
m_paint.setColor(color);
this.m_x = x;
this.m_y = y;
this.m_t = t;
}
/**
* µµÇü ±×¸®´Â ¸Þ¼Òµå
*
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText(m_t, m_x, m_y, m_paint);
super.onDraw(canvas);
}
}
/**
* ÅÍÄ¡ À̺¥Æ® °³º° ³ëµå Ŭ·¡½º
*
* @author yeongeon
*/
class EventNode {
/**
* Æ÷ÀÎÅÍ ¾ÆÀ̵ð
*/
private int m_pointerId = -1;
/**
* Æ÷ÀÎÅÍ À妽º
*/
private int m_pointerIndex = -1;
/**
* x ÁÂÇ¥ °ª
*/
private float m_x = -1;
/**
* y ÁÂÇ¥ °ª
*/
private float m_y = -1;
/**
* ¾Ð·Â °ª
*/
private float m_pressure = -1;
/**
* µµÇü ÀνºÅϽº
*/
private Ball m_ball = null;
/**
* Paint »ö
*/
private int m_color = Color.YELLOW;
/**
* Áøµ¿ ½Ã°£ °£°Ý
*/
int m_vibrationInterval = 2;
/**
* ¹ÝÁö¸§
*/
int m_radius = 50;
/**
* ¶°´Ù´Ï´Â ¹®ÀÚ ÀνºÅϽº
*/
FloatingText m_floatingText = null;
/**
* »ý¼ºÀÚ ÀÔ´Ï´Ù.
*
* @param event
* @param idx
*/
public EventNode(MotionEvent event, int idx) {
this.m_pointerId = event.getPointerId(idx);
this.m_pointerIndex = event.findPointerIndex(this.m_pointerId);
this.m_x = event.getX(this.m_pointerIndex);
this.m_y = event.getY(this.m_pointerIndex);
this.setPressure(event.getPressure(this.m_pointerIndex) );
if (this.getPressure() >= 0.1 && this.getPressure() < 0.18) {
this.m_color = Color.GREEN;
this.m_vibrationInterval = 3;
this.m_radius = 60;
} else if (this.getPressure() > 0.25) {
this.m_color = Color.RED;
this.m_vibrationInterval = 6;
this.m_radius = 100;
} else if (this.getPressure() >= 0.18 && this.getPressure() < 0.25) {
this.m_color = Color.BLUE;
this.m_vibrationInterval = 4;
this.m_radius = 70;
} else {
this.m_color = Color.YELLOW;
this.m_vibrationInterval = 2;
this.m_radius = 50;
}
this.m_ball = new Ball(getApplicationContext(), this.getX(), this
.getY(), this.m_radius, this.getColor());
this.m_floatingText = new FloatingText(getApplicationContext(), this.getX()-(this.m_radius/2), this
.getY()-this.m_radius, ""+ this.getPressure(), Color.WHITE );
}
public FloatingText getFloatingText(){
return m_floatingText;
}
public int getVibrationInterval() {
return this.m_vibrationInterval;
}
/**
* Á¤ÀÇµÈ Paint »ö °ªÀ» ¹ÝȯÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @return
*/
public int getColor() {
return this.m_color;
}
/**
* Paint »öÀ» ÇÒ´çÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @param color
*/
public void setColor(int color) {
this.m_color = color;
}
/**
* ¾Ð·Â °ªÀ» ¹ÝȯÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @return
*/
public float getPressure() {
return this.m_pressure;
}
/**
* ¾Ð·Â °ªÀ» Á¤ÀÇÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @param pressure
*/
public void setPressure(float pressure) {
this.m_pressure = pressure;
}
/**
* Æ÷ÀÎÅÍ ¾ÆÀ̵𸦠¹ÝȯÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @return
*/
public int getPointerId() {
return this.m_pointerId;
}
/**
* Æ÷ÀÎÅÍ À妽º¸¦ ¹ÝȯÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @return
*/
public int getPointerIndex() {
return this.m_pointerIndex;
}
/**
* x ÁÂÇ¥°ªÀ» ¹ÝȯÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @return
*/
public float getX() {
return this.m_x;
}
/**
* y ÁÂÇ¥°ªÀ» ¹ÝȯÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @return
*/
public float getY() {
return this.m_y;
}
/**
* µµÇü ÀνºÅϽº¸¦ ¹ÝȯÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @return
*/
public Ball getBall() {
return this.m_ball;
}
/**
* °¢ ÀνºÅϽº °ªÀ» ÃʱâÈÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*/
public void setInit() {
this.m_pointerId = -1;
this.m_pointerIndex = -1;
this.m_x = -1;
this.m_y = -1;
this.m_pressure = -1;
this.m_color = Color.GREEN;
this.m_ball = null;
}
}
/**
* ¸ð¼Ç À̺¥Æ®¸¦ ´ã¾ÆµÎ´Â ArrayList ÀνºÅϽºÀÔ´Ï´Ù.
*/
private ArrayList<EventNode> m_aEventNodes = new ArrayList<EventNode>();
/**
* Activity »ý¼º½Ã Á¢±Ù ¸Þ¼Òµå
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ·¹À̾ƿô ÀνºÅϵå ÇÒ´ç
m_mainLayout = (FrameLayout) this.findViewById(R.id.main);
// µð¹ö±× ÅؽºÆ® ºä ÀνºÅϽº ÇÒ´ç
m_tvDebugText = (TextView) this.findViewById(R.id.debug_text);
m_tvDebugText2 = (TextView) this.findViewById(R.id.debug_text2);
}
/**
* ÅÍÄ¡ÇßÀ»¶§ ¹ÝÀÀÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù.
*
* @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
m_eventCnt = event.getPointerCount();
m_tvDebugText.setVisibility(View.VISIBLE);
m_tvDebugText.setText("Points : " + m_eventCnt
+ " / EventNodes : " + m_aEventNodes.size());
switch (event.getAction()) {
// ´·¶À» ¶§
case MotionEvent.ACTION_DOWN:
drawBall(event);
return true;
// ´©¸£°í À̵¿ÇÒ ¶§
case MotionEvent.ACTION_MOVE:
moveBall(event);
return true;
// ¶¼¾úÀ» ¶§
case MotionEvent.ACTION_UP:
removeBall();
m_tvDebugText.setVisibility(View.GONE);
return true;
}
return false;
}
/**
* µµÇü ±×¸®´Â ¸Þ¼Òµå
*
* @param event
*/
private void drawBall(MotionEvent event) {
removeBall();
for (int i = 0; i < m_eventCnt; i++) {
EventNode eventNode = new EventNode(event, i);
m_aEventNodes.add(eventNode);
m_mainLayout.addView(eventNode.getBall());
m_mainLayout.addView(eventNode.getFloatingText());
((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(eventNode.getVibrationInterval());
}
}
/**
* µµÇü À̵¿ ó¸® ¸Þ¼Òµå
*
* @param event
*/
private void moveBall(MotionEvent event) {
removeBall();
drawBall(event);
}
/**
* µµÇü Á¦°Å ó¸® ¸Þ¼Òµå
*/
private void removeBall() {
int nSize = m_aEventNodes.size();
for (int i = 0; i < nSize; i++) {
m_mainLayout.removeView(m_aEventNodes.get(0).getBall());
m_mainLayout.removeView(m_aEventNodes.get(0).getFloatingText());
m_aEventNodes.remove(0);
}
}
}
|
|
̵̧ : 271 |
̵̧
¸ñ·Ï
|
|
| |
|