May
07
2012
0

Eclipse 4 RCP Tutorial updated to M7 and Eclipse 4 book (Early Access Version) soon available

I updated the Eclipse 4 RCP Tutorial to Eclipse 4.2 M7.

This week I’m in the process of delivering a 5 days Eclipse 4 training together with Marcel Bruch and M7 really shines in terms of stability.

M7 contains lots of small enhancements which make adapting Eclipse 4 much nicer, my personal favorite is Bug report: Application.e4xmi should be optional for which Sopot Cela provided a patch. With this patch you can convert an Eclipse plug-in into an Eclipse 4 application without creating manually any extension in the plugin.xml file.

Please give Eclipse 4 M7 a try and report bugs you find in Bugzilla.

I also thinks its about time to release my Eclipse 4 book for the Kindle. This will be an “Early Access Version” due to the fact that Eclipse 4 is still not final and that certains chapters still need some rework but should hopefully be a good reference. I plan to provide updates for this version once Eclipse 4 is released via the Kindle store.

I hope that I will be able to publish this version of the Eclipse 4 book next week. Fingers crossed. -)

Written by admin in: sample code |
May
02
2012
0

Map the touch position with a mask

In previous exercise, I show how to:
- Detect touched position on a ImageView
- Get color on a specified location from ImageView’s background bitmap
- Display text on a specified location in a custom View

In this step, I want to convert the touch position to a meaningful description, such as Head, Body, Heart, Hand and Foot. As show in the video here.

I make(copy) the Android Bitmap as the image source of the custom ImageView, TouchView. And re-paint the image with specified color for various part. Both images have the same size, and same pattern. Copy them in /res/drawable/ folder.

When I get the touch position from onTouchEvent() method in TouchView, I get the pixel color from the mask image, instead of get from the original image. So I can detect the touched part by comparing the mask pixel color with various specified color.

In order to compare the Android image and the mask image, I have to make both in same proportion. So I re-assign main.xml to use Anadroid:src instead of android:background. And I use fixed layout size for the FrameLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
 <FrameLayout
        android:layout_width="384px"
        android:layout_height="384px"
     >

     <com.exercise.AndroidDetechTouch.TouchView
         android:id="@+id/myandroid"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:src="@drawable/android"
         />
     <com.exercise.AndroidDetechTouch.InfoView
         android:id="@+id/infoview"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         />

 </FrameLayout>
</LinearLayout>

The custom ImageView, TouchView.java

package com.exercise.AndroidDetechTouch;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class TouchView extends ImageView {

 Bitmap bitmap, mask;
 double bmWidth, bmHeight; 

 String touchInfo;
 float touchX, touchY;

 String part;

 public TouchView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  init();
 }

 public TouchView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  init();
 }

 public TouchView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
  init();
 }

 private void init(){

  bitmap = ((BitmapDrawable)getDrawable()).getBitmap();
  mask = BitmapFactory.decodeResource(getResources(), R.drawable.android_mask);
  bmWidth = (double)bitmap.getWidth();
  bmHeight = (double)bitmap.getHeight();
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
       MeasureSpec.getSize(heightMeasureSpec));
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub

  switch(event.getAction()){
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_MOVE:
   touchX = event.getX();
   touchY = event.getY();

   long maskColor = getColor(touchX, touchY);

   //Match the color in Mask bitmap
   if(maskColor == Color.RED){
    touchInfo = "Heart";
   }else if(maskColor == Color.GREEN){
    touchInfo = "Head";
   }else if(maskColor == Color.BLUE){
    touchInfo = "Body";
   }else if(maskColor == -256){
    touchInfo = "Hand";
   }else if(maskColor == -16711681){
    touchInfo = "Foot";
   }else{
    touchInfo = "";
   }

   ((AndroidDetechTouchActivity)getContext()).updateMsg(touchInfo, touchX, touchY, (int)maskColor);
   return true;

  default:
   return false;
  }

 }

 private long getColor(float x, float y){

  if ( x < 0 || y < 0 || x > (float)getWidth() || y > (float)getHeight()){
   return 0; //Invalid, return 0
  }else{
   //Convert touched x, y on View to on Bitmap
   int xBm = (int)(x * (bmWidth / (double)getWidth()));
      int yBm = (int)(y * (bmHeight / (double)getHeight()));

   return mask.getPixel(xBm, yBm);
  }
 }

}

InfoView.java, the wording will be display here.

package com.exercise.AndroidDetechTouch;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class InfoView extends View {

 String info = "";
 float x = 0; //init value
 float y = 0; //init value
 int color = Color.BLACK;

 public InfoView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

 public InfoView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }

 public InfoView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }

 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);

  Paint paint = new Paint();
  paint.setStyle(Paint.Style.FILL_AND_STROKE);
  paint.setColor(color);
  paint.setStrokeWidth(2);
  paint.setTextSize(30);

  canvas.drawLine(x-10, y, x+10, y, paint);
  canvas.drawLine(x, y-10, x, y+10, paint);
  canvas.drawText(info, x, y, paint);

 }

 public void updateInfo(String t_info, float t_x, float t_y, int t_c){
  info = t_info;
  x = t_x;
  y = t_y;
  color = t_c;
  invalidate();
 }

 public void clearInfo(){
  info = "";
  x = 0;
  y = 0;
  invalidate();
 }

}

main activity, AndroidDetechTouchActivity.java

package com.exercise.AndroidDetechTouch;

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

public class AndroidDetechTouchActivity extends Activity {

 TouchView myAndroid;
 InfoView infoView;

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

        myAndroid = (TouchView)findViewById(R.id.myandroid);
        infoView = (InfoView)findViewById(R.id.infoview);

    }

    public void updateMsg(String t_info, float t_x, float t_y, int t_c){

     infoView.updateInfo(t_info, t_x, t_y, t_c);

    }

    public void clearMsg(){

     infoView.clearInfo();

    }

}

Download the files.


Written by admin in: android, sample code |
Apr
30
2012
0

Android: How to check if a LocationProvider is available?

In Android the LocationProvider for example for GPS might not be available.

You can find out if a LocationManager is enabled via the isProviderEnabled() method. If its not enabled you can send the user to the settings via an Intent with the Settings.ACTION_LOCATION_SOURCE_SETTINGS action for the android.provider.Settings class.

This is demonstrated via the following code snippets:


LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
	.isProviderEnabled(LocationManager.GPS_PROVIDER);

// Check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled) {
  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	startActivity(intent);
}

You find more info about the LocationManager in Location API and Google Maps in Android – Tutorial

Written by admin in: sample code |

AndGPS | Connect Android Phone To GPS Solutions