a

Creating an Android Image Slider Control for your App (Part 2 of 3)

In Part 1 of of Creating an Android Image Slider Control, we built out the ImageSlider class, but when you added this class to a View, the horizontal scrolling didn’t behave well. In order to resolve the scrolling issue, we started playing with the Gesture options and listeners. We tried implementing the onFling event, but we did not receive consistent behavior on my older Samsung device. As a result we opted for handling the TouchEvent in our own code,

@Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent( ev );
    	final int action = MotionEventCompat.getActionMasked(ev);
        switch (action) {
	        case MotionEvent.ACTION_DOWN: {
	            final int pointerIndex = MotionEventCompat.getActionIndex(ev);
	            // Remember where we started (for dragging)
	            touchX = MotionEventCompat.getX(ev, pointerIndex);
	            touchY = MotionEventCompat.getY(ev, pointerIndex);
	            distanceX = 0;
	            distanceY = 0;
	            // Save the ID of this pointer (for dragging)
	            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
	            break;
	        }
	        case MotionEvent.ACTION_MOVE: {
	            // Find the index of the active pointer and fetch its position
	            final int pointerIndex =
	                    MotionEventCompat.findPointerIndex(ev, mActivePointerId);
	            final float x = MotionEventCompat.getX(ev, pointerIndex);
	            final float y = MotionEventCompat.getY(ev, pointerIndex);
	            // Calculate the distance moved
	            distanceX += x - touchX;
	            distanceY += y - touchY;
	            invalidate();
	            // Remember this touch position for the next move event
	            touchX = x;
	            touchY = y;
	            break;
	        }
	        case MotionEvent.ACTION_CANCEL: {
	        	final int pointerIndex = MotionEventCompat.getActionIndex(ev);
	            final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
	            if (pointerId == mActivePointerId) {
	            	mActivePointerId = INVALID_POINTER_ID;
	            	//i need to know if the movement in X was bigger than the movement in Y.
	            	if( Math.abs(distanceX) > Math.abs(distanceY) ){
		            	if(distanceX > 0){
		            		onSwipeRight();
		            	}else if(distanceX < 0){
		            		onSwipeLeft();
		            	}
	            	}
	            }
	            break;
	        }
        }
        return true;
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Do not allow touch events.
        return false;
    }
    public void onSwipeRight(){
    }
    public void onSwipeLeft(){
    }

Handling the onTouch and onInterceptTouch events cancels the scrolling behavior of the component and allows us to add a new behavior that will scroll item by item. We implement our own scrolling logic in a moveToSlide function.
pub

public void moveToSlide(int index){
	//let's ignore movement to images outside the bounds of the slider
    	if(index < 0 || index >= images.size()){
    		return;
    	}
    	this.currentSelectedIndex = index;
    	int childX = Math.round( layout.getChildAt(index).getX() );
    	this.smoothScrollTo(childX, 0);
}
public int getCurrentSlide(){
    	return this.currentSelectedIndex;
}
public void onSwipeRight(){
    	moveToSlide( getCurrentSlide() - 1);
}
public void onSwipeLeft(){
    	moveToSlide( getCurrentSlide() + 1);
}

In our next post, we’ll resolve the scenarios where the ImageViews are wider than the actual screen size, and complete the Image Carousel.

Related Articles

3 Ways Gen AI and AWS can Enhance Your Business

3 Ways Gen AI and AWS can Enhance Your Business

Amazon is on the cutting edge of new technologies. They have been increasingly experimenting with AI and learning algorithms, culminating in their most recent breakthroughs in Generative AI. Developers and technology enthusiasts have access to their innovations through the tools available on AWS.

Business Owner’s Guide to DevOps Essentials

Business Owner’s Guide to DevOps Essentials

As a business owner, it’s essential to maximize workplace efficiency. DevOps is a methodology that unites various departments to achieve business goals swiftly. Maintaining a DevOps loop is essential for the health and upkeep of deployed applications.

AWS Graviton and Arm-architecture Processors

AWS Graviton and Arm-architecture Processors

AWS launched its new batch of Arm-based processors in 2018 with AWS Graviton. It is a series of server processors designed for Amazon EC2 virtual machines. The EC2 AI instances support web servers, caching fleets, distributed data centers, and containerized microservices. Arm architecture is gradually being rolled out to handle enterprise-grade utilities at scale. Graviton instances are popular for handling intense workloads in the cloud.