0 comments

Flipper view with Animation Example


This Example will show you how to use flipper view that contain a few image and allow user to navigate the image by clicking on the button next or previous or user can touch and fling on the image to change the content of the flipper with animation.

for the first you should have 4 xml file for the animation. so you need to create directory name anim under res folder. see the image bellow.

push_left_in.xml

      
     


push_left_out.xml

      
     


push_right_in.xml

      
      


push_right_out.xml
  
      
      



This is your main.xml file

  
      
      
          

finally your java file.
FliperViewExampleActivity.java
package monstercodz.blogspot.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class FliperViewExampleActivity extends Activity implements OnGestureListener {
    /** Called when the activity is first created. */
 private int imageID[] = {R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
 private ViewFlipper viewFlipper = null;  
    private GestureDetector gestureDetector = null;
    private Button btn_next,btn_pre;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn_next = (Button)findViewById(R.id.btn_next);
        btn_pre = (Button)findViewById(R.id.btn_pre);
        
        btn_next.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    nextImage();
   }
  });
        
        btn_pre.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    previousImage();
   }
  });
        
        viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper); 
        // gestureDetector Object is used to detect gesture events
        gestureDetector = new GestureDetector(this); 
        for (int i = 0; i < imageID.length; i++)  
        { 
            ImageView image = new ImageView(this);  
            image.setImageResource(imageID[i]);  
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            viewFlipper.addView(image, new LayoutParams(  
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        }
    }
    
    private void nextImage(){
     this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,  
                R.anim.push_left_in));  
        this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
                R.anim.push_left_out));  
        this.viewFlipper.showNext(); 
    }
    
    private void previousImage(){
     this.viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,  
                R.anim.push_right_in));  
        this.viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
                R.anim.push_right_out));  
        this.viewFlipper.showPrevious();  
    }
    
 @Override
 public boolean onDown(MotionEvent arg0) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
  // TODO Auto-generated method stub
  if (e1.getX() - e2.getX() > 120)  
        {  
             nextImage();
            return true;  
        }
        else if (e1.getX() - e2.getX() < -120)  
        {  
          previousImage();
          return true;
        }  
        return true;
 }
 @Override
 public void onLongPress(MotionEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
  // TODO Auto-generated method stub
  return false;
 }
 @Override
 public void onShowPress(MotionEvent e) {
  // TODO Auto-generated method stub
  
 }
 @Override
 public boolean onSingleTapUp(MotionEvent e) {
  // TODO Auto-generated method stub
  return false;
 }
 
 @Override 
    public boolean onTouchEvent(MotionEvent event)  
    {  
        return gestureDetector.onTouchEvent(event);  
    }
}




You can download the sample project by clicking here
3 comments

Media player with progress bar Example

This example will show you how to use the media player to play the mp3 and progress bar will load with the media duration.



This is your main.xml file


    

