自定义ViewGroup

本文介绍了一种自定义ViewGroup实现的ScrollView效果,当滑动距离超过屏幕高度的1/3时,会自动滚动到相邻的控件。通过继承ViewGroup并重写关键方法,实现了特殊的滚动行为。

实现ScrollView的效果,且滚动带有“黏性”,滑动距离大于控件高度(屏幕高度)的1/3时,会自动滚动到上一个(下一个)控件

public class DiyViewGroup extends ViewGroup {
    private int mScreenHeight;
    private int mLastY;
    private Scroller mScroller;
    private int mStart;
    private int mEnd;

    public DiyViewGroup(Context context) {
        super(context);
        init(context);
    }

    public DiyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DiyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    // 初始化mScreenHeight和mScroller
    private void init(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenHeight = outMetrics.heightPixels;
        mScroller = new Scroller(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        // 对子View进行布局
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if(child.getVisibility() != GONE) {
                child.layout(l, mScreenHeight * i, r, mScreenHeight * (i + 1));
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        // 通知子View对自身进行测量
        for (int i = 0; i <count ; i++) {
            View childView = getChildAt(i);
            // 可能子View的宽高是match_parent,所以要将父控件的宽高传进去
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
        // 设置ViewGroup自己的宽高
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = mScreenHeight * count;
        setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("qqq", "onTouchEvent: down");
                mLastY = y;
                // 记录触摸起点
                // getScrollY()从最原始位置向上滚动的距离
                mStart = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("qqq", "onTouchEvent: move");
                if(!mScroller.isFinished()) {
                    // 停止动画,立即移动到终点位置
                    mScroller.abortAnimation();
                }
                int dy = mLastY - y;
                if(getScrollY() < 0) {
                    dy = 0;
                }
                if(getScrollY() + mScreenHeight > getHeight()) {
                    dy = 0;
                }
                scrollBy(0, dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                Log.i("qqq", "onTouchEvent: up");
                // 记录触摸终点
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if(dScrollY > 0) {
                    if(dScrollY < mScreenHeight / 3) {
                        // 第三个参数第四个参数分别是向左移动的距离和向上移动的距离
                        mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    } else {
                        mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - dScrollY);
                    }
                } else {
                    if(-dScrollY < mScreenHeight / 3) {
                        mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    } else {
                        mScroller.startScroll(0, getScrollY(), 0, -(mScreenHeight - (-dScrollY)));
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(mScroller.computeScrollOffset()) {
            scrollTo(0, mScroller.getCurrY());
            postInvalidate();
        }
    }
}


基于径向基函数神经网络RBFNN的自适应滑模控制学习(Matlab代码实现)内容概要:本文介绍了基于径向基函数神经网络(RBFNN)的自适应滑模控制方法,并提供了相应的Matlab代码实现。该方法结合了RBF神经网络的非线性逼近能力和滑模控制的强鲁棒性,用于解决复杂系统的控制问题,尤其适用于存在不确定性和外部干扰的动态系统。文中详细阐述了控制算法的设计思路、RBFNN的结构与权重更新机制、滑模面的构建以及自适应律的推导过程,并通过Matlab仿真验证了所提方法的有效性和稳定性。此外,文档还列举了大量相关的科研方向和技术应用,涵盖智能优化算法、机器学习、电力系统、路径规划等多个领域,展示了该技术的广泛应用前景。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的研究生、科研人员及工程技术人员,特别是从事智能控制、非线性系统控制及相关领域的研究人员; 使用场景及目标:①学习和掌握RBF神经网络与滑模控制相结合的自适应控制策略设计方法;②应用于电机控制、机器人轨迹跟踪、电力电子系统等存在模型不确定性或外界扰动的实际控制系统中,提升控制精度与鲁棒性; 阅读建议:建议读者结合提供的Matlab代码进行仿真实践,深入理解算法实现细节,同时可参考文中提及的相关技术方向拓展研究思路,注重理论分析与仿真验证相结合。
### 创建或使用自定义 ViewGroup in Android 在 Android 中,创建自定义 `ViewGroup` 是一个复杂但非常强大的功能。它允许开发者根据自己的需求设计和实现布局逻辑。以下是一个完整的指南,涵盖从基本概念到具体实现的各个方面。 #### 1. 自定义 ViewGroup 的基本原理 `ViewGroup` 是一个容器类,它可以包含其他 `View` 或 `ViewGroup`。与普通的 `View` 不同,`ViewGroup` 需要处理子元素的测量、布局和绘制。因此,在自定义 `ViewGroup` 时,通常需要重写以下几个方法: - `onMeasure()`:用于测量当前视图及其子视图的大小[^1]。 - `onLayout()`:用于确定每个子视图的位置[^1]。 - `generateLayoutParams()` 和 `checkLayoutParams()`:用于处理子视图的布局参数,避免出现 `ClassCastException` 等错误[^2]。 #### 2. 实现步骤 ##### (1) 创建自定义 ViewGroup 类 首先,继承 `ViewGroup` 并定义构造函数。例如: ```java public class CustomViewGroup extends ViewGroup { public CustomViewGroup(Context context) { super(context); } public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } } ``` ##### (2) 重写 `onMeasure()` 方法 `onMeasure()` 方法用于测量当前视图及其子视图的大小。可以通过遍历所有子视图并调用它们的 `measure()` 方法来完成测量过程。例如: ```java @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(width, height); // 设置最终的宽高 } ``` ##### (3) 重写 `onLayout()` 方法 `onLayout()` 方法用于确定每个子视图的位置。通过调用子视图的 `layout()` 方法,可以将它们放置在指定的位置。例如: ```java @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); int x = 0; int y = 0; for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); child.layout(x, y, x + childWidth, y + childHeight); x += childWidth; // 假设水平排列 } } } ``` ##### (4) 处理布局参数 为了避免运行时异常(如 `ClassCastException`),需要重写 `generateLayoutParams()` 和 `checkLayoutParams()` 方法。例如: ```java @Override protected LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } @Override protected boolean checkLayoutParams(LayoutParams p) { return p instanceof MarginLayoutParams; } ``` #### 3. 示例代码 以下是一个完整的自定义 `ViewGroup` 示例,实现了水平排列的子视图布局: ```java public class HorizontalViewGroup extends ViewGroup { public HorizontalViewGroup(Context context) { super(context); } public HorizontalViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int totalWidth = 0; int maxHeight = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { measureChild(child, widthMeasureSpec, heightMeasureSpec); totalWidth += child.getMeasuredWidth(); maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); } } setMeasuredDimension(resolveSize(totalWidth, widthMeasureSpec), resolveSize(maxHeight, heightMeasureSpec)); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int x = 0; int y = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); child.layout(x, y, x + childWidth, y + childHeight); x += childWidth; } } } @Override protected LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } @Override protected boolean checkLayoutParams(LayoutParams p) { return p instanceof MarginLayoutParams; } } ``` #### 4. 使用自定义 ViewGroup 在 XML 文件中声明自定义 `ViewGroup`,并添加子视图。例如: ```xml <com.example.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 2" /> </com.example.CustomViewGroup> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值