출처 : http://miksnug.pe.kr/24
Android 에서 Multi Touch 를 구현한 Code 입니다.
허접하지만 튜닝하면 좀 더 나아지지 않을까 생각합니다.
+ MainActivity.java
+ main.xml
+ strings.xml
+ AndroidManifest.xml
허접하지만 튜닝하면 좀 더 나아지지 않을까 생각합니다.
+ MainActivity.java
- 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;
- public class MainActivity extends Activity {
- private FrameLayout m_mainLayout = null;
- TextView m_tvDebugText = null;
- TextView m_tvDebugText2 = null;
- private int m_eventCnt = 0;
- class Ball extends View {
- public float m_x;
- public float m_y;
- private int m_r;
- private Paint m_paint = new Paint(Paint.LINEAR_TEXT_FLAG);
- 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;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawCircle(m_x, m_y, m_r, m_paint);
- super.onDraw(canvas);
- }
- }
- class FloatingText extends View {
- public float m_x;
- public float m_y;
- private String m_t;
- private Paint m_paint = new Paint(Paint.LINEAR_TEXT_FLAG);
- 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;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawText(m_t, m_x, m_y, m_paint);
- super.onDraw(canvas);
- }
- }
- class EventNode {
- private int m_pointerId = -1;
- private int m_pointerIndex = -1;
- private float m_x = -1;
- private float m_y = -1;
- private float m_pressure = -1;
- private Ball m_ball = null;
- private int m_color = Color.YELLOW;
- int m_vibrationInterval = 2;
- int m_radius = 50;
- FloatingText m_floatingText = null;
- 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;
- }
- public int getColor() {
- return this.m_color;
- }
- public void setColor(int color) {
- this.m_color = color;
- }
- public float getPressure() {
- return this.m_pressure;
- }
- public void setPressure(float pressure) {
- this.m_pressure = pressure;
- }
- public int getPointerId() {
- return this.m_pointerId;
- }
- public int getPointerIndex() {
- return this.m_pointerIndex;
- }
- public float getX() {
- return this.m_x;
- }
- public float getY() {
- return this.m_y;
- }
- 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;
- }
- }
- private ArrayList<EventNode> m_aEventNodes = new ArrayList<EventNode>();
- @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);
- }
- @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;
- }
- 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());
- }
- }
- 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);
- }
- }
- }
+ main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main" android:orientation="vertical"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
- <TextView android:id="@+id/debug_text2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="left" />
- <TextView android:id="@+id/debug_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="right"/>
- </LinearLayout>
- </FrameLayout>
+ strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">AndroidMultiTouch</string>
- </resources>
+ AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="kr.pe.miksnug"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name"
- android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
- </manifest>
추가
--------------------------------------------------------------------
안드로이드 데브가이드 사이트에서 매니페스트에
<uses-permission android:name="android.hardware.touchscreen.multitouch"/>
이 퍼미션도 주란다..
'프로그래밍 > 안드로이드' 카테고리의 다른 글
[펌]java 의 스트링 인코딩이해( 자바 한글 깨짐해법을 위한연구) (0) | 2010.12.08 |
---|---|
[펌][코딩] 안드로이드 유용한 Activity Flag들 (0) | 2010.07.24 |
Android requires .class compatibility set to 5.0. Please fix project properties. (0) | 2010.04.23 |
[펌] Project 'xxx' is missing required source folder: 'gen' 해결하기 (0) | 2010.04.14 |
안드로이드 에뮬 실행시 에러... (0) | 2010.04.06 |