国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義scrollView實現頂部圖片下拉放大

2019-10-22 18:19:06
字體:
來源:轉載
供稿:網友

本文實例為大家分享了scrollView實現頂部圖片下拉放大的具體代碼,供大家參考,具體內容如下

之前的scrollView頂部圖片下拉放大在之后的項目用到了幾次,但沒次都寫在Activity中很麻煩,也不方便復用。這幾天有空,所以重新使用自定義scrollView的方法實現這個效果。原理和之前的基本是一致的,所以也不多說了,直接上代碼。

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:         //手指離開后恢復圖片         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); // 滾動距離乘以一個系數         if (distance < 0) { // 當前位置比記錄位置要小,正常返回           break;         }          // 處理放大         mScaling = true;         setZoom(1 + distance);         return true; // 返回true表示已經完成觸摸事件,不再處理     }     return false;   }    // 回彈動畫 (使用了屬性動畫)   public void replyImage() {     final float distance = dropZoomView.getMeasuredWidth() - dropZoomViewWidth;      // 設置動畫     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> 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聊城市| 西充县| 滕州市| 涟水县| 清河县| 定兴县| 舒兰市| 建瓯市| 前郭尔| 南宁市| 洪洞县| 合水县| 城步| 英吉沙县| 邢台市| 含山县| 当雄县| 罗甸县| 河曲县| 灌阳县| 错那县| 大厂| 公安县| 罗山县| 景德镇市| 大丰市| 灵川县| 沁阳市| 阿鲁科尔沁旗| 五原县| 淮阳县| 庄浪县| 维西| 方城县| 五峰| 邵武市| 渭南市| 南城县| 长汀县| 德安县| 克山县|