網(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-07-06 python繪制子圖技巧之plt.subplot、plt.subplots及坐標(biāo)軸修改_python
- 2022-09-18 golang實(shí)現(xiàn)文件上傳并轉(zhuǎn)存數(shù)據(jù)庫(kù)功能_Golang
- 2022-05-22 Nginx基礎(chǔ)location語(yǔ)法及功能配置實(shí)例_nginx
- 2021-12-12 【Groovy】集合遍歷 ( 使用集合的 eachWithIndex 方法進(jìn)行遍歷 | 代碼示例 )
- 2022-06-25 如何利用Pandas刪除某列指定值所在的行_python
- 2022-08-02 淺談Redis常見(jiàn)延遲問(wèn)題定位與分析_Redis
- 2022-08-28 隔代獲取dom,多個(gè)commit合并成一個(gè),計(jì)算屬性完整寫(xiě)法
- 2022-06-12 GO語(yǔ)言中常見(jiàn)的排序算法使用示例_Golang
- 最近更新
-
- 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)程分支