GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 添加自定义 CoordinatorLayout,下拉放大图片 · tmexcept/AndroidCustomView@91d5250

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .idea
    • misc.xml
  • app/src/main
    • AndroidManifest.xml
    • java/com/allen/android
      • activity
        • MainActivity.kt
        • PathActivity.kt
      • coordinatorzoom
        • CoordinatorZoomActivity.java
        • CustomCoordinatorLayout.java
    • res/layouts/custompop/layout
      • activity_custom_coordinator.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.idea/misc.xml‎

Copy file name to clipboardExpand all lines: .idea/misc.xml
+1-1Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎app/src/main/AndroidManifest.xml‎

Copy file name to clipboardExpand all lines: app/src/main/AndroidManifest.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<activity android:name="com.allen.android.gradient.GradientActivity" />
4444
<activity android:name="com.allen.android.activity.TranslateRotateCanvasActivity" />
4545
<activity android:name="com.allen.android.stretch.StretchPagerTestActivity" />
46+
<activity android:name="com.allen.android.coordinatorzoom.CoordinatorZoomActivity" />
4647
</application>
4748

4849
</manifest>
Collapse file

‎app/src/main/java/com/allen/android/activity/MainActivity.kt‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/android/activity/MainActivity.kt
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.allen.android.R
1010
import com.allen.android.activity.custompop.CustomPopActivity
1111
import com.allen.android.adapter.MainAdapter
1212
import com.allen.android.bean.TypeBean
13+
import com.allen.android.coordinatorzoom.CoordinatorZoomActivity
1314
import com.allen.android.gradient.GradientActivity
1415
import com.allen.android.helper.DragViewHelper
1516
import com.allen.android.stretch.StretchPagerTestActivity
@@ -51,6 +52,7 @@ class MainActivity : AppCompatActivity(), BaseQuickAdapter.OnItemClickListener {
5152
typeBeans.add(TypeBean("自定义Popwindow", index++))
5253
typeBeans.add(TypeBean("横滑ViewPager显示右侧加载更多", index++))
5354
typeBeans.add(TypeBean("幸运转盘、翻翻乐", index++))
55+
typeBeans.add(TypeBean("自定义 Coordinator 下拉放大", index++))
5456
return typeBeans
5557
}
5658

@@ -97,7 +99,8 @@ class MainActivity : AppCompatActivity(), BaseQuickAdapter.OnItemClickListener {
9799
18 -> startActivity(Intent(this@MainActivity, TranslateRotateCanvasActivity::class.java))
98100
19 -> startActivity(Intent(this@MainActivity, CustomPopActivity::class.java))
99101
20 -> startActivity(Intent(this@MainActivity, StretchPagerTestActivity::class.java))
100-
20 -> startActivity(Intent(this@MainActivity, LuckyWheelActivity::class.java))
102+
21 -> startActivity(Intent(this@MainActivity, LuckyWheelActivity::class.java))
103+
22 -> startActivity(Intent(this@MainActivity, CoordinatorZoomActivity::class.java))
101104
}
102105
}
103106
}
Collapse file

‎app/src/main/java/com/allen/android/activity/PathActivity.kt‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/android/activity/PathActivity.kt
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.allen.android.activity
33
import android.os.Bundle
44
import android.support.v7.app.AppCompatActivity
55
import android.util.Log
6-
import android.widget.TextView
76
import com.allen.android.R
87
import kotlinx.android.synthetic.main.activity_path.*
98
import java.util.*
Collapse file

