안 드 로 이 드 는 위 챗 모멘트 를 모방 하여 스크롤 바 드 롭 다운 바 운 드 효 과 를 실현 합 니 다.

위 챗 모멘트 위 에 있 는 그림 표지,QQ 공간 에서 위 에 있 는 그림 표지 가 모두 아래로 당 겨 서 튕 기 는 효과 가 있 는데 이것 은 모두 스크롤 바 를 사용 하여 이 루어 진 것 입 니 다.내리 고 풀 었 을 때 원래 위치 로 되 돌아 갑 니 다.내 려 갈 때 배경 그림 을 볼 수 있 습 니 다.그럼 이 효과 의 실현 을 간단히 소개 하 겠 습 니 다.
1.효과 도

이 휴대 전 화 는 해상도 가 제한 되 어 있어 오래된 휴대 전화 디 버 깅 이다.
2、바 운 드 효과 가 있 음 BounceScrollView

package com.org.scroll; 
 
import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.animation.TranslateAnimation; 
import android.widget.ScrollView; 
 
/** 
 * ScrollView        
 */ 
public class BounceScrollView extends ScrollView { 
 private View inner;//   View 
 
 private float y;//    y   
 //   (       ,            .) 
 private Rect normal = new Rect(); 
 
 private boolean isCount = false;//        
 
 public BounceScrollView(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 } 
 
 /*** 
 *    XML         .             ,           .         onFinishInflate 
 *   ,          ,        . 
 */ 
 @Override 
 protected void onFinishInflate() { 
 if (getChildCount() > 0) { 
  inner = getChildAt(0); 
 } 
 } 
 
 /*** 
 *   touch 
 */ 
 @Override 
 public boolean onTouchEvent(MotionEvent ev) { 
 if (inner != null) { 
  commOnTouchEvent(ev); 
 } 
 
 return super.onTouchEvent(ev); 
 } 
 
 /*** 
 *      
 * 
 * @param ev 
 */ 
 public void commOnTouchEvent(MotionEvent ev) { 
 int action = ev.getAction(); 
 switch (action) { 
 case MotionEvent.ACTION_DOWN: 
  break; 
 case MotionEvent.ACTION_UP: 
  //     . 
  if (isNeedAnimation()) { 
  animation(); 
  isCount = false; 
  } 
  break; 
 /*** 
  *           ,         y  ,  MotionEvent.ACTION_DOWN     , 
  *      MyScrollView touch       LIstView   item  .          . 
  *            ,                0.             . 
  */ 
 case MotionEvent.ACTION_MOVE: 
  final float preY = y;//     y   
  float nowY = ev.getY();//   y   
  int deltaY = (int) (preY - nowY);//      
  if (!isCount) { 
  deltaY = 0; //      0. 
  } 
 
  y = nowY; 
  //                  ,       
  if (isNeedMove()) { 
  //         
  if (normal.isEmpty()) { 
   //           
   normal.set(inner.getLeft(), inner.getTop(), 
    inner.getRight(), inner.getBottom()); 
  } 
//  Log.e("jj", "  :" + inner.getLeft() + "," + inner.getTop() 
//   + "," + inner.getRight() + "," + inner.getBottom()); 
  //      
  inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, 
   inner.getRight(), inner.getBottom() - deltaY / 2); 
  } 
  isCount = true; 
  break; 
 
 default: 
  break; 
 } 
 } 
 
 /*** 
 *      
 */ 
 public void animation() { 
 //        
 TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), 
  normal.top); 
 ta.setDuration(200); 
 inner.startAnimation(ta); 
 //             
 inner.layout(normal.left, normal.top, normal.right, normal.bottom); 
 
// Log.e("jj", "  :" + normal.left + "," + normal.top + "," + normal.right 
//  + "," + normal.bottom); 
 
 normal.setEmpty(); 
 
 } 
 
 //          
 public boolean isNeedAnimation() { 
 return !normal.isEmpty(); 
 } 
 
 /*** 
 *          inner.getMeasuredHeight():           
 * 
 * getHeight():          
 * 
 * @return 
 */ 
 public boolean isNeedMove() { 
 int offset = inner.getMeasuredHeight() - getHeight(); 
 int scrollY = getScrollY(); 
// Log.e("jj", "scrolly=" + scrollY); 
 // 0   ,        
 if (scrollY == 0 || scrollY == offset) { 
  return true; 
 } 
 return false; 
 } 
 
} 
3、MainActivity

