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

首頁 > 系統 > Android > 正文

Android實現下拉放大圖片松手自動反彈效果

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

本文實例為大家分享了Android實現下拉放大圖片松手自動反彈的具體代碼,供大家參考,具體內容如下

直接看效果:

Android,下拉,放大圖片,自動反彈

下面就是代碼

HeadZoomScrollView類

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 BAIPEI on 2017/11/21.  */  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, 0, 0);     mZoomView.setLayoutParams(lp);   }  } 

MainActivity里面沒有寫代碼就不粘了

下面是布局activity_main

<bwie.com.pulllistview.HeadZoomScrollView   xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/scrollView"   android:layout_width="match_parent"   android:layout_height="match_parent">    <LinearLayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">      <ImageView       android:id="@+id/iv_show"       android:layout_width="match_parent"       android:layout_height="200dp"       android:layout_weight="1"       android:src="@drawable/a1"/>     <TextView       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="數據1"/>     <TextView       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="數據2"/>     <TextView       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="數據3"/>     <TextView       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:text="數據4"/>    </LinearLayout>  </bwie.com.pulllistview.HeadZoomScrollView> 

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 察雅县| 筠连县| 岫岩| 明光市| 合作市| 浠水县| 偃师市| 株洲县| 勃利县| 青阳县| 平南县| 越西县| 烟台市| 东阳市| 滨州市| 民丰县| 内丘县| 永平县| 丰城市| 白玉县| 安国市| 曲阳县| 宜黄县| 桓台县| 缙云县| 乌兰察布市| 根河市| 亳州市| 西丰县| 平武县| 石楼县| 清水河县| 南漳县| 图们市| 启东市| 定安县| 敦煌市| 清涧县| 阿克陶县| 天镇县| 奉节县|