Android 不规则的按钮组合 自定义控件

public class ProgressView extends View {

    private int mWidth, mHeight;
    private int colorLeft, colorRight, colorRightProcess;
    private Paint mLeftPaint, mRightPaint;
    private Path mLeftPath, mRightPath;
    private int arcRadius;
    private int padding;
    private Region leftRegion, rightRegion;

    CornerPathEffect cornerPathEffect;
    private int progress = 100;

    private OnClickListener mLeftClick, mRightClick;

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        colorLeft = Color.parseColor("#5890FD");
        colorRight = Color.parseColor("#c2ecef");
        colorRightProcess = Color.parseColor("#13C5CD");
        mLeftPaint = new Paint();
        mLeftPaint.setAntiAlias(true);
        mLeftPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mLeftPaint.setStrokeCap(Paint.Cap.ROUND);
        arcRadius = 20;
        cornerPathEffect = new CornerPathEffect(arcRadius);
        mLeftPaint.setPathEffect(cornerPathEffect);

        mRightPaint = new Paint();
        mRightPaint.setAntiAlias(true);
        mRightPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mRightPaint.setStrokeCap(Paint.Cap.ROUND);


        padding = 10;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();


        mLeftPath = new Path();
        mLeftPath.moveTo(mHeight / 2, 0);
        mLeftPath.lineTo(mWidth / 2 + mHeight / 2 - padding, 0);
        mLeftPath.lineTo(mWidth / 2 - mHeight / 2 - padding, mHeight);
        mLeftPath.lineTo(mHeight / 2, mHeight);
        mLeftPath.addArc(new RectF(0, 0, mHeight, mHeight), 90, 180);
        mLeftPath.close();

        mRightPath = new Path();

        mRightPath.moveTo(mWidth / 2 + mHeight / 2 + padding, 0);
        mRightPath.lineTo(mWidth - mHeight / 2, 0);
        mRightPath.addArc(new RectF(mWidth - mHeight, 0, mWidth, mHeight), 270, 180);
        mRightPath.lineTo(mWidth - mHeight / 2, mHeight);
        mRightPath.lineTo(mWidth / 2 - mHeight / 2 + padding, mHeight);
        mRightPath.lineTo(mWidth / 2 + mHeight / 2 + padding, 0);
        mRightPath.close();

        leftRegion = new Region();
        RectF leftRectF = new RectF();
        mLeftPath.computeBounds(leftRectF, true);
        leftRegion.setPath(mLeftPath, new Region((int) leftRectF.left, (int) leftRectF.top, (int) leftRectF.right, (int) leftRectF.bottom));

        rightRegion = new Region();
        RectF rightRectF = new RectF();
        mRightPath.computeBounds(rightRectF, true);
        rightRegion.setPath(mRightPath, new Region((int) rightRectF.left, (int) rightRectF.top, (int) rightRectF.right, (int) rightRectF.bottom));


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mLeftPaint.setColor(colorLeft);
        canvas.drawPath(mLeftPath, mLeftPaint);

        int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), mRightPaint, Canvas.ALL_SAVE_FLAG);
        int start = mWidth / 2 - mHeight / 2 + padding;

        mRightPaint.setColor(colorRight);
        mRightPaint.setPathEffect(cornerPathEffect);
        canvas.drawPath(mRightPath, mRightPaint);

        mRightPaint.setColor(colorRightProcess);
        mRightPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        mRightPaint.setPathEffect(null);
        int end = (mWidth - start) * progress / 100 + start;

        canvas.drawRect(new RectF(start, 0, end, mHeight), mRightPaint);
        mRightPaint.setXfermode(null);
        canvas.restoreToCount(sc);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (leftRegion.contains((int) event.getX(), (int) event.getY()) ||
                    rightRegion.contains((int) event.getX(), (int) event.getY())) {

                return true;
            }
        }

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (leftRegion.contains((int) event.getX(), (int) event.getY())) {
                if (mLeftClick != null) {
                    mLeftClick.onClick(this);
                }
                LogUtils.e("左边点击");
                return true;
            }
            if (rightRegion.contains((int) event.getX(), (int) event.getY())) {
                if (mRightClick != null) {
                    mRightClick.onClick(this);
                }
                LogUtils.e("右边点击");
                return true;
            }
        }
        return super.onTouchEvent(event);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public void setLeftClick(OnClickListener mLeftClick) {
        this.mLeftClick = mLeftClick;
    }

    public void setRightClick(OnClickListener mRightClick) {
        this.mRightClick = mRightClick;
    }
}

 使用方式 如下

  <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/w44"
                    android:layout_weight="1">

                    <com.xxx.xxx.widget.ProgressView
                        android:id="@+id/downProgressButton"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/w44"
                        android:layout_weight="1" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">


                        <FrameLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1">

                            <TextView
                                style="@style/Text.Common"
                                android:layout_gravity="center"
                                android:layout_weight="1"
                                android:drawableLeft="@mipmap/ic_game_custer"
                                android:drawablePadding="@dimen/w2"
                                android:gravity="center"
                                android:text="客服"
                                android:textColor="@color/white"
                                android:textSize="@dimen/sp15" />
                        </FrameLayout>


                        <FrameLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1">

                            <TextView
                                android:id="@+id/appSize"
                                style="@style/Text.Common"
                                android:layout_alignParentRight="true"
                                android:layout_gravity="center"
                                android:layout_weight="1"
                                android:gravity="center"
                                android:textColor="@color/white"
                                android:textSize="@dimen/sp15"
                                tools:text="下载(450MB)" />
                        </FrameLayout>


                    </LinearLayout>


                </FrameLayout>

binding.downProgressButton.setProgress(100); //设置进度
//下载按钮左边点击事件
binding.downProgressButton.setLeftClick(v -> {

});

//下载按钮右边点击事件
binding.downProgressButton.setRightClick(v -> {
 
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 控件集合是指在Android开发中,用于构建用户界面的一组预定义控件的集合。Android提供了丰富的控件可供开发者使用,包括文本框、按钮、图像视图、列表视图、下拉框等。这些控件可以通过XML布局文件或者Java代码动态创建和配置。 Android 控件集合具有以下特点: 1. 多样性:Android控件集合中包含了丰富的控件种类,满足了不同应用场景下的需求。开发者可以根据具体的界面设计要求选择合适的控件进行使用。 2. 可定制性:Android控件集合提供了丰富的配置属性,开发者可以通过设置这些属性来满足自己的设计需求。同时还可以通过自定义控件继承或覆盖Android原生控件,实现更加个性化的效果。 3. 事件监听:Android控件集合支持事件监听机制,开发者可以为控件添加相应的事件处理器,对用户的交互行为做出响应。比如,可以为按钮控件添加点击事件监听器,实现点击按钮后执行相应的操作。 4. 布局管理:Android控件集合中的控件可以通过各种布局管理器进行灵活的排列和组合。比如,线性布局、相对布局、帧布局等。开发者可以根据需求选择合适的布局管理器,实现想要的页面布局效果。 综上所述,Android控件集合提供了丰富多样的控件供开发者使用,具备较高的可定制性和灵活性,方便开发者构建出各种各样的用户界面。通过合理运用和配置控件集合,可以实现强大的Android应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值