‎app/src/main/java/com/allen/android/coordinatorzoom/CoordinatorZoomActivity.java‎

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.allen.android.coordinatorzoom;
2+
3+
import android.graphics.Color;
4+
import android.os.Build;
5+
import android.os.Bundle;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.view.View;
8+
9+
import com.allen.android.R;
10+
11+
public class CoordinatorZoomActivity extends AppCompatActivity {
12+
13+
@Override
14+
protected void onCreate(Bundle savedInstanceState) {
15+
super.onCreate(savedInstanceState);
16+
// setContentView(R.layout.activity_nestscrollview);
17+
setContentView(R.layout.activity_custom_coordinator);
18+
if (Build.VERSION.SDK_INT >= 21) {//沉浸状态栏
19+
View decorView = getWindow().getDecorView();
20+
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
21+
getWindow().setStatusBarColor(Color.TRANSPARENT);
22+
}
23+
24+
CustomCoordinatorLayout layout = findViewById(R.id.cl_layout);
25+
layout.setmZoomView(findViewById(R.id.mine_iv_avatar));
26+
layout.setmMoveView(findViewById(R.id.rl_layout), findViewById(R.id.ll_layout));
27+
}
28+
29+
}
Collapse file

‎app/src/main/java/com/allen/android/coordinatorzoom/CustomCoordinatorLayout.java‎

Copy file name to clipboard
+139Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.allen.android.coordinatorzoom;
2+
3+
4+
import android.animation.ValueAnimator;
5+
import android.content.Context;
6+
import android.support.annotation.NonNull;
7+
import android.support.annotation.Nullable;
8+
import android.support.design.widget.CollapsingToolbarLayout;
9+
import android.support.design.widget.CoordinatorLayout;
10+
import android.util.AttributeSet;
11+
import android.util.Log;
12+
import android.view.MotionEvent;
13+
import android.view.View;
14+
import android.view.ViewGroup;
15+
16+
public class CustomCoordinatorLayout extends CoordinatorLayout {
17+
private View mZoomView;
18+
private int mZoomViewWidth;
19+
private int mZoomViewHeight;
20+
21+
private float firstPosition;//记录第一次按下的位置
22+
private boolean isScrolling;//是否正在缩放
23+
private boolean isScrollDown;//是否下滑
24+
25+
private float mScrollRate = 0.6f;//缩放系数,缩放系数越大,变化的越大
26+
private float mReplyRate = 0.3f;//回调系数,越大,回调越慢
27+
private View mMoveView;
28+
private View mMoveView2;
29+
private int height,height2;
30+
31+
public CustomCoordinatorLayout(@NonNull Context context) {
32+
super(context);
33+
}
34+
35+
public CustomCoordinatorLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
36+
super(context, attrs);
37+
}
38+
39+
public CustomCoordinatorLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
40+
super(context, attrs, defStyleAttr);
41+
}
42+
43+
public void setmZoomView(View mZoomView) {
44+
this.mZoomView = mZoomView;
45+
}
46+
47+
public void setmMoveView(View mMoveView1,View mMoveView2) {
48+
this.mMoveView = mMoveView1;
49+
this.mMoveView2 = mMoveView2;
50+
height = mMoveView.getMeasuredHeight();
51+
height2 = mMoveView2.getMeasuredHeight();
52+
}
53+
54+
55+
@Override
56+
public boolean dispatchTouchEvent(MotionEvent ev) {
57+
int[] location = new int[2];
58+
mZoomView.getLocationOnScreen(location);
59+
int y = location[1];
60+
61+
62+
if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) {
63+
mZoomViewWidth = mZoomView.getMeasuredWidth();
64+
mZoomViewHeight = mZoomView.getMeasuredHeight();
65+
}
66+
67+
switch (ev.getAction()) {
68+
case MotionEvent.ACTION_UP:
69+
if(isScrollDown) break;
70+
//手指离开后恢复图片
71+
isScrolling = false;
72+
replyImage();
73+
break;
74+
case MotionEvent.ACTION_MOVE:
75+
Log.e("xijingLog", "dispatchTouchEvent axes="+getScrollY()+" getY="+ev.getY()+" y="+y);
76+
if(y != 0) return super.dispatchTouchEvent(ev);
77+
isScrollDown = false;
78+
if (!isScrolling) {
79+
if (getScrollY() == 0) {
80+
firstPosition = ev.getY();// 滚动到顶部时记录位置,否则正常返回
81+
} else {
82+
break;
83+
}
84+
}
85+
int distance = (int) ((ev.getY() - firstPosition) * mScrollRate); // 滚动距离乘以一个系数
86+
if (distance < 0) { // 当前位置比记录位置要小,正常返回
87+
isScrollDown = true;
88+
break;
89+
}
90+
// 处理放大
91+
isScrolling = true;
92+
setZoom(distance);
93+
94+
return super.dispatchTouchEvent(ev);
95+
}
96+
return super.dispatchTouchEvent(ev);
97+
98+
}
99+
100+
private void scrollDown(float zoom) {
101+
mMoveView2.setScrollY(-(int)(height2 * ((height2 + zoom) / height2)));
102+
}
103+
104+
//回弹动画
105+
private void replyImage() {
106+
float distance = mZoomView.getMeasuredWidth() - mZoomViewWidth;
107+
ValueAnimator valueAnimator = ValueAnimator.ofFloat(distance, 0f).setDuration((long) (distance * mReplyRate));
108+
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
109+
@Override
110+
public void onAnimationUpdate(ValueAnimator animation) {
111+
setZoom((Float) animation.getAnimatedValue());
112+
}
113+
114+
});
115+
valueAnimator.start();
116+
mMoveView.setScrollY(height);
117+
mMoveView2.setScrollY(height2);
118+
}
119+
120+
public void setZoom(float zoom) {
121+
if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) {
122+
return;
123+
}
124+
ViewGroup.LayoutParams lp = mZoomView.getLayoutParams();
125+
lp.width = (int) (mZoomViewWidth * ((mZoomViewWidth + zoom) / mZoomViewWidth));
126+
lp.height = (int) (mZoomViewHeight * ((mZoomViewWidth + zoom) / mZoomViewWidth));
127+
((MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, 0, 0);
128+
// mZoomView.setLayoutParams(lp);
129+
try {
130+
CollapsingToolbarLayout parent = (CollapsingToolbarLayout) (mMoveView.getParent());
131+
ViewGroup.LayoutParams layoutParams = parent.getLayoutParams();
132+
layoutParams.height = lp.height;
133+
parent.setLayoutParams(layoutParams);
134+
}catch (Exception e){
135+
136+
}
137+
138+
}
139+
}
Collapse file

