Java 在片段中绘制多层矩形
在片段中绘制此图像的最佳方式是什么(所有矩形应使用屏幕的整个宽度,高度应以特定的dp测量值为准)?显然需要绘制矩形,但我不知道如何在下面的大灰色矩形顶部绘制白色和黄色矩形。这也可以通过使用相同的片段java类而不是创建一个新的类来实现吗Java 在片段中绘制多层矩形,java,android,xml,Java,Android,Xml,在片段中绘制此图像的最佳方式是什么(所有矩形应使用屏幕的整个宽度,高度应以特定的dp测量值为准)?显然需要绘制矩形,但我不知道如何在下面的大灰色矩形顶部绘制白色和黄色矩形。这也可以通过使用相同的片段java类而不是创建一个新的类来实现吗 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac
@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中的自定义属性和公共方法来调整is,以设置颜色和内容,使其更具自定义性,但基本思想如下
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>
这就是它的样子
(来源:)你需要尝试一些东西,然后再回来。是的,我试着保持它干净,不把不必要的东西放进去,就像我提到的自定义属性和其他方法一样。你可以在任何场合使用它ViewGrouo@BojanKseneman好啊虽然你看不到,但在我的画的底部也有一条白线(与黄线的高度相同)。请您根据需要调整代码,将最下面的白线包括在内好吗?@BojanKseneman Brilliant。谢谢我怎样才能做同样的事情,但是上面有一条白线,下面有一条黄线?在onMeasure方法中定义每个矩形的位置及其大小。如果要交换teo,只需交换在onMeasure过程中设置为矩形的参数