Friday, May 20, 2011

A Single touch view + a multi touch view in a activity

In the exercise "Detect single touch on custom View" and "Detect Multi touch on custom View", we have implemented a single touch view class and a multi touch view class. We are going to place both in a single activity.



Tested on Nexus One running 2.2.1.

The upper frame is a single touch view.
The lower frame is a multi touch view.

Keep no change on the and SingleTouchView.java in "Detect single touch on custom View", and MultiTouchView.java in "Detect Multi touch on custom View".

Modify main.xml to include both SingleTouchView and MultiTouchView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.exercise.AndroidTouch.SingleTouchView
android:id="@+id/stv1"
android:layout_width="fill_parent"
android:layout_height="200dp"
/>
<com.exercise.AndroidTouch.MultiTouchView
android:id="@+id/mtv2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>


Modify AndroidTouch.java to set them in different background color.
package com.exercise.AndroidTouch;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class AndroidTouch extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

SingleTouchView singleTouchView1 = (SingleTouchView)findViewById(R.id.stv1);
MultiTouchView multiTouchView2 = (MultiTouchView)findViewById(R.id.mtv2);
singleTouchView1.setBackgroundColor(Color.GRAY);
multiTouchView2.setBackgroundColor(Color.LTGRAY);

}
}


Download the files.

No comments:

Post a Comment