ȸ¿ø°¡ÀԡžÆÀ̵ð/ºñ¹øã±â
ȨÀ¸·Î

[°³¹ß 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);
    }
  }

}
ÃßõÃßõ : 236 Ãßõ ¸ñ·Ï
¹øÈ£ Á¦¸ñ
1,136
Android VideoView Example
1,135
[°³¹ß Tip] WebView ·Îµù½Ã ŸÀÌƲ¹Ù¿¡ ÁøÇàÁß ¾ÆÀÌÄÜ º¸ÀÌ°Ô Çϱâ
1,134
[°³¹ß Tip] webview ½ºÅ©·Ñ
1,133
[°³¹ß Tip] WebView ¿¡¼­ ÀÚ¹Ù ½ºÅ©¸³Æ® »ç¿ë¿¹
1,132
[°³¹ß Tip] WebView À¥ÆäÀÌÁö ·Îµù½Ã°£ ±¸Çϱâ
1,131
[°³¹ß Tip] WebView ÆäÀÌÁö°¡ ·ÎµùµÇ¾úÀ»¶§ ó¸®
1,130
[°³¹ß Tip] WebView¿¡ »õâ(href)ÀÌ ¶ã¶§ À¥ºê¶ó¿ìÁ®°¡ ¾Æ´Ñ ÇöÀç WebView·Î Ãâ·ÂÇϱâ
1,129
[°³¹ß Tip] WebView¿¡¼­ ¸ÖƼ ÅÍÄ¡ °¡´ÉÇÏ°Ô Çϱâ
1,128
[°³¹ß Tip] WebView¿¡¼­ ¸ÖƼ ÅÍÄ¡ ÁÜ °¡´ÉÇÏ°Ô Çϱâ
1,127
[°³¹ß Tip] WebView¿¡¼­ ¹®ÀÚ¿­ ¼±ÅÃÇϱâ
1,126
[°³¹ß Tip] webview¿¡¼­ À¯Æ©ºê µ¿ÀÛ½ÃÅ°±â
1,125
[°³¹ß Tip] Wifi ¿¬°á »óÅ °¨ÁöÇϱâ
1,124
[°³¹ß Tip] ³»ÀåµÈ ¾îÇ÷ΠSMS ¸Þ½ÃÁö º¸³»±â
1,123
[°³¹ß Tip] ´Ü¸»±â Æù¹øÈ£ ¾Ë¾Æ³»±â
1,122
[°³¹ß Tip] ¸®¼Ò½º¸íÀ¸·Î ÇØ´ç ¸®¼Ò½º ID ±¸Çϱâ
[°³¹ß Tip] ¸ÖƼÅÍÄ¡ (Multi Touch) ±¸Çö ¼Ò½º
1,120
[°³¹ß Tip] ºÎÆýà ¼­ºñ½º ½ÇÇàÇϱâ
1,119
[°³¹ß Tip] ¾Èµå·ÎÀÌµå ±â±âº° °íÀ¯ ID
1,118
[°³¹ß Tip] ¾ÈÅ׳ª ¿µ¿ª ¾ø¾Ö±â
1,117
[°³¹ß Tip] ¾×Ƽ¹öƼ(Activity)¿¡¼­ ¾Ö´Ï¸ÞÀÌ¼Ç È¿°úÁÖ±â
1,116
[°³¹ß Tip] ¾×Ƽ¹öƼÀÇ Å¸ÀÌƲ¹Ù Á¦°Å, ¾ÈÅ׳ª ¿µ¿ª±îÁö Á¦°Å
1,115
[°³¹ß Tip] ¿Àµð¿À / ºñµð¿À ½ÇÇà
1,114
[°³¹ß Tip] ÀüÈ­°¡ ¿ÔÀ»¶§ À̺¥Æ® ¼ö½ÅÇϱâ
1,113
[°³¹ß Tip] ÆÄÀÏ(File) »ç¿ë
1,112
[°³¹ß Tip] Æù ¸¶ÀÌÅ©¿¡ ³ìÀ½Çϱâ
1,111
[°³¹ß Tip] ÇØ´ç ActionÀ» ¼öÇàÇÒ¼ö°¡ ÀÖ´Â ¾×Ƽ¹öƼ(¾îÇÃ)ÀÌ Á¸ÀçÇÏ´ÂÁö ã±â
1,110
[°³¹ß Tip] ÇöÀç µð¹ÙÀ̽º(Æù)ÀÇ IP ÁÖ¼Ò ¾Ë¾Æ³»±â
1,109
[°³¹ß Tip] XmlPullParser¸¦ ÀÌ¿ëÇؼ­ rss³» title Ç׸ñ Àбâ
1,108
[°³¹ß Tip] ÇöÀç ¿¬ÁÖÁßÀÎ ¿Àµð¿À ¾îÇÃÀ» À½¼Ò°Å Çϱâ
1,107
[°³¹ß Tip] È­¸é »ó´ÜÀÇ Å¸ÀÌƲ¹Ù (»óŹ٠°¨Ãß±â)
¸ñ·Ï
¹ÂÁ÷Æ®·ÎÆ® ºÎ»ê±¤¿ª½Ã ºÎ»êÁø±¸ °¡¾ßµ¿ ¤Ó °³ÀÎÁ¤º¸Ãë±Þ¹æħ
Copyright ¨Ï musictrot All rights reserved.