網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了android自定義滾動(dòng)上下回彈scollView的具體代碼,供大家參考,具體內(nèi)容如下
這是一個(gè)自定義view,在xml布局中用這個(gè)view嵌套要使之可以上下回彈的view
就能實(shí)現(xiàn)布局可以滾動(dòng)上下回彈了,自定義view代碼如下:
package com.loopfire.meitaotao.view.scrollView;
?
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;
?
/**
?* 上下回彈 scollView
?*?
?* @author Administrator
?*?
?*/
public class MyScrollView extends ScrollView {
?? ?private View inner;
?? ?private float y;
?? ?private Rect normal = new Rect();
?? ?private boolean animationFinish = true;
?
?? ?public MyScrollView(Context context) {
?? ??? ?super(context);
?? ?}
?
?? ?public MyScrollView(Context context, AttributeSet attrs) {
?? ??? ?super(context, attrs);
?? ?}
?
?? ?@Override
?? ?protected void onFinishInflate() {
?? ??? ?if (getChildCount() > 0) {
?? ??? ??? ?inner = getChildAt(0);
?? ??? ?}
?? ?}
?
?? ?@Override
?? ?public boolean onInterceptTouchEvent(MotionEvent ev) {
?? ??? ?return super.onInterceptTouchEvent(ev);
?? ?}
?
?? ?@Override
?? ?public boolean onTouchEvent(MotionEvent ev) {
?? ??? ?if (inner == null) {
?? ??? ??? ?return super.onTouchEvent(ev);
?? ??? ?} else {
?? ??? ??? ?commOnTouchEvent(ev);
?? ??? ?}
?? ??? ?return super.onTouchEvent(ev);
?? ?}
?
?? ?private void commOnTouchEvent(MotionEvent ev) {
?? ??? ?if (animationFinish) {
?? ??? ??? ?int action = ev.getAction();
?? ??? ??? ?switch (action) {
?? ??? ??? ?case MotionEvent.ACTION_DOWN:
?? ??? ??? ??? ?// System.out.println("ACTION_DOWN");
?? ??? ??? ??? ?y = ev.getY();
?? ??? ??? ??? ?super.onTouchEvent(ev);
?? ??? ??? ??? ?break;
?? ??? ??? ?case MotionEvent.ACTION_UP:
?? ??? ??? ??? ?// System.out.println("ACTION_UP");
?? ??? ??? ??? ?y = 0;
?? ??? ??? ??? ?if (isNeedAnimation()) {
?? ??? ??? ??? ??? ?animation();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?super.onTouchEvent(ev);
?? ??? ??? ??? ?break;
?? ??? ??? ?case MotionEvent.ACTION_MOVE:
?? ??? ??? ??? ?// System.out.println("ACTION_MOVE");
?? ??? ??? ??? ?final float preY = y == 0 ? ev.getY() : y;
?? ??? ??? ??? ?float nowY = ev.getY();
?? ??? ??? ??? ?int deltaY = (int) (preY - nowY);
?? ??? ??? ??? ?// 滾動(dòng)
?? ??? ??? ??? ?// scrollBy(0, deltaY);
?
?? ??? ??? ??? ?y = nowY;
?? ??? ??? ??? ?// 當(dāng)滾動(dòng)到最上或者最下時(shí)就不會(huì)再滾動(dòng),這時(shí)移動(dòng)布局
?? ??? ??? ??? ?if (isNeedMove()) {
?? ??? ??? ??? ??? ?if (normal.isEmpty()) {
?? ??? ??? ??? ??? ??? ?// 保存正常的布局位置
?? ??? ??? ??? ??? ??? ?normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?// 移動(dòng)布局
?? ??? ??? ??? ??? ?inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, inner.getRight(), inner.getBottom() - deltaY / 2);
?? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ?super.onTouchEvent(ev);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?default:
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?
?? ?// 開(kāi)啟動(dòng)畫(huà)移動(dòng)
?
?? ?private void animation() {
?? ??? ?// 開(kāi)啟移動(dòng)動(dòng)畫(huà)
?? ??? ?TranslateAnimation ta = new TranslateAnimation(0, 0, 0, normal.top - inner.getTop());
?? ??? ?ta.setDuration(200);
?? ??? ?ta.setAnimationListener(new AnimationListener() {
?? ??? ??? ?@Override
?? ??? ??? ?public void onAnimationStart(Animation animation) {
?? ??? ??? ??? ?animationFinish = false;
?
?? ??? ??? ?}
?
?? ??? ??? ?@Override
?? ??? ??? ?public void onAnimationRepeat(Animation animation) {
?
?? ??? ??? ?}
?
?? ??? ??? ?@Override
?? ??? ??? ?public void onAnimationEnd(Animation animation) {
?? ??? ??? ??? ?inner.clearAnimation();
?? ??? ??? ??? ?// 設(shè)置回到正常的布局位置
?? ??? ??? ??? ?inner.layout(normal.left, normal.top, normal.right, normal.bottom);
?? ??? ??? ??? ?normal.setEmpty();
?? ??? ??? ??? ?animationFinish = true;
?? ??? ??? ?}
?? ??? ?});
?? ??? ?inner.startAnimation(ta);
?? ?}
?
?? ?// 是否需要開(kāi)啟動(dòng)畫(huà)
?? ?private boolean isNeedAnimation() {
?? ??? ?return !normal.isEmpty();
?? ?}
?
?? ?// 是否需要移動(dòng)布局
?? ?private boolean isNeedMove() {
?? ??? ?int offset = inner.getMeasuredHeight() - getHeight();
?? ??? ?int scrollY = getScrollY();
?? ??? ?if (scrollY == 0 || scrollY == offset) {
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?
}
在xml中使用如下:
<com.loopfire.meitaotao.view.scrollView.MyScrollView> ? ? ? ? ? <TextView ? ? ? ? ? ? ? style="@style/form_left_text_style" ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:layout_marginLeft="@dimen/text_margin_left2" ? ? ? ? ? ? ? android:text="@string/about" /> </com.loopfire.meitaotao.view.scrollView.MyScrollView>
那么包含的這個(gè)textview可以上下滾動(dòng)并且回彈了
原文鏈接:https://blog.csdn.net/u014763302/article/details/45957657
相關(guān)推薦
- 2022-05-04 詳解Tomcat中查看JVM內(nèi)存使用情況_Tomcat
- 2022-11-03 關(guān)于golang?test緩存問(wèn)題_Golang
- 2022-04-16 Python繪圖示例程序中的幾個(gè)語(yǔ)法糖果你知道嗎_python
- 2022-05-11 Python實(shí)現(xiàn)圖書(shū)管理系統(tǒng)設(shè)計(jì)_python
- 2022-04-22 Number精度超了如何解決
- 2022-04-29 Go語(yǔ)言中的并發(fā)goroutine底層原理_Golang
- 2022-09-16 python解析照片拍攝時(shí)間進(jìn)行圖片整理_python
- 2022-10-23 C#優(yōu)雅的實(shí)現(xiàn)INotifyPropertyChanged接口_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支