this is your MP3PlayerActitivy.java
package monstercodz.blogspot.com;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Mp3PlayerActivity extends Activity implements OnPreparedListener {
    /** Called when the activity is first created. */
 private Button btnPlay;
 private Button btnPouse;
 private int current = 0;
 private boolean   running = true;
 private int duration = 0;
 private MediaPlayer mPlayer;
 private SeekBar mSeekBarPlayer;
 private TextView mMediaTime;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);
        btnPlay = (Button) findViewById(R.id.button1);
        btnPouse = (Button) findViewById(R.id.button2);
        mMediaTime = (TextView)findViewById(R.id.mediaTime);
        mSeekBarPlayer = (SeekBar)findViewById(R.id.progress_bar);
        mPlayer.setOnPreparedListener(this);
        
        mSeekBarPlayer.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
         @Override
         public void onStopTrackingTouch(SeekBar seekBar) {}
         @Override
         public void onStartTrackingTouch(SeekBar seekBar) {}
         @Override
         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
             if(fromUser){
                 mPlayer.seekTo(progress);
                 updateTime();
             }
         }
     });
        
        btnPlay.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    try {
     mPlayer.prepare();
    } catch (IllegalStateException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    mPlayer.start();
    mSeekBarPlayer.postDelayed(onEverySecond, 1000);
   }
  });
        
        
        btnPouse.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mPlayer.pause();
   }
  });
    }
    
    private Runnable onEverySecond = new Runnable() {
        @Override
        public void run(){
         if(true == running){
             if(mSeekBarPlayer != null) {
              mSeekBarPlayer.setProgress(mPlayer.getCurrentPosition());
             }
             
             if(mPlayer.isPlaying()) {
              mSeekBarPlayer.postDelayed(onEverySecond, 1000);
              updateTime();
             }
         }
        }
    };
    
    private void updateTime(){
      do {
            current = mPlayer.getCurrentPosition();
            System.out.println("duration - " + duration + " current- "
                    + current);
            int dSeconds = (int) (duration / 1000) % 60 ;
            int dMinutes = (int) ((duration / (1000*60)) % 60);
            int dHours   = (int) ((duration / (1000*60*60)) % 24);
            
            int cSeconds = (int) (current / 1000) % 60 ;
            int cMinutes = (int) ((current / (1000*60)) % 60);
            int cHours   = (int) ((current / (1000*60*60)) % 24);
            
            if(dHours == 0){
             mMediaTime.setText(String.format("%02d:%02d / %02d:%02d", cMinutes, cSeconds, dMinutes, dSeconds));
            }else{
             mMediaTime.setText(String.format("%02d:%02d:%02d / %02d:%02d:%02d", cHours, cMinutes, cSeconds, dHours, dMinutes, dSeconds));
            }
            
            try{
                Log.d("Value: ", String.valueOf((int) (current * 100 / duration)));
                if(mSeekBarPlayer.getProgress() >= 100){
                    break;
                }
            }catch (Exception e) {}
        }while (mSeekBarPlayer.getProgress() <= 100);
    }
    
 @Override
 public void onPrepared(MediaPlayer arg0) {
  // TODO Auto-generated method stub
  duration = mPlayer.getDuration();
  mSeekBarPlayer.setMax(duration);
  mSeekBarPlayer.postDelayed(onEverySecond, 1000);
 }
}
you should have the directory name raw/music.mp3 under res folder. you can also download the sample project by click Here
0 comments

Xcode how to sort array Object

NSMutableArray *array = [[NSMutableArray alloc] init];
        [array addObject:[[Student alloc] initWithID:1 withName:@"Chi kheang"]];
        [array addObject:[[Student alloc] initWithID:3 withName:@"Rabee"]];
        [array addObject:[[Student alloc] initWithID:2 withName:@"Sothearoth"]];
        [array addObject:[[Student alloc] initWithID:4 withName:@"Bebe"]];
        [array addObject:[[Student alloc] initWithID:5 withName:@"Lyhour"]];
        
        //sort the object
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
        [array sortUsingDescriptors:[NSArray arrayWithObject:sort]];
        
        //print log
        for (Student *s in array) {
            NSLog(@"Student ID = %i and Name = %@\n",s.studentID,s.name);
        }


This is the out put log
0 comments

Xcode how to set tapGuesture on UIImage

in your viewDidLoad method
CGRect frame = CGRectMake(0, 50, 320, 200);
    
    //create the object for the imageview
    imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = [UIImage imageNamed:@"p5.png"];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;
    
    [self.view addSubview:imageView];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    //tap gesture
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] 
                                          initWithTarget:self 
                                          action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 2;
    [imageView addGestureRecognizer:tapGesture];
    [tapGesture release];

implement handleTapGesture method when you tap on image.
-(IBAction)handleTapGesture:(UIGestureRecognizer *)sender {
    if (sender.view.contentMode == UIViewContentModeScaleAspectFit)
        sender.view.contentMode = UIViewContentModeCenter;
    else
        sender.view.contentMode = UIViewContentModeScaleAspectFit;
}

this is your p5.png image

You can download the Example here