How to implement view pager like scrolling using swipe gesture in android

530 views Asked by At

I have to make an image by combining multiple images, Images are placed one over another at the same position

I have 9 different array of images and can change an image by selecting a particular array

All these images are transparent images that are put one over another , I have to select an image an change by scrolling

I have used swipe gesture for this it is working fine but I want view pager like scrolling in this

Here is my code that I have done.

   import java.util.Random;

import com.example.toros1.ImageSelector;
import com.example.toros1.SimpleGestureFilter;

import android.app.Fragment;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.toros1.R;
import com.example.toros1.fragment.ShakeDetector.OnShakeListener;

public class Main_Fragment extends Fragment implements OnTouchListener, OnShakeListener{
    ImageView image1 , image2, image3,image4,image5,image6,image7,image8,image9,image10;
    Boolean direction=false;
    private SimpleGestureFilter detector;
    private GestureDetector gestureDetector;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;
    public static int index=0;
    public static int count,index0,index1,index2,index3,index4,index5,index6,index7,index8,index9;
    TextView imagename;
    Button random;
    public static int resId1=R.drawable.albahio,resId2=R.drawable.bien_armado,resId3=R.drawable.axiblanco,resId4=R.drawable.careto,resId5=R.drawable.bociblanco,resId6=R.drawable.alunarado,resId7=R.drawable.cinchado,resId8=R.drawable.lloron,resId9=R.drawable.bragado;

    public  static int imagelocation;
@SuppressWarnings("deprecation")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
     View view = inflater.inflate(R.layout.fragmentlayout, null);
     image1= (ImageView) view.findViewById(R.id.imageView1);
     image2= (ImageView) view.findViewById(R.id.imageView2);
     image3= (ImageView) view.findViewById(R.id.imageView3);
     image4= (ImageView) view.findViewById(R.id.imageView4);
     image5= (ImageView) view.findViewById(R.id.imageView5);
     image6= (ImageView) view.findViewById(R.id.imageView6);
     image7= (ImageView) view.findViewById(R.id.imageView7);
     image8= (ImageView) view.findViewById(R.id.imageView8);
     image9= (ImageView) view.findViewById(R.id.imageView9);
     random=(Button) view.findViewById(R.id.random);
     imagename=(TextView) view.findViewById(R.id.text);
     imagename.setText(ImageSelector.bottomImageName[0]);


