Android自定義scrollView實現(xiàn)頂部圖片下拉放大
本文實例為大家分享了scrollView實現(xiàn)頂部圖片下拉放大的具體代碼,供大家參考,具體內(nèi)容如下
之前的scrollView頂部圖片下拉放大在之后的項目用到了幾次,但沒次都寫在Activity中很麻煩,也不方便復(fù)用。這幾天有空,所以重新使用自定義scrollView的方法實現(xiàn)這個效果。原理和之前的基本是一致的,所以也不多說了,直接上代碼。
package com.example.myapplication.dropzoom; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ScrollView; /** * Created by Liujinhua on 2016/3/25. * 下拉放大scrollView */ public class DropZoomScrollView extends ScrollView implements View.OnTouchListener { // 記錄首次按下位置 private float mFirstPosition = 0; // 是否正在放大 private Boolean mScaling = false; private View dropZoomView; private int dropZoomViewWidth; private int dropZoomViewHeight; public DropZoomScrollView(Context context) { super(context); } public DropZoomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public DropZoomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onFinishInflate() { super.onFinishInflate(); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } private void init() { setOverScrollMode(OVER_SCROLL_NEVER); if (getChildAt(0) != null) { ViewGroup vg = (ViewGroup) getChildAt(0); if (vg.getChildAt(0) != null) { dropZoomView = vg.getChildAt(0); setOnTouchListener(this); } } } @Override public boolean onTouch(View v, MotionEvent event) { if (dropZoomViewWidth <= 0 || dropZoomViewHeight <= 0) { dropZoomViewWidth = dropZoomView.getMeasuredWidth(); dropZoomViewHeight = dropZoomView.getMeasuredHeight(); } switch (event.getAction()) { case MotionEvent.ACTION_UP: //手指離開后恢復(fù)圖片 mScaling = false; replyImage(); break; case MotionEvent.ACTION_MOVE: if (!mScaling) { if (getScrollY() == 0) { mFirstPosition = event.getY();// 滾動到頂部時記錄位置,否則正常返回 } else { break; } } int distance = (int) ((event.getY() - mFirstPosition) * 0.6); // 滾動距離乘以一個系數(shù) if (distance < 0) { // 當(dāng)前位置比記錄位置要小,正常返回 break; } // 處理放大 mScaling = true; setZoom(1 + distance); return true; // 返回true表示已經(jīng)完成觸摸事件,不再處理 } return false; } // 回彈動畫 (使用了屬性動畫) public void replyImage() { final float distance = dropZoomView.getMeasuredWidth() - dropZoomViewWidth; // 設(shè)置動畫 ValueAnimator anim = ObjectAnimator.ofFloat(0.0F, 1.0F).setDuration((long) (distance * 0.7)); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float cVal = (Float) animation.getAnimatedValue(); setZoom(distance - ((distance) * cVal)); } }); anim.start(); } //縮放 public void setZoom(float s) { if (dropZoomViewHeight <= 0 || dropZoomViewWidth <= 0) { return; } ViewGroup.LayoutParams lp = dropZoomView.getLayoutParams(); lp.width = (int) (dropZoomViewWidth + s); lp.height = (int) (dropZoomViewHeight * ((dropZoomViewWidth + s) / dropZoomViewWidth)); dropZoomView.setLayoutParams(lp); } }
使用的時候也十分的簡單
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.myapplication.dropzoom.DropZoomScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/bg" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/home_bg" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/home_bg" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/home_bg" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/home_bg" /> </LinearLayout> </com.example.myapplication.dropzoom.DropZoomScrollView> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何玩轉(zhuǎn)Android矢量圖VectorDrawable
這篇文章主要教大家如何玩轉(zhuǎn)Android矢量圖VectorDrawable,對矢量圖感興趣的小伙伴們可以參考一下2016-05-05Android動態(tài)表格的實現(xiàn)代碼(內(nèi)容、樣式可擴縮)
這篇文章主要介紹了Android動態(tài)表格的實現(xiàn)代碼(內(nèi)容、樣式可擴縮),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Android 中clipToPadding 和 clipChildren區(qū)別和作用
這篇文章主要介紹了Android 中clipToPadding 和 clipChildren區(qū)別和作用的相關(guān)資料,需要的朋友可以參考下2017-06-06Android中URLEncoder空格被轉(zhuǎn)碼為"+"號的處理辦法
當(dāng)上傳文件的文件名中間有空格,用URLEncoder.encode方法會把空格變成加號(+)在前臺頁面顯示的時候會多出加號,下面這篇文章主要給大家介紹了關(guān)于Android中URLEncoder空格被轉(zhuǎn)碼為"+"號的處理辦法,需要的朋友可以參考下2023-01-01Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別
Android的源碼中很多地方對final關(guān)鍵字的用法很是“別出心裁”,之所以這么說是因為我從沒看過是這么使用final關(guān)鍵字的,通過本文給大家分享Android源碼中final關(guān)鍵字的用法及final,finally,finalize的區(qū)別,感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android PopupWindow使用方法小結(jié)
這篇文章主要為大家詳細介紹了Android PopupWindow使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06android實現(xiàn)圖片上傳功能(springMvc)
這篇文章主要為大家詳細介紹了android結(jié)合springMvc實現(xiàn)圖片上傳的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android自定義鍵盤的實現(xiàn)(數(shù)字鍵盤和字母鍵盤)
本篇文章主要介紹了Android自定義鍵盤的實現(xiàn)(數(shù)字鍵盤和字母鍵盤),具有一定的參考價值,有興趣的可以了解一下2017-08-08Android自定義View實現(xiàn)自動轉(zhuǎn)圈效果
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)自動轉(zhuǎn)圈效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05