package com.org.activity; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.Window; 
 
public class MainActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 requestWindowFeature(Window.FEATURE_NO_TITLE); 
 setContentView(R.layout.activity_main); 
 } 
 
 @Override 
 public boolean onCreateOptionsMenu(Menu menu) { 
 // Inflate the menu; this adds items to the action bar if it is present. 
 getMenuInflater().inflate(R.menu.activity_main, menu); 
 return true; 
 } 
 
} 
이것 은 아무것도 하지 않 았 습 니 다.주로 레이아웃 과 BounceScrollView 류 를 봅 니 다.
4、activity_기본 레이아웃

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" > 
 
 <include layout="@layout/common_title_bg" /> 
 
 <com.org.scroll.BounceScrollView 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:background="@drawable/coversation_bg" 
 android:focusable="true" 
 android:focusableInTouchMode="true" > 
 
 <LinearLayout 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  android:paddingTop="10.0dip" > 
 
  <RelativeLayout 
  android:id="@+id/accountSetting" 
  android:layout_width="fill_parent" 
  android:layout_height="63.0dip" 
  android:background="#80ffffff" 
  android:focusable="true" > 
 
  <FrameLayout 
   android:id="@+id/frameLayout1" 
   android:layout_width="54.0dip" 
   android:layout_height="54.0dip" 
   android:layout_centerVertical="true" 
   android:layout_marginLeft="10.0dip" > 
 
   <ImageView 
   android:id="@+id/face" 
   android:layout_width="50.0dip" 
   android:layout_height="50.0dip" 
   android:layout_gravity="center" 
   android:contentDescription="@null" 
   android:src="@drawable/h0" /> 
 
   <ImageView 
   android:id="@+id/statusIcon" 
   android:layout_width="18.0dip" 
   android:layout_height="18.0dip" 
   android:layout_gravity="bottom|right|center" 
   android:contentDescription="@null" /> 
  </FrameLayout> 
 
  <ImageView 
   android:id="@+id/imageView1" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_alignParentRight="true" 
   android:layout_centerVertical="true" 
   android:layout_marginRight="10.0dip" 
   android:contentDescription="@null" 
   android:duplicateParentState="true" /> 
 
  <TextView 
   android:id="@+id/status" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_alignBottom="@+id/nick" 
   android:layout_marginRight="10.0dip" 
   android:layout_toLeftOf="@id/imageView1" 
   android:duplicateParentState="true" 
   android:text="  " /> 
 
  <TextView 
   android:id="@+id/nick" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_centerVertical="true" 
   android:layout_marginLeft="10.0dip" 
   android:layout_marginRight="69.0dip" 
   android:layout_toRightOf="@id/frameLayout1" 
   android:duplicateParentState="true" 
   android:ellipsize="end" 
   android:singleLine="true" /> 
  </RelativeLayout> 
 
  <LinearLayout 
  android:layout_width="match_parent" 
  android:layout_height="600dp" 
  android:layout_marginTop="16.0dip" 
  android:layout_weight="2.13" 
  android:background="#ffffffff" 
  android:orientation="vertical" > 
 
  <TextView 
   android:id="@+id/my_profile" 
   android:layout_width="fill_parent" 
   android:layout_height="44.0dip" 
   android:background="#800000ff" 
   android:clickable="true" 
   android:gravity="center_vertical" 
   android:paddingLeft="10.0dip" 
   android:paddingRight="10.0dip" 
   android:text="   " /> 
 
  <LinearLayout 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:layout_marginTop="16.0dip" 
   android:orientation="vertical" > 
 
   <RelativeLayout 
   android:id="@+id/set_feedback" 
   android:layout_width="fill_parent" 
   android:layout_height="44.0dip" 
   android:background="#8000ffff" 
   android:clickable="true" 
   android:focusable="true" > 
 
   <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="12.0dip" 
    android:duplicateParentState="true" 
    android:gravity="center_vertical" 
    android:text="  " /> 
   </RelativeLayout> 
  </LinearLayout> 
  </LinearLayout> 
 </LinearLayout> 
 </com.org.scroll.BounceScrollView> 
 
</LinearLayout> 
원본 다운로드:Android 스크롤 바 드 롭 다운 바 운 드 효과 구현
본 고 는 여러분 들 이 안 드 로 이 드 소프트웨어 프로 그래 밍 을 배 우 는 데 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기