      random.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        setRandomImage();
    }
});
        detector = new SimpleGestureFilter();
        gestureDetector = new GestureDetector(detector);

         mSensorManager = (SensorManager)getActivity().getSystemService(getActivity().SENSOR_SERVICE);
            mAccelerometer = mSensorManager
                    .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            mShakeDetector = new ShakeDetector();
        mShakeDetector.setOnShakeListener(this);

        view.setOnTouchListener(this);
        return view;

}

                // TODO Auto-generated method stub


    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub

        if (gestureDetector.onTouchEvent(arg1)) {

            if (detector.isRightToLeft) {
                count = 1;
                switch (imagelocation) {

                case 0:
                    index0=index;
                    index0 = changeIndexForward(ImageSelector.bottom_image);

                    setImageName(index0, ImageSelector.bottomImageName);
                    resId1 = ImageSelector.bottom_image[index0];
                    setImage(image1, ImageSelector.bottom_image, index0, count);
                    break;
                case 1:
                    index1=index;
                    index1 = changeIndexForward(ImageSelector.horns);
                    setImageName(index1, ImageSelector.hornsImageName);
                    resId2 = ImageSelector.horns[index1];
                    setImage(image2, ImageSelector.horns, index1, count);
                    break;
                case 2:
                    index2=index;
                    index2 = changeIndexForward(ImageSelector.feet);
                    setImageName(index2, ImageSelector.feetImageName);
                    resId3 = ImageSelector.feet[index2];
                    setImage(image3, ImageSelector.feet, index2, count);
                    break;
                case 3:
                    index3=index;
                    index3 = changeIndexForward(ImageSelector.forehead);
                    setImageName(index3, ImageSelector.foreheadImageName);
                    resId4 = ImageSelector.forehead[index3];
                    setImage(image4, ImageSelector.forehead, index3, count);
                    break;
                case 4:
                    index4=index;
                    index4 = changeIndexForward(ImageSelector.mouthimage);
                    setImageName(index4, ImageSelector.mouthimageImageName);
                    resId5 = ImageSelector.mouthimage[index4];
                    setImage(image5, ImageSelector.mouthimage, index4, count);
                    break;
                case 5:
                    index5=index;
                    index5 = changeIndexForward(ImageSelector.upperimage);
                    setImageName(index5, ImageSelector.upperimageImageName);
                    resId6 = ImageSelector.upperimage[index5];
                    setImage(image6, ImageSelector.upperimage, index5, count);
                    break;
                case 6:
                    index6=index;
                    index6 = changeIndexForward(ImageSelector.lowerscreen);
                    setImageName(index6, ImageSelector.lowerscreenImageName);
                    resId7 = ImageSelector.lowerscreen[index6];
                    setImage(image7, ImageSelector.lowerscreen, index6, count);
                    break;
                case 7:
                    index7=index;
                    index7 = changeIndexForward(ImageSelector.eyes);
                    setImageName(index7, ImageSelector.eyesImageName);
                    resId8 = ImageSelector.eyes[index7];
                    setImage(image8, ImageSelector.eyes, index7, count);
                    break;
                case 8:
                    index8=index;
                    index8 = changeIndexForward(ImageSelector.top_rightimage);
                    setImageName(index8, ImageSelector.topImageName);
                    resId9 = ImageSelector.top_rightimage[index8];
                    setImage(image9, ImageSelector.top_rightimage, index8, count);
                    break;
                }
                detector.isRightToLeft = false;
            } else if (detector.isLeftToRight) {
                count = 0;
                switch (imagelocation) {
                case 0:
                    index0=index;
                    index0 = changeIndexForward(ImageSelector.bottom_image);
                    setImageName(index0, ImageSelector.bottomImageName);
                    resId1 = ImageSelector.bottom_image[index0];

                    setImage(image1, ImageSelector.bottom_image, index0, count);
                    break;

                case 1:
                    index1=index;
                    index1 = changeIndexForward(ImageSelector.horns);
                    setImageName(index1, ImageSelector.hornsImageName);
                    resId2 = ImageSelector.horns[index1];
                    setImage(image2, ImageSelector.horns, index1, count);
                    break;
                case 2:
                    index2=index;
                    index2 = changeIndexForward(ImageSelector.feet);
                    setImageName(index2, ImageSelector.feetImageName);
                    resId3 = ImageSelector.feet[index2];
                    setImage(image3, ImageSelector.feet, index2, count);
                    break;
                case 3:
                    index3=index;
                    index3 = changeIndexForward(ImageSelector.forehead);
                    setImageName(index3, ImageSelector.foreheadImageName);
                    resId4 = ImageSelector.forehead[index3];
                    setImage(image4, ImageSelector.forehead, index3, count);
                    break;
                case 4:
                    index4=index;
                    index4 = changeIndexForward(ImageSelector.mouthimage);
                    setImageName(index4, ImageSelector.mouthimageImageName);
                    resId5 = ImageSelector.mouthimage[index4];
                    setImage(image5, ImageSelector.mouthimage, index4, count);
                    break;
                case 5:
                    index5=index;
                    index5 = changeIndexForward(ImageSelector.upperimage);
                    setImageName(index5, ImageSelector.upperimageImageName);
                    resId6 = ImageSelector.upperimage[index5];
                    setImage(image6, ImageSelector.upperimage, index5, count);
                    break;
                case 6:
                    index6=index;
                    index6 = changeIndexForward(ImageSelector.lowerscreen);
                    setImageName(index6, ImageSelector.lowerscreenImageName);
                    resId7 = ImageSelector.lowerscreen[index6];
                    setImage(image7, ImageSelector.lowerscreen, index6, count);
                    break;
                case 7:
                    index7=index;
                    index7 = changeIndexForward(ImageSelector.eyes);
                    setImageName(index7, ImageSelector.eyesImageName);
                    resId8 = ImageSelector.eyes[index7];
                    setImage(image8, ImageSelector.eyes, index7, count);
                    break;
                case 8:
                    index8=index;
                    index8 = changeIndexForward(ImageSelector.top_rightimage);
                    setImageName(index8, ImageSelector.topImageName);
                    resId9 = ImageSelector.top_rightimage[index8];
                    setImage(image9, ImageSelector.top_rightimage, index8, count);
                    break;

                }

                Log.v("swipeleft", "left");
                detector.isLeftToRight = false;
            }
        }

        return true;
    }

    public void setImage(ImageView imgaeview, int a[], int index1, int flag) {

        if (flag == 1) {
            if ((index1 > a.length) || (index1 == a.length)) {

                count = flag;
            } else {
                TranslateAnimation translateAnimation1 = new TranslateAnimation(500,
                        0, 0, 0);
                translateAnimation1.setDuration(500);
                imgaeview.setAnimation(translateAnimation1);
                imgaeview.setImageResource(a[index1]);
                index = index1 + 1;

            }
            Log.v("index value after", "" + index);
        } else if (flag == 0) {
            if (index1 < 0) {

                count = flag;
            } else {
                if (index1 == a.length) {
                    index1--;
                }
                TranslateAnimation translateAnimation = new TranslateAnimation(-500,
                        0, 0, 0);
                translateAnimation.setDuration(500);
                imgaeview.setAnimation(translateAnimation);
                imgaeview.setImageResource(a[index1]);
                index = index1 - 1;

            }
        }
    }

    @SuppressWarnings("unused")
    private void setAnimation(ImageView nextImage, ImageView previousImage) {
        TranslateAnimation translateAnimation = new TranslateAnimation(-1000,
                0, 0, 0);
        translateAnimation.setDuration(2000);

        TranslateAnimation translateAnimation1 = new TranslateAnimation(1000,
                0, 0, 0);
        translateAnimation.setDuration(2000);

        nextImage.setAnimation(translateAnimation);
        previousImage.setAnimation(translateAnimation1);

    }

    public void  setRandomImage() {
        final int min = 0,mn=0;
        final int max = 10,mx=2;
         final Random myRandom = new Random();
         int num = myRandom.nextInt(max-min+1)+min;

         int i=myRandom.nextInt(mx-mn+1)+mn;
         imagename.setText(ImageSelector.bottomImageName[num]);
         resId1=ImageSelector.bottom_image[num];
         resId2=ImageSelector.horns[num];
         resId3=ImageSelector.feet[i];
         resId8=ImageSelector.eyes[i];
         resId4=ImageSelector.forehead[i];
         resId6=ImageSelector.upperimage[num];
         if(!direction){
         TranslateAnimation translateAnimation1 = new TranslateAnimation(50,
                    0, 0, 0);
            translateAnimation1.setDuration(50);
        image1.setAnimation(translateAnimation1);
        image1.setImageResource(resId1);
        image2.setAnimation(translateAnimation1);
        image2.setImageResource(resId2);
        image3.setImageResource(resId3);
        image4.setImageResource(resId4);
        image8.setImageResource(resId8);
        image4.setAnimation(translateAnimation1);
        image8.setAnimation(translateAnimation1);
        image3.setAnimation(translateAnimation1);

        image6.setImageResource(resId6);
        direction=true;
         }else{
             TranslateAnimation translateAnimation1 = new TranslateAnimation(-50,
                        0, 0, 0);
                translateAnimation1.setDuration(50);
            image1.setAnimation(translateAnimation1);
            image1.setImageResource(resId1);
            image2.setAnimation(translateAnimation1);
            image2.setImageResource(resId2);
            image3.setImageResource(resId3);
            image4.setImageResource(resId4);
            image8.setImageResource(resId8);
            image4.setAnimation(translateAnimation1);
            image8.setAnimation(translateAnimation1);
            image3.setAnimation(translateAnimation1);
            image6.setImageResource(resId6);
            direction=false;
         }

    }
    public int changeIndexForward(int a[]) {

        if (index < 0) {
            index++;
        }
        if (index == a.length) {
            index = index - 1;

        }
        return index;
    }
public void  setImageName(int indexp,String a[]) {
    imagename.setText(a[indexp]);
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

     // setting default images
        image1.setImageResource(ImageSelector.bottom_image[index0]);
        image2.setImageResource(ImageSelector.horns[index1]);
        image3.setImageResource(ImageSelector.feet[index2]);
        image4.setImageResource(ImageSelector.forehead[index3]);

    mSensorManager.registerListener(mShakeDetector, mAccelerometer,    SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onPause() {
    // Add the following line to unregister the Sensor Manager onPause
    mSensorManager.unregisterListener(mShakeDetector);
    super.onPause();
}


@Override
public void onShake(int count) {
    // TODO Auto-generated method stub
    Log.v("in sensor", "getting shake sensor");
    setRandomImage();
}



}
0

There are 0 answers