【问题标题】:Cutting the corners off a layout削减布局的角落
【发布时间】:2015-09-10 12:11:49
【问题描述】:

我在 android 中有一个需要特定形状的布局,即:

角被切断的地方。有没有办法在不将布局背景设置为图像的情况下以编程方式执行此操作? 我希望让应用程序的大小尽可能小,因此必须尽量减少应用程序中可绘制对象的数量。

【问题讨论】:

    标签: android android-linearlayout android-shapedrawable


    【解决方案1】:
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.drawable.shapes.Shape;
    
    public class WeirdShape extends Shape {
        private static final int    COLOUR       = Color.BLACK;
        private static final float  STROKE_WIDTH = 1.0f;
        private static final float  CORNER = 35.0f;
    
        private final Paint border = new Paint();
        private final Path  path;  
    
        public WeirdShape() {
           path   = new Path();
    
            border.setColor      (COLOUR);
            border.setStyle      (Paint.Style.FILL);
            border.setStrokeWidth(STROKE_WIDTH);
            border.setAntiAlias  (true);
            border.setDither     (true);
            border.setStrokeJoin (Paint.Join.ROUND);  
            border.setStrokeCap  (Paint.Cap.ROUND);  
        }
    
        @Override
        protected void onResize(float width, float height) {
            super.onResize(width, height);
    
            float dx = STROKE_WIDTH/2.0f;
            float dy = STROKE_WIDTH/2.0f;
            float x  = dx;
            float y  = dy;
            float w  = width  - dx;
            float h  = height - dy;
    
            //RectF arc = new RectF(x,h-2*CORNER,x+2*CORNER,h);
    
            path.reset();
            path.moveTo(x + CORNER,y);
            path.lineTo(w - CORNER,y);
            path.lineTo(w,y + CORNER);
            path.lineTo(w, h);
            path.lineTo(x + CORNER,h);
           // path.arcTo (arc,90.0f,90.0f);
            path.lineTo(dx,h - CORNER);
            path.lineTo(dx,y);//path.lineTo(dx,y + CORNER);
            path.close();
        }
    
    
        @Override
        public void draw(Canvas canvas, Paint paint) {
            // TODO Auto-generated method stub
            canvas.drawPath(path,border);
        }
    }
    

    然后使用ShapeDrawable中的自定义Shape作为背景Drawable:

    view.setBackground(new ShapeDrawable(new WeirdShape()));
    

    看起来像:

    【讨论】:

    • 画布。(路径,边框);这里缺少什么?
    • 它是 .drawPath,我已经相应地编辑了你的答案
    • 我会改变什么来使形状像我的图像?并填充形状?
    • 我已经编辑了我的答案。请检查。确保为视图的宽度和高度都做了一个 fill_parent。
    【解决方案2】:

    您可以使用材料组件库提供的ShapeAppearanceModel

        <LinearLayout
            android:id="@+id/layout"
            ..>
    

    与:

        val radius = resources.getDimension(R.dimen.cornerSize16)
    
        val linearLayout = findViewById<LinearLayout>(R.id.layout)
        val shapeAppearanceModel = ShapeAppearanceModel()
            .toBuilder()
            .setTopRightCorner(CornerFamily.CUT, radius)
            .setBottomLeftCorner(CornerFamily.CUT, radius)
            .build()
    
        val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
        ViewCompat.setBackground(linearLayout, shapeDrawable)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 2014-05-11
      • 2012-01-18
      • 2019-09-29
      相关资源
      最近更新 更多