(Android) Membuat Multi Touch

Feb. 19, 2017, 5:20 p.m. 5 2532

Salam semangat Mas Dan Mbak bro developer android, kemaren kita sudah utak – atik windows phone, kebetulan ada sedikit clien yang memesan suatu app, kebetulan saya kewalahan dalam mebuat app tersebut, ternyata pada membuat multitouch tidah hanya device yang mendukung kita harus mengaktifkan sedikit beberapa fungsi yang harus di perhatikan, apa lagi kalau kita sudah bermain dengan user interface Games nah ini sangat berguna, tapi postingan ini saya berikan yang sangat sederhaan, agar teman – teman lebih mudah untuk memahaminya…. kita langsung ke tkp..

 

1. Create PRoject dengan nama MultiTouchAnnabe

2. Create Class dengan nama MultiTouchActivity

disini kita membuat aktivitas dari jarak pada lembar kerja dan bagian mana saja yang akan bisa di sentuh atau di tekan dengan beberapa jari.

package com.example.multitouch;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;

public class MultitouchActivity extends Activity implements OnTouchListener{
	public static final int MAX_POINTERS = 10;

	private MultiTouchTrackingView touchView;
	Paint paint;
	Paint paintInfoText;

	PointF[] points;
	private int[] lastActions;
	int pointerCount;

	private float displayDensity;
	int radius;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		displayDensity = getResources().getDisplayMetrics().density;

		points = new PointF[MAX_POINTERS];
		lastActions = new int[MAX_POINTERS];

		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setTextSize(calcDevicePixels(15));

		paintInfoText = new Paint();
		paintInfoText.setColor(Color.BLACK);
		paintInfoText.setAntiAlias(true);
		paintInfoText.setTextSize(calcDevicePixels(18));

		touchView = new MultiTouchTrackingView(this, this);
		touchView.setOnTouchListener(this);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(touchView);

		radius = calcDevicePixels(40);
	}

	public int calcDevicePixels(int deviceIndependentPixel) {
		return (int) (deviceIndependentPixel * displayDensity + 0.5f);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction() & MotionEvent.ACTION_MASK;
		int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
		pointerCount = event.getPointerCount();

		int actionId = event.getPointerId(pointerIndex);
		Log.d("greenrobot", "MotionEvent - pointer ID:  " + actionId
				+ ", action: " + mapActionCodeToString(action)
				+ ", pointer count: " + pointerCount);
		if (actionId < MAX_POINTERS) {
			lastActions[actionId] = action;
		}

		for (int i = 0; i < pointerCount; i++) {
			int pointerId = event.getPointerId(i);
			if (pointerId < MAX_POINTERS) {
				points[pointerId] = new PointF(event.getX(i), event.getY(i));
				if (action == MotionEvent.ACTION_MOVE) {
					lastActions[pointerId] = action;
				}
			}
		}

		touchView.invalidate();
		return true;
	}

	public int getColor(int pointerId) {
		int color;
		switch (lastActions[pointerId]) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN:
			color = 0xaa0000ff; // BLUE
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			color = 0xaa999999; // GREY
			break;
		case MotionEvent.ACTION_MOVE:
			color = 0xaa00ff00; // GREEN
			break;
		default:
			color = 0xaaFF0000; // RED
		}
		return color;
	}

	public String getActionText(int pointerId) {
		String action = mapActionCodeToString(lastActions[pointerId]);
		return pointerId + ": " + action;
	}

	private String mapActionCodeToString(int actionCode) {
		String action;
		switch (actionCode) {
		case MotionEvent.ACTION_DOWN:
			action = "Down";
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			action = "Pointer Down";
			break;
		case MotionEvent.ACTION_UP:
			action = "Up";
			break;
		case MotionEvent.ACTION_POINTER_UP:
			action = "Pointer Up";
			break;
		case MotionEvent.ACTION_MOVE:
			action = "Move";
			break;
		default:
			action = "Other (" + actionCode + ")";
		}
		return action;
	}
}

 

3. Create Class dengan nama MultiTouchTrackingView

disini kita membuat radius untuk menjaga jarak antar touch

package com.example.multitouch;

import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.view.View;

class MultiTouchTrackingView extends View{
	private final MultitouchActivity multitouchActivity;

	public MultiTouchTrackingView(MultitouchActivity multitouchActivity,
			Context context) {
		super(context);
		this.multitouchActivity = multitouchActivity;
		setBackgroundColor(Color.WHITE);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for (int i = 0; i < MultitouchActivity.MAX_POINTERS; i++) {
			PointF point = this.multitouchActivity.points[i];
			if (point != null) {
				this.multitouchActivity.paint.setColor(this.multitouchActivity
						.getColor(i));
				canvas.drawCircle(point.x, point.y,
						this.multitouchActivity.radius,
						this.multitouchActivity.paint);
				String text = this.multitouchActivity.getActionText(i);
				float textWidth = this.multitouchActivity.paint
						.measureText(text);
				canvas.drawText(text, point.x - textWidth / 2, point.y
						- this.multitouchActivity.radius
						- this.multitouchActivity.calcDevicePixels(8),
						this.multitouchActivity.paint);
			}
		}
		canvas.drawText("Last pointer count: "
				+ this.multitouchActivity.pointerCount, 10,
				this.multitouchActivity.calcDevicePixels(30),
				this.multitouchActivity.paintInfoText);
	}
}

 

4. Create Class dengan nama SingelMultiTouchMoveVIew

Disini kita membuat tampilan dan penyimpanan letak dari akhir touch di lepas..

package com.example.multitouch;

import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SingleTouchMoveMeView extends View{
	private Drawable mIcon;
	private float mPosX;
	private float mPosY;

	private float mLastTouchX;
	private float mLastTouchY;

	public SingleTouchMoveMeView(Context context) {
		this(context, null, 0);
	}

	public SingleTouchMoveMeView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SingleTouchMoveMeView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);

		mIcon = context.getResources().getDrawable(R.drawable.ic_launcher);
		mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(),
				mIcon.getIntrinsicHeight());
	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.save();
		canvas.translate(mPosX, mPosY);
		mIcon.draw(canvas);
		canvas.restore();
	}

	// Let the user move the icon around
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		final int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN: {
			final float x = ev.getX();
			final float y = ev.getY();

			// Remember where we started
			mLastTouchX = x;
			mLastTouchY = y;
			break;
		}

		case MotionEvent.ACTION_MOVE: {
			final float x = ev.getX();
			final float y = ev.getY();

			// Calculate the distance moved
			final float dx = x - mLastTouchX;
			final float dy = y - mLastTouchY;

			// Move the object
			mPosX += dx;
			mPosY += dy;

			// Remember this touch position for the next move event
			mLastTouchX = x;
			mLastTouchY = y;

			// Invalidate to request a redraw
			invalidate();
			break;
		}
		}

		return true;
	}
}

 

5. Buka class java MainActivity.java

disini kita mengganti view R.main mengganti menjadi class yang kita buat sebelumnya yaitu SinglemultiToucheMoveView.java

package com.example.multitouch;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SingleTouchMoveMeView(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

 

Run dan hasilnya seperti di bawah ini…

 

Tentang Penulis

Vimio

Kalau bisa jadi 10 akan kita jadikan semua nya 10, 10 jika nilai 0 - 10 yang tertinggi. tapi sayangnya ketika nilai dan niat saling menghianati. bersyukurlah semua apa yang kita dapat dan yang telah di berikan. ya itu nilai 10 yang dikasih. { 1 0 }

Komentar