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
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值