首页 文章

在片段中绘制多层矩形

提问于
浏览
2

在片段中绘制此图像的最佳方法是什么(所有矩形应使用屏幕的整个宽度,高度应在特定的dp测量中)?显然需要绘制矩形,但我不知道如何在下面的大灰色矩形顶部绘制白色和黄色矩形 . 也可以使用相同的片段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));   
 }

1 回答

  • 2

    你可以使用这样的东西 . 您可以使用attrs.xml和公共方法调整自定义属性来设置颜色和内容以使其更具可定制性,但基本思路就在这里 .

    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>
    

    这就是它的样子

    http://shrani.si/f/2c/pb/4VoNhZ2y/layerrectangle2.png

相关问题