Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Imageview on touch event

Tags:

android

I am creating a android application. Here I have a imageview . my goal is to get next picture from database when move the finger on image

float nPicturePositionM = 0;

public boolean onTouch(View v, MotionEvent event) {
    boolean aHandledL = false;
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        float nNewPicturePosL = event.getX();
        if (nPicturePositionM < nNewPicturePosL) {
            nPicturePositionM = nNewPicturePosL;
            GetPictures(true);
        } else {
            nPicturePositionM = nNewPicturePosL;
            GetPictures(false);
        }
        aHandledL = true;
    }

    return aHandledL;

    //return false;
}

How can i handle it using touch event? Image should slide as in we do it in gallery

like image 382
user1618951 Avatar asked Aug 23 '12 07:08

user1618951


1 Answers

Firstly you declare an imageview in your xml :

<ImageView
    android:id="@+id/imageview1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="45dp"
    android:layout_centerHorizontal="true" />

Then in your activity you initialize the component :

private ImageView image1;

In onCreate :

this.image1= (ImageView)this.findViewById(R.id.imageview1);
this.image1.setImageResource(R.drawable.NAMEOFIMAGE);

The next step is detecting if the image is "clicked". You can then use an online database or an SQLite database to save the images or the path to the images, the database part of it is a large part to teach from scratch depending which route you want to go with:

       this.image1.setOnClickListener(new View.OnClickListener() {        
        @Override
           public void onClick(View view) {
            if (view == findViewById(R.id.imageview1)) {     
                         //PUT IN CODE HERE TO GET NEXT IMAGE
            }
            }
        });

I hope this helps. Let me know how it goes :)

like image 174
Ushal Naidoo Avatar answered Oct 20 '22 10:10

Ushal Naidoo