Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CoordinatorLayout+AppBarLayout to realize the function of stretching the top picture in Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use CoordinatorLayout+AppBarLayout in Android to achieve the function of stretching the top picture, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Go directly to the layout file code

Java code

Package com.ce.myscrollimg;import androidx.appcompat.app.AppCompatActivity;import androidx.fragment.app.Fragment;import androidx.viewpager.widget.ViewPager;import android.graphics.Typeface;import android.os.Bundle;import android.util.TypedValue;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TextView;import com.google.android.material.tabs.TabLayout;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {private TabLayout toolbar_tab Private NoScrollViewPager vp_content; private ViewPagerAdapter vpAdapter; private List listFragment = new ArrayList (); @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); initView ();} / / initialize view private void initView () {/ / tab toolbar_tab = findViewById (R.id.toolbar_tab); / / vp_content = findViewById (R.id.vp_content); vpAdapter = new ViewPagerAdapter (getSupportFragmentManager (), listFragment); vp_content.setAdapter (vpAdapter) Vp_content.setOffscreenPageLimit (2); toolbar_tab.setupWithViewPager (vp_content); for (int iTunes 0 & & child.getBottom () > mParentHeight)) {scale (child, target, dy); return;}} super.onNestedPreScroll (coordinatorLayout, child, target, dx, dy, consumed,x) } @ Override public boolean onNestedPreFling (CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY) {if (velocityY > 100) {/ / when y speed > 100, isAnimate = false;} return super.onNestedPreFling (coordinatorLayout, child, target, velocityX, velocityY);} @ Override public void onStopNestedScroll (CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target, int x) {recovery (abl); super.onStopNestedScroll (coordinatorLayout, abl, target,x) } private void initial (AppBarLayout abl) {abl.setClipChildren (false); mParentHeight = abl.getHeight (); mTargetViewHeight = mTargetView.getHeight (); mMiddleHeight = middleLayout.getHeight ();} private void scale (AppBarLayout abl, View target, int dy) {mTotalDy + =-dy; mTotalDy = Math.min (mTotalDy, TARGET_HEIGHT); mLastScale = Math.max (1f, 1f + mTotalDy / TARGET_HEIGHT); ViewCompat.setScaleX (mTargetView, mLastScale); ViewCompat.setScaleY (mTargetView, mLastScale) MLastBottom = mParentHeight + (int) (mTargetViewHeight / 2 * (mLastScale-1)); abl.setBottom (mLastBottom); target.setScrollY (0); middleLayout.setTop (mLastBottom-mMiddleHeight); middleLayout.setBottom (mLastBottom); if (onProgressChangeListener! = null) {float progress = Math.min ((mLastScale-1) / MAX_REFRESH_LIMIT, 1); / / calculate the progress of onProgressChangeListener.onProgressChange (progress, false) }} public interface onProgressChangeListener {/ * * @ param progress * @ param isRelease is released * / void onProgressChange (float progress, boolean isRelease);} public void setOnProgressChangeListener (AppBarLayoutOverScrollViewBehavior.onProgressChangeListener onProgressChangeListener) {this.onProgressChangeListener = onProgressChangeListener;} onProgressChangeListener onProgressChangeListener; private void recovery (final AppBarLayout abl) {if (isRecovering) return; if (mTotalDy > 0) {isRecovering = true; mTotalDy = 0 If (isAnimate) {ValueAnimator anim = ValueAnimator.ofFloat (mLastScale, 1f) .setDuration (200); anim.addUpdateListener (new ValueAnimator.AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {float value = (float) animation.getAnimatedValue (); ViewCompat.setScaleX (mTargetView, value); ViewCompat.setScaleY (mTargetView, value) Abl.setBottom ((int) (mLastBottom-(mLastBottom-mParentHeight) * animation.getAnimatedFraction ()); middleLayout.setTop ((int) (mLastBottom-(mLastBottom-mParentHeight) * animation.getAnimatedFraction ()-mMiddleHeight)); if (onProgressChangeListener! = null) {float progress = Math.min ((value-1) / MAX_REFRESH_LIMIT, 1); / / calculate the progress of onProgressChangeListener.onProgressChange (progress, true) }); anim.addListener (new Animator.AnimatorListener () {@ Override public void onAnimationStart (Animator animation) {} @ Override public void onAnimationEnd (Animator animation) {isRecovering = false;} @ Override public void onAnimationCancel (Animator animation) {} @ Override public void onAnimationRepeat (Animator animation) {}}); anim.start () } else {ViewCompat.setScaleX (mTargetView, 1f); ViewCompat.setScaleY (mTargetView, 1f); abl.setBottom (mParentHeight); middleLayout.setTop (mParentHeight-mMiddleHeight); / / middleLayout.setBottom (mParentHeight); isRecovering = false; if (onProgressChangeListener! = null) onProgressChangeListener.onProgressChange (0, true) Thank you for reading this article carefully. I hope the article "how to use CoordinatorLayout+AppBarLayout to stretch the top picture in Android" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report