Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slide image with finger touch in android?

I am developing an android application in which I want to slide images with finger touch. I have implemented an onClickListener with which I can slide images but I don't know how to implement finger touch functionality.

Please suggest me any method how to slide images with finger touch. Any Suggestion or any tutorial or method will be helpful.

like image 882
Avi Dhiman Avatar asked Jun 07 '11 07:06

Avi Dhiman


3 Answers

You can use onTouchListner method instead of onClickListner. Below onTouchListners example is given..

public class abc extends Activity implements OnTouchListener 
{
     ImageView img;
     protected void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);

                img = (ImageView) findViewById(R.id.imageView1);
                img.setOnTouchListener(this);
     }

        public boolean onTouch(View v, MotionEvent event) 
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {       
                  // Here u can write code which is executed after the user touch on the screen 
                     break; 
            }
            case MotionEvent.ACTION_UP:
            {             
                   // Here u can write code which is executed after the user release the touch on the screen    
                 break;
            }
            case MotionEvent.ACTION_MOVE:
            {  
               // Here u can write code which is executed when user move the finger on the screen   
                break;
            }
        }
        return true;
    }
like image 117
Mohit Kanada Avatar answered Nov 09 '22 16:11

Mohit Kanada


What you are looking for is an ViewFlipper. This will help you to get the look what you are expecting.

This mgiht be helpful

check this too

Or try this,

In your xml just add only this,

<ViewFlipper android:id="@+id/flipper" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</ViewFlipper>

need not enclose with any ImageViews.

Now do this in your coding.

Considering that you have stored your Images in a array like this,

int gallery_grid_Images[]={R.drawable.gallery_image_1,R.drawable.gallery_image_2,R.drawable.gallery_image_3,
        R.drawable.gallery_image_4,R.drawable.gallery_image_5,R.drawable.gallery_image_6,R.drawable.gallery_image_7,
        R.drawable.gallery_image_8,R.drawable.gallery_image_9,R.drawable.gallery_image_10
        };

Now in your onCreate(),

viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
 for(int i=0;i<gallery_grid_Images.length;i++)
        {
        //  This will create dynamic image view and add them to ViewFlipper
            setFlipperImage(gallery_grid_Images[i]);
        }

And now add this method to your activity,

private void setFlipperImage(int res) {
    Log.i("Set Filpper Called", res+"");
    ImageView image = new ImageView(getApplicationContext());
    image.setBackgroundResource(res);
    viewFlipper.addView(image);
}

That's it. And now using your viewFlipper.showNext(); and viewFlipper.showPrevious(); methods you can show your dynamic images.

like image 31
Andro Selva Avatar answered Nov 09 '22 16:11

Andro Selva


I would really suggest you using RealViewSwitcher class by Marc Reichelt, get it from here It provides a slight view swticher, like android launcher does.

like image 2
artouiros Avatar answered Nov 09 '22 15:11

artouiros