安卓画一个白底红边的圆形背景
时间: 2024-04-18 17:27:14 浏览: 80
好的,我可以帮你生成一段代码来绘制一个白底红边的圆形背景。请注意,以下代码仅提供一个简单的示例,你可能需要根据自己的需求进行适当的调整。
```java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class CircleBackgroundView extends View {
private Paint circlePaint;
private Paint borderPaint;
public CircleBackgroundView(Context context) {
super(context);
init();
}
private void init() {
circlePaint = new Paint();
circlePaint.setColor(Color.WHITE);
circlePaint.setAntiAlias(true);
borderPaint = new Paint();
borderPaint.setColor(Color.RED);
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(5);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int radius = Math.min(centerX, centerY);
canvas.drawCircle(centerX, centerY, radius, circlePaint);
canvas.drawCircle(centerX, centerY, radius - borderPaint.getStrokeWidth() / 2, borderPaint);
}
}
```
你可以在你的布局文件中使用这个自定义的视图:
```xml
<LinearLayout
...
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
<com.example.yourpackage.CircleBackgroundView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@android:color/transparent" />
...
</LinearLayout>
```
请将 `com.example.yourpackage` 替换为你的包名,然后将 `CircleBackgroundView` 放在你希望显示圆形背景的位置。
阅读全文