gpt4 book ai didi

java - 在 fragment 中绘制多层矩形

转载 作者:行者123 更新时间:2023-11-30 08:52:41 26 4
gpt4 key购买 nike

在 fragment 中绘制此图像的最佳方式是什么(所有矩形都应使用屏幕的整个宽度,高度应采用特定的 dp 测量值)?显然需要绘制矩形,但我不知道如何在下方的大灰色矩形上方绘制白色和黄色矩形。这也可以使用相同的 fragment java 类来实现,而不必创建一个新的吗?

platform

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#868F98"));
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
canvas.drawRect(0, 0, 200, 200, paint);
LinearLayout ll = (LinearLayout) findViewById(R.id.rect);
ll.setBackgroundDrawable(new BitmapDrawable(bg));
}

最佳答案

你可以使用这样的东西。您可以使用 attrs.xml 中的自定义属性和公共(public)方法进行调整以设置颜色和其他内容以使其更具可定制性,但基本思想在这里。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;


public class LayerRectangle extends View {


private int measuredWidth, measuredHeight;
private Paint mBackgroundPaint, mYellowLinePaint, mWhiteLinePaint;
private RectF mBackgroundRect, mYellowLineRectF, mWhiteLineRectF;


public LayerRectangle(Context context) {
super(context);
init(context, null, 0);
}

public LayerRectangle(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}

public LayerRectangle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attributeSet, int defStyle) {

mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint.setColor(0xFF3C3C3C);
mBackgroundPaint.setStyle(Paint.Style.FILL);

mYellowLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mYellowLinePaint.setColor(0xFFFFFF00);
mYellowLinePaint.setStyle(Paint.Style.FILL);

mWhiteLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mWhiteLinePaint.setColor(0xFFFFFFFF);
mWhiteLinePaint.setStyle(Paint.Style.FILL);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);

mBackgroundRect = new RectF(0, 0, measuredWidth, measuredHeight);
mYellowLineRectF = new RectF(0, 0.7f * measuredHeight, measuredWidth, 0.8f * measuredHeight);
mWhiteLineRectF = new RectF(0, 0.9f * measuredHeight, measuredWidth, measuredHeight);

setMeasuredDimension(measuredWidth, measuredHeight);
}

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

if (measuredHeight == 0 || measuredWidth == 0)
return;

canvas.drawRect(mBackgroundRect, mBackgroundPaint);
canvas.drawRect(mYellowLineRectF, mYellowLinePaint);
canvas.drawRect(mWhiteLineRectF, mWhiteLinePaint);
}
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<replace.me.with.your.package.name.LayerRectangle
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_centerVertical="true"
/>

</RelativeLayout>

这就是它的样子

enter image description here
(来源:shrani.si)

关于java - 在 fragment 中绘制多层矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30110609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com