Android實(shí)現(xiàn)下拉放大圖片松手自動(dòng)反彈效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)下拉放大圖片松手自動(dòng)反彈的具體代碼,供大家參考,具體內(nèi)容如下
直接看效果:

下面就是代碼
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;//縮放系數(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:
//手指離開(kāi)后恢復(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)畫(huà)
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里面沒(méi)有寫(xiě)代碼就不粘了
下面是布局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="數(shù)據(jù)1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="數(shù)據(jù)2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="數(shù)據(jù)3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="數(shù)據(jù)4"/>
</LinearLayout>
</bwie.com.pulllistview.HeadZoomScrollView>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android app判斷是否有系統(tǒng)簽名步驟詳解
這篇文章主要為大家介紹了android app判斷是否有系統(tǒng)簽名步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Android?Compose之Animatable動(dòng)畫(huà)停止使用詳解
這篇文章主要為大家介紹了Android?Compose之Animatable動(dòng)畫(huà)停止使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android中ProgressBar用法簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中ProgressBar用法,以簡(jiǎn)單實(shí)例形式分析了Android中ProgressBar進(jìn)度條控件的功能與布局相關(guān)技巧,需要的朋友可以參考下2016-01-01
ActivityLifecycleCallbacks如何判斷APP是否在前臺(tái)
這篇文章主要為大家詳細(xì)介紹了ActivityLifecycleCallbacks判斷APP是否在前臺(tái)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
android實(shí)現(xiàn)圖片驗(yàn)證碼方法解析(自繪控件)
本文主要介紹了android自繪控件的應(yīng)用--實(shí)現(xiàn)圖片驗(yàn)證碼方法案例,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
Android實(shí)現(xiàn)循環(huán)平移動(dòng)畫(huà)示例
這篇文章主要介紹了Android實(shí)現(xiàn)循環(huán)平移動(dòng)畫(huà)示例,本文講解實(shí)現(xiàn)用一張背景圖做循環(huán)從左往右平移動(dòng)畫(huà),需要的朋友可以參考下2015-06-06

