Thursday, May 3, 2012

Android - Basic Gesture Detection

//Implement the following events in your class


public class ActivitySwipeDetector implements View.OnTouchListener {
static final String logTag = "ActivitySwipeDetector";
static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;

public void onRightToLeftSwipe(){
   
Log.i(logTag, "RightToLeftSwipe!");
    activity
.doSomething();
}
public void onLeftToRightSwipe(){
   
Log.i(logTag, "LeftToRightSwipe!");
    activity
.doSomething();
}
public void onTopToBottomSwipe(){
   
Log.i(logTag, "onTopToBottomSwipe!");
    activity
.doSomething();
}
public void onBottomToTopSwipe(){
   
Log.i(logTag, "onBottomToTopSwipe!");
    activity
.doSomething();
}
public boolean onTouch(View v, MotionEvent event) {
   
switch(event.getAction()){
       
case MotionEvent.ACTION_DOWN: {
            downX
= event.getX();
            downY
= event.getY();
           
return true;
       
}
       
case MotionEvent.ACTION_UP: {
            upX
= event.getX();
            upY
= event.getY();

           
float deltaX = downX - upX;
           
float deltaY = downY - upY;

           
// swipe horizontal?
           
if(Math.abs(deltaX) > MIN_DISTANCE){
               
// left or right
               
if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
               
if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
           
}
           
else {
                   
Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                   
return false; // We don't consume the event
           
}

           
// swipe vertical?
           
if(Math.abs(deltaY) > MIN_DISTANCE){
               
// top or down
               
if(deltaY < 0) { this.onTopToBottomSwipe(); return true; }
               
if(deltaY > 0) { this.onBottomToTopSwipe(); return true; }
           
}
           
else {
                   
Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
                   
return false; // We don't consume the event
           
}

           
return true;
       
}
   
}
   
return false;
}
}



//To Bind the touch events on your controls
ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
lowestLayout
= (RelativeLayout)this.findViewById(R.id.lowestLayout);
lowestLayout
.setOnTouchListener(activitySwipeDetector);

Sunday, April 29, 2012

Android - Open Gallery For Image

//Open Gallery To Select Image
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);


//To Show Image After Selection or Get its Path
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode) {
    case 0:
        if(resultCode == RESULT_OK){ 
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex); // file path of selected image
            cursor.close();
                   //  Convert file path into bitmap image using below line.
            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
          
                   // put  bitmapimage in your imageview
            yourimgView.setImageBitmap(yourSelectedImage);
        }
    }
}