android 一些通用View

本文将深入探讨Android开发中的一种常见组件——DividerLineLinearLayout,它常用于在列表或布局中添加分隔线,提升界面清晰度。我们将讨论其使用方法、自定义属性以及在实际项目中的应用。

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

1. DividerLineLinearLayout

import android.content.Context ;
import android.content.res.TypedArray ;
import android.graphics.Canvas ;
import android.graphics.drawable.Drawable ;
import android.util.AttributeSet ;
import android.view.View ;
import android.widget.LinearLayout ;

/**
* Created by whuthm
* 分隔线线性布局
*/
public class DividerLineLinearLayout extends LinearLayout {
   
    private int mDividerTopMargin;
    private int mDividerBottomMargin ;
    private int mDividerLeftMargin ;
    private int mDividerRightMargin ;
   
    private Drawable mDividerDrawable;
    private int mDividerWidth ;
    private int mDividerHeight ;
   
    private boolean mDrawStart ;
    private boolean mDrawEnd ;
   
    public DividerLineLinearLayout(Context context) {
        this (context, null) ;
    }
   
    public DividerLineLinearLayout(Context context , AttributeSet attrs) {
        super (context, attrs) ;
       
        setWillNotDraw( false);
       
        final TypedArray a = context.obtainStyledAttributes(attrs ,
                R.styleable.DividerLinearLayout) ;
        mDividerDrawable = a.getDrawable(R.styleable.DividerLinearLayout_android_divider );
        mDividerWidth = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_dividerWidth, 0 );
        mDividerHeight = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_android_dividerHeight, 0 );
       
        final int horMargin = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_dividerHorMargin, 0 );
        final int verMargin = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_dividerVerMargin, 0 );
        mDividerTopMargin = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_dividerTopMargin, verMargin) ;
        mDividerBottomMargin = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_dividerBottomMargin, verMargin) ;
        mDividerLeftMargin = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_dividerLeftMargin, horMargin) ;
        mDividerRightMargin = a.getDimensionPixelSize(
                R.styleable.DividerLinearLayout_dividerRightMargin, horMargin) ;
       
        mDrawStart = a.getBoolean(R.styleable.DividerLinearLayout_drawStart, true) ;
        mDrawEnd = a.getBoolean(R.styleable.DividerLinearLayout_drawEnd, true) ;
        a.recycle() ;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super .dispatchDraw(canvas);
        final int orientation = getOrientation() ;
        final Drawable d = mDividerDrawable;
        final int width = getWidth() ;
        final int height = getHeight() ;
        if (d != null && width > 0 && height > 0) {
            if (orientation == HORIZONTAL && mDividerWidth > 0 ) {
                drawHorizontalDividerLine(canvas) ;
            }
            else if (orientation == VERTICAL && mDividerHeight > 0 ) {
                drawVerticalDividerLine(canvas) ;
            }
        }
    }

    private void drawHorizontalDividerLine(Canvas canvas) {
        final Drawable d = mDividerDrawable;
        final int width = getWidth() ;
        final int height = getHeight() ;
        int left ;
        int right ;
        int top = mDividerTopMargin;
        int bottom = height - mDividerBottomMargin;
        if (bottom <= top) {
            return;
        }
       
        int count = getChildCount();
        if (count > 0 && mDrawStart ) {
            left = 0 ;
            right = left + mDividerWidth;
            d.setBounds(left , top, right, bottom) ;
            d.draw(canvas) ;
        }
        for (int i = 0; i < count - 1; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == View. VISIBLE) {
                left = child.getRight();
                right = left + mDividerWidth ;
                d.setBounds(left, top, right , bottom);
                d.draw(canvas);
            }
        }
       
        if (count > 0 && mDrawEnd) {
            left = width - mDividerWidth;
            right = left + mDividerWidth;
            d.setBounds(left , top, right, bottom) ;
            d.draw(canvas) ;
        }
    }
   
    private void drawVerticalDividerLine(Canvas canvas) {
        final Drawable d = mDividerDrawable;
        final int width = getWidth() ;
        final int height = getHeight() ;
        int left = mDividerLeftMargin;
        int right = width - mDividerRightMargin;
        int top ;
        int bottom ;
        if (right <= left) {
            return;
        }
       
        int count = getChildCount();
        if (count > 0 && mDrawStart ) {
            top = 0 ;
            bottom = top + mDividerHeight;
            d.setBounds(left , top, right, bottom) ;
            d.draw(canvas) ;
        }
        for (int i = 0; i < count - 1; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == View. VISIBLE) {
                top = child.getBottom();
                bottom = top + mDividerHeight ;
                d.setBounds(left, top, right , bottom);
                d.draw(canvas);
            }
        }
       
        if (count > 0 && mDrawEnd) {
            top = height - mDividerHeight;
            bottom = top + mDividerHeight;
            d.setBounds(left , top, right, bottom) ;
            d.draw(canvas) ;
        }
    }
}
 attrs

