May 8, 2011

Set background of your app to be transparent

To set background of your app to be transparent, you can use Android pre-defined Theme.Translucent.

Modify AndroidManifest.xml, to add the code inside <activity>
android:theme="@android:style/Theme.Translucent"

example:

Set background of your app to be transparent

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidTranslucent"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidTranslucent"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

May 6, 2011

Handle onTouchEvent in SurfaceView

Here Override onTouchEvent() in SurfaceView to handle user touch event.

Handle onTouchEvent in SurfaceView

package com.TestSurefaceView;

import java.util.Random;
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.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TestSurefaceView extends Activity {
 
 MySurfaceView mySurfaceView;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mySurfaceView = new MySurfaceView(this);
       setContentView(mySurfaceView);
   }
  
   @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  mySurfaceView.onResumeMySurfaceView();
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  mySurfaceView.onPauseMySurfaceView();
 }

 class MySurfaceView extends SurfaceView implements Runnable{
    
    Thread thread = null;
    SurfaceHolder surfaceHolder;
    volatile boolean running = false;
    
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Random random;
    
    volatile boolean touched = false;
    volatile float touched_x, touched_y;

  public MySurfaceView(Context context) {
   super(context);
   // TODO Auto-generated constructor stub
   surfaceHolder = getHolder();
   random = new Random();
  }
  
  public void onResumeMySurfaceView(){
   running = true;
   thread = new Thread(this);
   thread.start();
  }
  
  public void onPauseMySurfaceView(){
   boolean retry = true;
   running = false;
   while(retry){
    try {
     thread.join();
     retry = false;
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

  @Override
  public void run() {
   // TODO Auto-generated method stub
   while(running){
    if(surfaceHolder.getSurface().isValid()){
     Canvas canvas = surfaceHolder.lockCanvas();
     //... actual drawing on canvas

     paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
     
     int w = canvas.getWidth();
     int h = canvas.getHeight();
     int x = random.nextInt(w-1); 
     int y = random.nextInt(h-1);
     int r = random.nextInt(255);
     int g = random.nextInt(255);
     int b = random.nextInt(255);
     paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
     canvas.drawPoint(x, y, paint);
     
     if(touched){
      paint.setStrokeWidth(50);
      paint.setColor(Color.BLACK);
      canvas.drawPoint(touched_x, touched_y, paint);
     }
     
     surfaceHolder.unlockCanvasAndPost(canvas);
    }
   }
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
   // TODO Auto-generated method stub
   
   touched_x = event.getX();
   touched_y = event.getY();
   
   int action = event.getAction();
   switch(action){
   case MotionEvent.ACTION_DOWN:
    touched = true;
    break;
   case MotionEvent.ACTION_MOVE:
    touched = true;
    break;
   case MotionEvent.ACTION_UP:
    touched = false;
    break;
   case MotionEvent.ACTION_CANCEL:
    touched = false;
    break;
   case MotionEvent.ACTION_OUTSIDE:
    touched = false;
    break;
   default:
   }
   return true; //processed
  }
    
   }
}


Related Post:
- A basic implementation of SurfaceView
- Drawing on SurfaceView
- Detect multi-touch, on SurfaceView
- Draw path on SurfaceView's canvas

May 5, 2011

Drawing on SurfaceView

Last post introduce "A basic implementation of SurfaceView". To draw something on the SurfaceView, place the codes in-between surfaceHolder.lockCanvas() and surfaceHolder.unlockCanvasAndPost(canvas).

Drawing on SurfaceView

package com.TestSurefaceView;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TestSurefaceView extends Activity {
 
 MySurfaceView mySurfaceView;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mySurfaceView = new MySurfaceView(this);
       setContentView(mySurfaceView);
   }
  
   @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  mySurfaceView.onResumeMySurfaceView();
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  mySurfaceView.onPauseMySurfaceView();
 }

 class MySurfaceView extends SurfaceView implements Runnable{
    
    Thread thread = null;
    SurfaceHolder surfaceHolder;
    volatile boolean running = false;
    
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Random random;

  public MySurfaceView(Context context) {
   super(context);
   // TODO Auto-generated constructor stub
   surfaceHolder = getHolder();
   random = new Random();
  }
  
  public void onResumeMySurfaceView(){
   running = true;
   thread = new Thread(this);
   thread.start();
  }
  
  public void onPauseMySurfaceView(){
   boolean retry = true;
   running = false;
   while(retry){
    try {
     thread.join();
     retry = false;
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

  @Override
  public void run() {
   // TODO Auto-generated method stub
   while(running){
    if(surfaceHolder.getSurface().isValid()){
     Canvas canvas = surfaceHolder.lockCanvas();
     //... actual drawing on canvas

     paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
     
     int w = canvas.getWidth();
     int h = canvas.getHeight();
     int x = random.nextInt(w-1); 
     int y = random.nextInt(h-1);
     int r = random.nextInt(255);
     int g = random.nextInt(255);
     int b = random.nextInt(255);
     paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
     canvas.drawPoint(x, y, paint);
     
     surfaceHolder.unlockCanvasAndPost(canvas);
    }
   }
  }
    
   }
}


Related Post:
- Handle onTouchEvent in SurfaceView
- Draw path on SurfaceView's canvas

May 4, 2011

A basic implementation of SurfaceView

SurfaceView provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.

Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by calling getHolder().

package com.TestSurefaceView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TestSurefaceView extends Activity {

MySurfaceView mySurfaceView;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mySurfaceView = new MySurfaceView(this);
       setContentView(mySurfaceView);
   }
  
   @Override
protected void onResume() {
 // TODO Auto-generated method stub
 super.onResume();
 mySurfaceView.onResumeMySurfaceView();
}

@Override
protected void onPause() {
 // TODO Auto-generated method stub
 super.onPause();
 mySurfaceView.onPauseMySurfaceView();
}

class MySurfaceView extends SurfaceView implements Runnable{
   
    Thread thread = null;
    SurfaceHolder surfaceHolder;
    volatile boolean running = false;

 public MySurfaceView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  surfaceHolder = getHolder();
 }

 public void onResumeMySurfaceView(){
  running = true;
  thread = new Thread(this);
  thread.start();
 }

 public void onPauseMySurfaceView(){
  boolean retry = true;
  running = false;
  while(retry){
   try {
    thread.join();
    retry = false;
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 @Override
 public void run() {
  // TODO Auto-generated method stub
  while(running){
   if(surfaceHolder.getSurface().isValid()){
    Canvas canvas = surfaceHolder.lockCanvas();
    //... actual drawing on canvas
   
    surfaceHolder.unlockCanvasAndPost(canvas);
   }
  }
 }
   
   }
}


next:
- Drawing on SurfaceView
- Handle onTouchEvent in SurfaceView
- Draw path on SurfaceView's canvas

May 1, 2011

Detect Multi-Touch Event, test on Custom View

Detect Multi-Touch Event, test on Custom View

package com.TestMultiTouch;

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.view.MotionEvent;
import android.view.View;

public class TestMultiTouch extends Activity {

public class MultiTouchView extends View{

//In this test, handle maximum of 2 pointer
final int MAX_POINT_CNT = 2;

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float[] x = new float[MAX_POINT_CNT];
float[] y = new float[MAX_POINT_CNT];
boolean[] isTouch = new boolean[MAX_POINT_CNT];

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

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if(isTouch[0]){
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.RED);
canvas.drawCircle(x[0], y[0], 50f, paint);
}
if(isTouch[1]){
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.BLUE);
canvas.drawCircle(x[1], y[1], 50f, paint);
}
}

@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 motionEvent) {
// TODO Auto-generated method stub

int pointerIndex = ((motionEvent.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT);
int pointerId = motionEvent.getPointerId(pointerIndex);
int action = (motionEvent.getAction() & MotionEvent.ACTION_MASK);
int pointCnt = motionEvent.getPointerCount();

if (pointCnt <= MAX_POINT_CNT){
if (pointerIndex <= MAX_POINT_CNT - 1){

for (int i = 0; i < pointCnt; i++) {
int id = motionEvent.getPointerId(i);
x[id] = (int)motionEvent.getX(i);
y[id] = (int)motionEvent.getY(i);
}

switch (action){
case MotionEvent.ACTION_DOWN:
isTouch[pointerId] = true;
break;
case MotionEvent.ACTION_POINTER_DOWN:
isTouch[pointerId] = true;
break;
case MotionEvent.ACTION_MOVE:
isTouch[pointerId] = true;
break;
case MotionEvent.ACTION_UP:
isTouch[pointerId] = false;
break;
case MotionEvent.ACTION_POINTER_UP:
isTouch[pointerId] = false;
break;
case MotionEvent.ACTION_CANCEL:
isTouch[pointerId] = false;
break;
default:
isTouch[pointerId] = false;
}
}
}

invalidate();
return true;
}

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MultiTouchView(this));
}
}



Related Post:
- Detect Touch Event
- Detect Touch Event, test on Custom View
- Detect Multi-Touch Event

Infolinks In Text Ads