本文实例为大家分享了Android ScrollView实现水平滑动回弹的具体代码,供大家参考,具体内容如下 在研究了View的一些属性之后做了个Scroll的水平滑动回弹。 效果图
本文实例为大家分享了Android ScrollView实现水平滑动回弹的具体代码,供大家参考,具体内容如下
在研究了View的一些属性之后做了个Scroll的水平滑动回弹。
效果图:
主要代码:
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
public class MHorizontalScrollView extends HorizontalScrollView {
private View view;
private static final int deltaX = 1;
private Rect nORMalRt = new Rect();
public MHorizontalScrollView(Context context) {
super(context);
}
public MHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onFinishInflate() {
if (getChildCount() > 0) {
view = getChildAt(0);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (view != null) {
onTouchEventImpl(event);
}
return super.onTouchEvent(event);
}
private void onTouchEventImpl(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
// 在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位
scrollBy(deltaX, 0);
// 当滚动到最左或最右时就不会再滚动,这时移动布局达到回弹效果
if (isLayoutMove()) {
if (normalRt.isEmpty()) {
// 保存当前正常的布局位置,拉过头才能回弹到正常位置
normalRt.set(view.getLeft(), view.getTop(),
view.getRight(), view.getBottom());
}
// 移动布局
view.layout(view.getLeft() - deltaX, view.getTop(),
view.getRight() - deltaX, view.getBottom());
}
break;
case MotionEvent.ACTION_UP:
if (isNeedAnimation()) {
animationImpl();
}
break;
default:
break;
}
}
private void animationImpl() {
// 移动动画
TranslateAnimation ta = new TranslateAnimation(view.getLeft(),
normalRt.left, 0, 0);
// 动画持续时间
ta.setDuration(50);
view.startAnimation(ta);
// 设置回到当前正常的布局位置
view.layout(normalRt.left, normalRt.top, normalRt.right,
normalRt.bottom);
normalRt.setEmpty();
}
private boolean isNeedAnimation() {
return !normalRt.isEmpty();
}
private boolean isLayoutMove() {
int offset = view.getMeasuredWidth() - getWidth();
if (offset <= 0) {
return false;
}
// 上面已固定deltaX=1,scrollX永远等于1所以向右拉不动
// 但当向左拉动到内容布局的最右端时scrollX == offset时还可以继续拉动
int scrollX = getScrollX();
if (scrollX == 0 || scrollX == offset) {
return true;
}
return false;
}
}
在xml布局文件里直接使用:
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background"
>
<cn.qhg.MHorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingTop="100dp"
android:id="@+id/ll_test"
android:onClick="test"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_1"
android:layout_marginRight="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_4"
android:layout_marginRight="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_2"
android:layout_marginRight="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_3"
android:layout_marginRight="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_1"
android:layout_marginRight="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_4"
android:layout_marginRight="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_2"
android:layout_marginRight="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/house_3"
android:layout_marginRight="40dp"
/>
<!-- 使右边多空一点 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
/>
</LinearLayout>
</cn.qhg.MHorizontalScrollView>
</LinearLayout>
--结束END--
本文标题: androidScrollView实现水平滑动回弹
本文链接: https://www.lsjlt.com/news/146416.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0