<!--Divider Line-->
<declare-styleable name="DividerLinearLayout">
    <attr name= "android:dividerHeight" />
    <attr name= "android:divider" />
    <attr name= "dividerWidth" />
    <attr name= "dividerTopMargin" format="reference|dimension" />
    <attr name= "dividerBottomMargin" format="reference|dimension" />
    <attr name= "dividerLeftMargin" format="reference|dimension" />
    <attr name= "dividerRightMargin" format="reference|dimension" />
    <attr name= "dividerHorMargin" format="reference|dimension" />
    <attr name= "dividerVerMargin" format="reference|dimension" />
    <attr name= "drawStart" format="boolean" />
    <attr name= "drawEnd" format="boolean" />
</declare-styleable>


2.三角形 View TriangleArrowTextView

import android.content.Context ;
import android.graphics.Canvas ;
import android.graphics.Path ;
import android.graphics.drawable.ColorDrawable ;
import android.graphics.drawable.Drawable ;
import android.util.AttributeSet ;
import android.widget.TextView ;

/**
* Created by whuthm 
* 父控件必须android:clipChildren="false"
*/
public class TriangleArrowTextView extends TextView {
   
    private int mTriangleWidth;
   
    public TriangleArrowTextView(Context context) {
        super (context);
        init() ;
    }
   
    public TriangleArrowTextView(Context context , AttributeSet attrs) {
        super (context, attrs) ;
        init() ;
    }
   
    public TriangleArrowTextView(Context context , AttributeSet attrs, int defStyle) {
        super (context, attrs , defStyle);
        init() ;
    }
   
    private void init() {
        mTriangleWidth = getResources().getDimensionPixelSize(R.dimen. triangle_width);
        mTriangleWidth = mTriangleWidth - mTriangleWidth % 2 ;
    }
   
    @Override
    protected void onDraw(Canvas canvas) {
        super .onDraw(canvas);
        int width = getWidth() ;
        int l = width / 20;
        if ( mTriangleWidth <= 0 || l + mTriangleWidth > width) {
            return;
        }
        int halfWidth = mTriangleWidth / 2;
        Drawable background = getBackground() ;
        if (background instanceof ColorDrawable) {
            Drawable.ConstantState constantState = background.getConstantState() ;
            if (constantState != null) {
                Drawable d = constantState.newDrawable(getResources()) ;
                if (d instanceof ColorDrawable) {
                    ColorDrawable colorDrawable = (ColorDrawable) d ;
                    Path path = new Path();
                    path.reset();
                    path.moveTo(l, 0); // 此点为多边形的起点
                    path.lineTo(l + halfWidth, -halfWidth);
                    path.lineTo(l + mTriangleWidth , 0) ;
                    path.close(); // 使这些点构成封闭的多边形
                   
                    canvas.save();
                    canvas.clipPath(path);
                    colorDrawable.setBounds(l, -halfWidth, l + mTriangleWidth, 0 );
                    colorDrawable.draw(canvas);
                    canvas.restore();
                }
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值