欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

android个人中心下拉弹回效果-PullscrollView,移动端开发

时间:2023-07-31

mState = State.UP;

} else if (deltaY > 0 && mState == State.NORMAL) {

mState = State.DOWN;

}

if (mState == State.UP) {

deltaY = deltaY < 0 ? deltaY : 0;

isMoving = false;

mEnableTouch = false;

} else if (mState == State.DOWN) {

if (getScrollY() <= deltaY) {

mEnableTouch = true;

isMoving = true;

}

deltaY = deltaY < 0 ? 0 : deltaY;

}

if (isMoving) {

// 初始化content view矩形

if (mContentRect.isEmpty()) {

// 保存正常的布局位置

mContentRect.set(mContentView.getLeft(), mContentView.getTop(), mContentView.getRight(),

mContentView.getBottom());

}

// 计算header移动距离(手势移动的距离阻尼系数0.5)

float headerMoveHeight = deltaY * 0.5f * SCROLL_RATIO;

mCurrentTop = (int) (mInitTop + headerMoveHeight);

mCurrentBottom = (int) (mInitBottom + headerMoveHeight);

// 计算content移动距离(手势移动的距离*阻尼系数)

float contentMoveHeight = deltaY * SCROLL_RATIO;

// 修正content移动的距离,避免超过header的底边缘

int headerBottom = mCurrentBottom - mHeaderVisibleHeight;

int top = (int) (mContentRect.top + contentMoveHeight);

int bottom = (int) (mContentRect.bottom + contentMoveHeight);

if (top <= headerBottom) {

// 移动content view

mContentView.layout(mContentRect.left, top, mContentRect.right, bottom);

// 移动header view

mHeader.layout(mHeader.getLeft(), mCurrentTop, mHeader.getRight(), mCurrentBottom);

}

}

}

private void rollBackAnimation() {

TranslateAnimation tranAnim = new TranslateAnimation(0, 0,

Math.abs(mInitTop - mCurrentTop), 0);

tranAnim.setDuration(200);

mHeader.startAnimation(tranAnim);

mHeader.layout(mHeader.getLeft(), mInitTop, mHeader.getRight(), mInitBottom);

// 开启移动动画

TranslateAnimation innerAnim = new TranslateAnimation(0, 0, mContentView.getTop(), mContentRect.top);

innerAnim.setDuration(200);

mContentView.startAnimation(innerAnim);

mContentView.layout(mContentRect.left, mContentRect.top, mContentRect.right, mContentRect.bottom);

mContentRect.setEmpty();

// 回调监听器

if (mCurrentTop > mInitTop + TURN_DISTANCE && monTurnListener != null){

mOnTurnListener.onTurn();

}

}

private boolean isNeedAnimation() {

return !mContentRect.isEmpty() && isMoving;

}

public interface onTurnListener {

public void onTurn();

}

}

调用Activity

package com.baobao.testpullscrollview;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ImageView;

public class MainActivity extends Activity implements PullScrollView.OnTurnListener{

private PullScrollView mScrollView;

private ImageView mHeadImg;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mScrollView = (PullScrollView) findViewById(R.id.scroll_view);

mHeadImg = (ImageView) findViewById(R.id.background_img);

mScrollView.setHeader(mHeadImg);

mScrollView.setonTurnListener(this);

}

@Override

public void onTurn() {

// TODO Auto-generated method stub

}

}

xml文件:

<?xml version="1.0" encoding="utf-8"?>

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background="@color/white"

android:orientation=“vertical” >

android:id="@+id/background_img"

android:layout_width=“match_parent”

android:layout_height=“300dp”

android:layout_marginTop="-100dp"

android:contentDescription="@null"

android:scaleType=“fitXY”

android:src="@drawable/scrollview_header" />

android:id="@+id/scroll_view"

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:fillViewport=“true”

app:headerHeight=“300dp”

app:headerVisibleHeight=“100dp” >

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background="@color/transparent"

android:orientation=“vertical” >

android:id="@+id/scroll_view_head"

android:layout_width=“match_parent”

android:layout_height=“112dp”

android:layout_marginTop=“100dp”

android:background="@color/transparent"

android:orientation=“vertical” >

android:id="@+id/user_avatar"

android:layout_width=“68dp”

android:layout_height=“68dp”

android:layout_marginLeft=“21dp”

android:background="@android:color/white"

android:contentDescription="@null"

android:padding=“1px”

android:src="@drawable/avatar_default" />

android:id="@+id/user_name"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginLeft=“13dp”

android:layout_toRightOf="@id/user_avatar"

android:ellipsize=“end”

android:shadowColor="@android:color/black"

android:shadowDx=“3.0”

android:shadowDy=“3.0”

android:shadowRadius=“5.0”

android:singleLine=“true”

android:text="@string/user_name"

android:textColor="@android:color/white"

android:textSize=“20sp” />

android:id="@+id/user_divider_layout"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_below="@id/user_avatar"

android:layout_marginLeft=“21dp”

android:layout_marginRight=“21dp”

android:layout_marginTop=“4dp” >

android:layout_width=“match_parent”

android:layout_height=“1px”

android:layout_marginTop=“5dp”

android:background="#DFDFDF"

android:contentDescription="@null" />

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginLeft=“29dp”

android:contentDescription="@null"

android:src="@drawable/arrow_up" />

最后

**一个零基础的新人,我认为坚持是最最重要的。**我的很多朋友都找我来学习过,我也很用心的教他们,可是不到一个月就坚持不下来了。我认为他们坚持不下来有两点主要原因:

他们打算入行不是因为兴趣,而是因为所谓的IT行业工资高,或者说完全对未来没有任何规划。

刚开始学的时候确实很枯燥,这确实对你是个考验,所以说坚持下来也很不容易,但是如果你有兴趣就不会认为这是累,不会认为这很枯燥,总之还是贵在坚持。

技术提升遇到瓶颈了?缺高级Android进阶视频学习提升自己吗?还有大量大厂面试题为你面试做准备!

点击:Android 学习,面试文档,视频收集大整理

来获取学习资料提升自己去挑战一下BAT面试难关吧

对于很多Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。整理的这些知识图谱希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

不论遇到什么困难,都不应该成为我们放弃的理由!

如果有什么疑问的可以直接私我,我尽自己最大力量帮助你!

文档,视频收集大整理](https://github.com/a120464/Android-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)

来获取学习资料提升自己去挑战一下BAT面试难关吧

[外链图片转存中…(img-LbBKW1WW-1643776308694)]

对于很多Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。整理的这些知识图谱希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

不论遇到什么困难,都不应该成为我们放弃的理由!

如果有什么疑问的可以直接私我,我尽自己最大力量帮助你!

最后祝各位新人都能坚持下来,学有所成。

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。