android---自定义ViewGroup

博客内容涉及XML布局文件,还提到编写一个类继承ViewGroup,这些都与前端开发相关,通过操作XML布局文件和自定义视图组,可实现特定的界面布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Second">
    <com.yifei.myapplication.MyGroupView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/a"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/b"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/c"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/d"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/e"
            />
    </com.yifei.myapplication.MyGroupView>

</LinearLayout>

写一个类继承ViewGroup

package com.yifei.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;

public class MyGroupView extends ViewGroup {
    private int mscreenHeight;
    private Scroller scroller;
    private int lastY;
    private int start;
    private int end;

    public MyGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);//初始化
    }

    private void initView(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);//获得
        mscreenHeight = dm.heightPixels;//得到高度
        scroller = new Scroller(context);//初始化

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();//获取所有子控件 的长度
        //遍历每一个子控件
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);//获取到每一个子控件measureChild(childView,widthMeasureSpec,heightMeasureSpec);//测量子控件

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastY = y;
                start = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                if(!scroller.isFinished()){
                    scroller.abortAnimation();
                }
                int dy = lastY-y;
                if(getScaleY()<0){
                    dy =0;
                }
                if(getScaleY()>getHeight()-mscreenHeight){
                    dy = 0;
                }
                scrollBy(0,dy);
                lastY =y;
                break;
            case MotionEvent.ACTION_UP:
                end = getScrollY();
                int dscrolly = end -start;
                if(dscrolly>0){
                    if(dscrolly<mscreenHeight/3){
                        scroller.startScroll(0,getScrollY(),0,-dscrolly);
                    }else {
                        scroller.startScroll(0,getScrollY(),0,mscreenHeight-dscrolly);
                    }
                }else {
                    if(-dscrolly<mscreenHeight/3){
                        scroller.startScroll(0,getScrollY(),0,-dscrolly);
                    }else {
                        scroller.startScroll(0,getScrollY(),0,-mscreenHeight);
                    }
                }
                break;
            default:

                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int clildCount = getChildCount();
//        设置ViewGroup的高度
        MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
        mlp.height = mscreenHeight * clildCount;
        setLayoutParams(mlp);
        for (int i = 0; i < clildCount; i++) {
            View clild = getChildAt(i);
            if (clild.getVisibility() != View.GONE) {//
                clild.layout(1, i * mscreenHeight, r, (i + 1) * mscreenHeight);
            }
        }
    }

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值