Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開回彈效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開回彈的具體代碼,供大家參考,具體內(nèi)容如下
原圖
放大后
1、自定義view繼承ScrollView實(shí)現(xiàn)效果
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;//縮放系數(shù),縮放系數(shù)越大,變化的越大 ? ? private float mReplyRate = 0.5f;//回調(diào)系數(shù),越大,回調(diào)越慢 ? ? 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: ? ? ? ? ? ? ? ? //手指離開后恢復(fù)圖片 ? ? ? ? ? ? ? ? isScrolling = false; ? ? ? ? ? ? ? ? replyImage(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? if (!isScrolling) { ? ? ? ? ? ? ? ? ? ? if (getScrollY() == 0) { ? ? ? ? ? ? ? ? ? ? ? ? firstPosition = ev.getY();// 滾動(dòng)到頂部時(shí)記錄位置,否則正常返回 ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? int distance = (int) ((ev.getY() - firstPosition) * mScrollRate); // 滾動(dòng)距離乘以一個(gè)系數(shù) ? ? ? ? ? ? ? ? if (distance < 0) { // 當(dāng)前位置比記錄位置要小,正常返回 ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 處理放大 ? ? ? ? ? ? ? ? isScrolling = true; ? ? ? ? ? ? ? ? setZoom(distance); ? ? ? ? ? ? ? ? return true; // 返回true表示已經(jīng)完成觸摸事件,不再處理 ? ? ? ? } ? ? ? ? return true; ? ? } ? ? //回彈動(dòng)畫 ? ? 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、直接布局中使用這個(gè)view就可以,要注意的是在布局中需要在自定義view下寫一個(gè)子控件才能使用
<?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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果
- Android?ScrollView實(shí)現(xiàn)滾動(dòng)超過邊界松手回彈
- android ScrollView實(shí)現(xiàn)水平滑動(dòng)回彈
- Android實(shí)現(xiàn)橡皮筋回彈和平移縮放效果
- Android自定義View實(shí)現(xiàn)豎向滑動(dòng)回彈效果
- android實(shí)現(xiàn)可上下回彈的scrollview
- Android實(shí)現(xiàn)回彈ScrollView的原理
- Android自定義實(shí)現(xiàn)可回彈的ScollView
- Android ScrollView的頂部下拉和底部上拉回彈效果
- android自定義滾動(dòng)上下回彈scollView
相關(guān)文章
android針對(duì)json數(shù)據(jù)解析方法實(shí)例分析
這篇文章主要介紹了android針對(duì)json數(shù)據(jù)解析方法,以實(shí)例形式較為詳細(xì)的分析了Android操作json格式數(shù)據(jù)的各種常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
這篇文章主要介紹了Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Android實(shí)現(xiàn)沉浸式導(dǎo)航欄實(shí)例代碼
通過本文給大家分享android實(shí)現(xiàn)沉浸式導(dǎo)航欄實(shí)例代碼,代碼非常實(shí)用,需要的朋友可以參考下2016-05-05詳解關(guān)于AndroidQ獲取不到imsi解決方案
這篇文章主要介紹了詳解關(guān)于AndroidQ獲取不到imsi解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Flutter使用AnimatedSwitcher實(shí)現(xiàn)場景切換動(dòng)畫
在應(yīng)用中,我們經(jīng)常會(huì)遇到切換組件的場景。本文將利用Flutter中提供的AnimatedSwitcher這一動(dòng)畫組件來實(shí)現(xiàn)頁面內(nèi)的場景切換,需要的可參考一下2022-03-03詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案
這篇文章主要介紹了詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12Android Studio 多層級(jí) Module 對(duì) aar 引用問題解決方法
這篇文章主要介紹了Android Studio 多層級(jí) Module 對(duì) aar 引用問題的解決方法,需要的朋友參考下2017-12-12FFmpeg Principle學(xué)習(xí)open_output_file打開輸出文件
這篇文章主要為大家介紹了FFmpeg Principle學(xué)習(xí)open_output_file打開輸出文件示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10