안 드 로 이 드 모방 경 동 금융 홈 페이지 프로필 효과

5482 단어 Android두상
1.소개
효과 도 를 보 니 gif 가 약간 끊 겨 서 실제 기기 에서 운행 효과 가 좋 습 니 다.

2.실현
재 미 있 는 효과 입 니 다.원 리 는 간단 합 니 다.바로 ScrollView 를 통 해 Y 축의 미끄럼 거 리 를 감청 한 다음 에 코드 에서 이미지 의 위치 와 크기 를 동적 으로 설정 하 는 것 입 니 다.

public class MainActivity extends AppCompatActivity {

 private CircleImageView ivPortrait;
 private ObservableScrollView scrollView;

 private ViewGroup.MarginLayoutParams marginLayoutParams;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 initView();
 }

 private void initView() {
 ivPortrait = (CircleImageView) findViewById(R.id.iv_portrait);
 scrollView = (ObservableScrollView) findViewById(R.id.scrollView);

 marginLayoutParams = new ViewGroup.MarginLayoutParams(ivPortrait.getLayoutParams());

 scrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
 @Override
 public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
 //            
 int top = dp2px(70) - y;
 if (top < dp2px(10)) {
  //       
  marginLayoutParams.setMargins(dp2px(20), dp2px(10), 0, 0);
 } else {
  //     
  marginLayoutParams.setMargins(dp2px(20), dp2px(70) - y, 0, 0);
 }

 //                 
 FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginLayoutParams);
 //      45dp,   30dp
 int height = dp2px(45) - y < dp2px(30) ? dp2px(30) : dp2px(45) - y;
 layoutParams.height = height;
 layoutParams.width = height;
 ivPortrait.setLayoutParams(layoutParams);
 }
 });
 }

 private int dp2px(float dp) {
 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
 getResources().getDisplayMetrics());
 }
}

레이아웃 파일

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#FFF">

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="50dp"
 android:background="#F2F4F7">

 ...

 </RelativeLayout>

 <com.yl.jdfinanceindex.ObservableScrollView
 android:id="@+id/scrollView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:overScrollMode="never"
 android:scrollbars="none">

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="80dp"
  android:background="#F2F4F7">

  ...

 </RelativeLayout>

 <View
  android:layout_width="match_parent"
  android:layout_height="1000dp" />

 </LinearLayout>

 </com.yl.jdfinanceindex.ObservableScrollView>

 </LinearLayout>

 <com.yl.jdfinanceindex.CircleImageView
 android:id="@+id/iv_portrait"
 android:layout_width="45dp"
 android:layout_height="45dp"
 android:layout_marginLeft="20dp"
 android:layout_marginTop="70dp"
 android:src="@mipmap/ic_portrait" />

</FrameLayout>

원생 ScrollView 는 미끄럼 감청 이 지원 되 지 않 으 므 로 Observable ScrollView 를 사용자 정의 해 야 합 니 다.

public class ObservableScrollView extends ScrollView {

 private ScrollViewListener scrollViewListener;

 public ObservableScrollView(Context context) {
 super(context);
 }

 public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 }

 public ObservableScrollView(Context context, AttributeSet attrs) {
 super(context, attrs);
 }

 public void setScrollViewListener(ScrollViewListener scrollViewListener) {
 this.scrollViewListener = scrollViewListener;
 }

 @Override
 protected void onScrollChanged(int x, int y, int oldx, int oldy) {
 super.onScrollChanged(x, y, oldx, oldy);
 if (scrollViewListener != null) {
 scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
 }
 }

 public interface ScrollViewListener {
 void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
 }
}

3.마지막 에 쓴다
학우 들 이 평론 하 는 것 을 환영 합 니 다.만약 당신 이 이 블 로그 가 당신 에 게 유용 하 다 고 생각한다 면,메 시 지 를 남기 거나 대 들 어 보 세 요.
온전한
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기