TechHui

Hawaiʻi's Technology Community

In IPhone, scrollView.bounce is used to control the nice bouncing behavior for scrolling.
Let's create the similar support in Android :)

First, create a custom view
public class BouncingHorizentalScrollView extends HorizontalScrollView {
public static final int GAP = 100;
   
public BouncingHorizentalScrollView(Context context) {
super(context);
init();
}
public BouncingHorizentalScrollView( Context context,  AttributeSet attrs){
super(context, attrs);
init();  
}
public BouncingHorizentalScrollView( Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.mygallery, this); 
}
@override
public void fling(int velocityX) { 
super.fling(velocityX); 
       int duration = calculate(velocityX, 0);
checkingFling( duration );
}
private void checkingFling(final int duration ){
           postDelayed( new Runnable() {
@Override
public void run() {    
int left = getScrollX(); 
boolean checked = false;
if ( left < GAP ){
smoothScrollBy( GAP - left, 0 ); 
checked = true;
}else{
View child =  getChildAt(0);
   int gap = left + getWidth() - ( child.getWidth() - GAP );
   if ( gap > 0 ){
    smoothScrollBy(  -gap, 0 );
    checked = true;
   }
}
if ( checked ){
return;
}
int newDuration = duration - 200;
if ( newDuration > 0 ){
checkingFling( newDuration );
}
}
},   200 ); 
}
private int calculate(int velocityX, int velocityY){
float ppi = getContext().getResources().getDisplayMetrics().density * 160.0f;
   float mDeceleration = 9.8f   // g (m/s^2)
        * 39.37f // inch/meter
        * ppi    // pixels per inch
        * ViewConfiguration.getScrollFriction();
   float velocity = (float)Math.hypot(velocityX, velocityY);
    
        return (int) (1000 * velocity / mDeceleration); // Duration is in
                                                            // milliseconds
}
    public void scrollBy(int x, final int y) {
     super.scrollBy(x, y);
        postDelayed( new Runnable() { 
@Override
public void run() {    
 
int left = getScrollX(); 
if ( left < GAP ){
scrollTo( GAP, 0 ); 
} else {
View child =  ETGallery.this.getChildAt(0);
   int gap = left + getWidth() - ( child.getWidth() - GAP );
   if ( gap > 0 ){
    //scrollTo( child.getWidth() - GAP, 0 );
    scrollBy( -gap , y);
   
   }
}
}
}, 200);
    }
    public void adjustLeft(){
     checkingFling(400);
    }
    
    public void animateToLeft(){
       postDelayed( new Runnable() { 
  
   @Override
   public void run() {  
   View child =   getChildAt(0);
   scrollTo( child.getWidth(), 0 );
  
   postDelayed( new Runnable() { 
      
       @Override
       public void run() {  
       fullScroll(View.FOCUS_LEFT);
          adjustLeft();
       }
       }, 200);
   }
   }, 200);
      
       
    }
    
    
}

in the layout file we specify the left-most and right-most padding area for bouncing.
<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/TableLayout01" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"  
            android:layout_height="wrap_content"   >
             <TextView android:id="@+id/leftPadding"  
                android:layout_width="100px"  
                android:layout_height="wrap_content" />
             <!--  more items go here.   -->
              <TextView android:id="@+id/rightPadding"  
                android:layout_width="100px"  
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout> 


That's it. 

Views: 8060

Replies to This Discussion

Thank you for sharing this code Hanning! I hope all is well with you.
Thank you, Dan.

Daniel Leuck said:
Thank you for sharing this code Hanning! I hope all is well with you.

Hai..... I try your code but I got an error when I make this...the error in this line:

 

View child =  ETGallery.this.getChildAt(0);

 

thanks for your help, I'm newbie in android programming..

btw sorry my english is not good.

HI, I  have the same problem, what is  ETGallery?

If i want use this code for vertical scroll (ScrollView), how i can modify it? Please help me!

 

Sorry for my bad english..

Windu Nurfebtiaman said:

Hai..... I try your code but I got an error when I make this...the error in this line:

 

View child =  ETGallery.this.getChildAt(0);

 

thanks for your help, I'm newbie in android programming..

btw sorry my english is not good.

RSS

Sponsors

web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service