網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)背景圖滑動變大松開回彈的具體代碼,供大家參考,具體內(nèi)容如下
原圖
放大后
1、自定義view繼承ScrollView實現(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();// 滾動到頂部時記錄位置,否則正常返回
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? int distance = (int) ((ev.getY() - firstPosition) * mScrollRate); // 滾動距離乘以一個系數(shù)
? ? ? ? ? ? ? ? if (distance < 0) { // 當(dāng)前位置比記錄位置要小,正常返回
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 處理放大
? ? ? ? ? ? ? ? isScrolling = true;
? ? ? ? ? ? ? ? setZoom(distance);
? ? ? ? ? ? ? ? return true; // 返回true表示已經(jīng)完成觸摸事件,不再處理
? ? ? ? }
? ? ? ? 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, -(lp.width - mZoomViewWidth) / 2, 0);
? ? ? ? mZoomView.setLayoutParams(lp);
? ? }
}
2、直接布局中使用這個view就可以,要注意的是在布局中需要在自定義view下寫一個子控件才能使用
<?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>
原文鏈接:https://blog.csdn.net/weixin_43117800/article/details/106119186
相關(guān)推薦
- 2022-04-09 cas5 編譯安裝依賴時提示: Failure to find net.shibboleth.too
- 2022-08-13 404究竟是什么意思呢?像404,200,503等數(shù)字究竟是什么東西
- 2023-01-18 Go語言讀取YAML?配置文件的兩種方式分享_Golang
- 2021-10-22 C#?基于NAudio實現(xiàn)對Wav音頻文件剪切(限PCM格式)_C#教程
- 2022-07-22 git倉庫的第一次上傳以及修改上傳項目
- 2022-10-15 QT實現(xiàn)FTP上傳文件_C 語言
- 2022-08-10 C++中string字符串分割函數(shù)split()的4種實現(xiàn)方法_C 語言
- 2022-08-14 git?stash(儲藏)的用法總結(jié)_相關(guān)技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支