这篇文章主要讲解了“Android怎么实现背景图滑动变大松开回弹效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android怎么实现背景图滑动变大松开回弹效果”吧!
原图
放大后
1、自定义view继承ScrollView实现效果
public class HeadZoomScrollView extends ScrollView { private View mZoomView; private int mZoomViewWidth; private int mZoomViewHeight; private float firstPosition;//记录第一次按下的位置 private boolean isScrolling;//是否正在缩放 private float mScrollRate = 0.3f;//缩放系数,缩放系数越大,变化的越大 private float mReplyRate = 0.5f;//回调系数,越大,回调越慢 public HeadZoomScrollView(Context context) { super(context); } public HeadZoomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public HeadZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setmZoomView(View mZoomView) { this.mZoomView = mZoomView; } public void setmScrollRate(float mScrollRate) { this.mScrollRate = mScrollRate; } public void setmReplyRate(float mReplyRate) { this.mReplyRate = mReplyRate; } @Override protected void onFinishInflate() { super.onFinishInflate(); init(); } private void init() { setOverScrollMode(OVER_SCROLL_NEVER); if (getChildAt(0) != null) { ViewGroup vg = (ViewGroup) getChildAt(0); if (vg.getChildAt(0) != null) { mZoomView = vg.getChildAt(0); } } } @Override public boolean onTouchEvent(MotionEvent ev) { if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) { mZoomViewWidth = mZoomView.getMeasuredWidth(); mZoomViewHeight = mZoomView.getMeasuredHeight(); } switch (ev.getAction()) { case MotionEvent.ACTION_UP: //手指离开后恢复图片 isScrolling = false; replyImage(); break; case MotionEvent.ACTION_MOVE: if (!isScrolling) { if (getScrollY() == 0) { firstPosition = ev.getY();// 滚动到顶部时记录位置,否则正常返回 } else { break; } } int distance = (int) ((ev.getY() - firstPosition) * mScrollRate); // 滚动距离乘以一个系数 if (distance < 0) { // 当前位置比记录位置要小,正常返回 break; } // 处理放大 isScrolling = true; setZoom(distance); return true; // 返回true表示已经完成触摸事件,不再处理 } return true; } //回弹动画 private void replyImage() { float distance = mZoomView.getMeasuredWidth() - mZoomViewWidth; ValueAnimator valueAnimator = ValueAnimator.ofFloat(distance, 0f).setDuration((long) (distance * mReplyRate)); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { setZoom((Float) animation.getAnimatedValue()); } }); valueAnimator.start(); } public void setZoom(float zoom) { if (mZoomViewWidth <= 0 || mZoomViewHeight <= 0) { return; } ViewGroup.LayoutParams lp = mZoomView.getLayoutParams(); lp.width = (int) (mZoomViewWidth + zoom); lp.height = (int) (mZoomViewHeight * ((mZoomViewWidth + zoom) / mZoomViewWidth)); ((MarginLayoutParams) lp).setMargins(-(lp.width - mZoomViewWidth) / 2, 0, -(lp.width - mZoomViewWidth) / 2, 0); mZoomView.setLayoutParams(lp); } }
2、直接布局中使用这个view就可以,要注意的是在布局中需要在自定义view下写一个子控件才能使用
<?xml version="1.0" encoding="utf-8"?> <你的包名.HeadZoomScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drop_down_menu" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_show" android:layout_width="match_parent" android:layout_height="200dp" android:layout_weight="1" android:src="@mipmap/ic_launcher"/> </RelativeLayout> </com.example.application.view.HeadZoomScrollView>
感谢各位的阅读,以上就是“Android怎么实现背景图滑动变大松开回弹效果”的内容了,经过本文的学习后,相信大家对Android怎么实现背景图滑动变大松开回弹效果这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。