how to build a trapezoid shape in android?

前端 未结 3 521
长情又很酷
长情又很酷 2021-01-04 08:23

how can I create a trapezoid shape like below image ?

\"enter

I don\'t want to

3条回答
  •  心在旅途
    2021-01-04 08:46

    This class is a View that defines and draws a trapezoid ShapeDrawable. Thus the trapezoid, being a Drawable, can be used in backgrounds as well.

    package com.stackoverflow.questions.q25768037;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.drawable.ShapeDrawable;
    import android.graphics.drawable.shapes.PathShape;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class TrapezoidView extends View {
    
        private ShapeDrawable mTrapezoid;
    
        public TrapezoidView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            Path path = new Path();
            path.moveTo(0.0f, 0.0f);
            path.lineTo(100.0f, 0.0f);
            path.lineTo(200.0f, 100.0f);
            path.lineTo(0.0f, 100.0f);
            path.lineTo(0.0f, 0.0f);
    
            mTrapezoid = new ShapeDrawable(new PathShape(path, 200.0f, 100.0f));
            mTrapezoid.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
            mTrapezoid.getPaint().setStrokeWidth(1.0f);
            mTrapezoid.getPaint().setColor(Color.GREEN);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            mTrapezoid.setBounds(0, 0, w, h);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            mTrapezoid.draw(canvas);
        }
    }
    

提交回复
热议问题