Android 半圆矩形的实现

新建一个项目 我命名为ARectangle,在res目录下新建drawable包,在这个包下新建a_rectangle.xml,写入代码

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:exitFadeDuration="@android:integer/config_shortAnimTime">
    
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <!-- 填充的颜色 -->
            <solid android:color="#9c2222"/>
            <!-- 设置按钮的四个角为弧形 -->
            <!-- android:radius 弧形的半径 -->
            <corners android:radius="20dp"/>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle">
     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现半圆环效果可以通过自定义 View 来实现。 下面是一个实现的示例代码: ```java public class SemiCircleView extends View { private Paint mPaint; private RectF mRectF; private int mStartAngle; private int mSweepAngle; private int mStrokeWidth; private int mColor; public SemiCircleView(Context context) { this(context, null); } public SemiCircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SemiCircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SemiCircleView); mStartAngle = typedArray.getInt(R.styleable.SemiCircleView_startAngle, 0); mSweepAngle = typedArray.getInt(R.styleable.SemiCircleView_sweepAngle, 180); mStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.SemiCircleView_strokeWidth, 10); mColor = typedArray.getColor(R.styleable.SemiCircleView_color, Color.RED); typedArray.recycle(); mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(mStrokeWidth); mPaint.setColor(mColor); mRectF = new RectF(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); int height = getHeight(); mRectF.set(mStrokeWidth / 2, mStrokeWidth / 2, width - mStrokeWidth / 2, height - mStrokeWidth / 2); canvas.drawArc(mRectF, mStartAngle, mSweepAngle, false, mPaint); } public void setStartAngle(int startAngle) { mStartAngle = startAngle; invalidate(); } public void setSweepAngle(int sweepAngle) { mSweepAngle = sweepAngle; invalidate(); } public void setStrokeWidth(int strokeWidth) { mStrokeWidth = strokeWidth; mPaint.setStrokeWidth(mStrokeWidth); invalidate(); } public void setColor(int color) { mColor = color; mPaint.setColor(mColor); invalidate(); } } ``` 在布局文件中使用: ```xml <com.example.widget.SemiCircleView android:id="@+id/semiCircleView" android:layout_width="200dp" android:layout_height="100dp" app:startAngle="0" app:sweepAngle="180" app:strokeWidth="20dp" app:color="@color/colorAccent"/> ``` 然后在代码中动态设置属性值即可实现半圆环效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值