‎app/src/main/res/layouts/custompop/layout/activity_custom_coordinator.xml‎

Copy file name to clipboard
+146Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<com.allen.android.coordinatorzoom.CustomCoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
xmlns:app="http://schemas.android.com/apk/res-auto"
7+
android:orientation="vertical"
8+
android:id="@+id/cl_layout">
9+
10+
<android.support.design.widget.AppBarLayout
11+
android:id="@+id/appbar_layout"
12+
android:layout_width="match_parent"
13+
android:layout_height = "wrap_content"
14+
>
15+
<android.support.design.widget.CollapsingToolbarLayout
16+
android:id="@+id/csl_layout"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:fitsSystemWindows="true"
20+
app:layout_scrollFlags="scroll|exitUntilCollapsed">
21+
<!--       //要放大的图片-->
22+
<ImageView
23+
android:id="@+id/mine_iv_avatar"
24+
android:layout_width="match_parent"
25+
android:layout_height="230dp"
26+
android:alpha="0.4"
27+
android:background="@mipmap/rong"
28+
android:scaleType="centerCrop"
29+
app:layout_collapseMode="parallax"
30+
/>
31+
32+
<!--       //平移布局一-->
33+
<RelativeLayout
34+
android:id="@+id/rl_layout"
35+
android:layout_width="match_parent"
36+
android:layout_height="230dp"
37+
android:layout_gravity="bottom"
38+
app:layout_collapseMode="parallax"
39+
app:layout_collapseParallaxMultiplier="0"
40+
>
41+
42+
<LinearLayout
43+
android:id="@+id/mine_ll_name"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:layout_marginStart="24dp"
47+
android:layout_marginTop="50dp"
48+
android:gravity="center_vertical">
49+
50+
<TextView
51+
android:id="@+id/mine_tv_name"
52+
android:layout_width="wrap_content"
53+
android:layout_height="wrap_content"
54+
android:maxLines="1"
55+
android:textColor="#ffffff"
56+
android:textSize="24dp"
57+
android:textStyle="normal"
58+
tools:text="节操君" />
59+
60+
<ImageView
61+
android:id="@+id/iv_ensure_icon"
62+
android:layout_width="20dp"
63+
android:layout_height="20dp"
64+
android:layout_marginStart="14dp"
65+
android:src="@mipmap/ic_launcher" />
66+
</LinearLayout>
67+
68+
69+
<TextView
70+
android:id="@+id/mine_tv_info"
71+
android:layout_width="wrap_content"
72+
android:layout_height="wrap_content"
73+
android:layout_below="@id/mine_ll_name"
74+
android:layout_alignStart="@id/mine_ll_name"
75+
android:layout_marginTop="6dp"
76+
android:alpha="0.6"
77+
android:background="@mipmap/ic_launcher"
78+
android:paddingLeft="6dp"
79+
android:paddingTop="2dp"
80+
android:paddingEnd="6dp"
81+
android:paddingBottom="2dp"
82+
android:text="我的主页"
83+
android:textColor="#ffffff"
84+
android:textSize="18dp" />
85+
86+
<View
87+
android:id="@+id/v_personal_info_dot"
88+
android:layout_width="8dp"
89+
android:layout_height="8dp"
90+
android:layout_alignTop="@+id/mine_tv_info"
91+
android:layout_alignEnd="@+id/mine_tv_info"
92+
android:background="@color/colorPrimary" />
93+
94+
<TextView
95+
android:id="@+id/tv_mine_user_setting"
96+
android:layout_width="wrap_content"
97+
android:layout_height="wrap_content"
98+
android:layout_alignBottom="@id/mine_tv_info"
99+
android:layout_marginStart="8dp"
100+
android:layout_toEndOf="@+id/mine_tv_info"
101+
android:alpha="0.6"
102+
android:background="@color/cardview_dark_background"
103+
android:paddingStart="6dp"
104+
android:paddingTop="2dp"
105+
android:paddingEnd="6dp"
106+
android:paddingBottom="2dp"
107+
android:text="编辑主页"
108+
android:textColor="#ffffff"
109+
android:textSize="18dp" />
110+
111+
<!--     //代码省略-->
112+
</RelativeLayout>
113+
</android.support.design.widget.CollapsingToolbarLayout>
114+
115+
</android.support.design.widget.AppBarLayout>
116+
117+
<android.support.v4.widget.NestedScrollView
118+
android:id="@+id/nestedScrollView"
119+
android:layout_width="match_parent"
120+
android:layout_height="match_parent"
121+
android:fillViewport="true"
122+
app:layout_behavior="@string/appbar_scrolling_view_behavior">
123+
124+
<LinearLayout
125+
android:id="@+id/ll_layout"
126+
android:layout_width="match_parent"
127+
android:layout_height="500dp"
128+
android:orientation="vertical"
129+
>
130+
<View
131+
android:layout_width="match_parent"
132+
android:layout_height="400dp"
133+
android:background="#ff0000"/>
134+
<View
135+
android:layout_width="match_parent"
136+
android:layout_height="400dp"
137+
android:background="#ffff00"/>
138+
<View
139+
android:layout_width="match_parent"
140+
android:layout_height="400dp"
141+
android:background="#00ff00"/>
142+
143+
</LinearLayout>
144+
</android.support.v4.widget.NestedScrollView>
145+
146+
</com.allen.android.coordinatorzoom.CustomCoordinatorLayout>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.