Android如何绘制圆形矩形和阴影Layout

标签 android android-linearlayout draw shadow rounding

我创建一个类扩展 LinearLayout 我想像这样显示一个矩形:

   ---------------------     => it is round corner 
 | border frame         | .
 |  -----------------   | .
 |  |  hearder      |   | .
 |  | - - - - - - - |   | .
 |  |  center       |   | .
 |  |               |   | .
 |  | - - - - - - - |   | .
 |  |  buttom       |   | .
 |  ----------------    | .
 |                      | .
  ---------------------- .
  . . . . .  . . . . . .      => it is shadow

我使用了 paintShadow.setShadowLayer(this.radius, 8, 8, Color.GRAY);影子不是圆的。

所以谁知道如何制作圆形矩形和阴影。 代码:

@SuppressLint("DrawAllocation")
public class CornerLinearLayout extends LinearLayout{

    public CornerLinearLayout(Context context,AttributeSet attr){
        super(context,attr);
        init();
    }

    public CornerLinearLayout(Context context) {
        super(context);

        init();

        // TODO Auto-generated constructor stub
    }
    private static final int RADIUS=10;
    private int frameColor;
    private int radius;
    private Path mClip;
    private int frameWidth;
    private int headerHeight;
    private int buttomHeight;
    private int headerColor;
    private int buttomColor;
    private int centerColor;
    private void init() {
        this.radius = RADIUS;
        this.frameColor = 0xFFFFFFFF;
        this.frameWidth = 4;
        this.headerHeight = 50;
        this.buttomHeight = 50;
        headerColor = 0xFF31234A;
        buttomColor = 0xFF9ACFFF;
        centerColor = 0xFF55AACC;
        this.setBackgroundColor(0);
//      GradientDrawable gd = new GradientDrawable();
//      gd.setStroke(frameWidth, frameColor);
//      gd.setCornerRadius(radius);
//      gd.setColor(0);
//      //gd.setStroke(30, 0xFFFFFFFF);
//      
//      setBackgroundDrawable(gd);
        this.setPadding(frameWidth, frameWidth, frameWidth,frameWidth);

//      LinearLayout.LayoutParams params = (LayoutParams) this.getLayoutParams();
//      params.setMargins(25, 10, 25, 10);

    }
    private Paint paintShadow = new Paint();
    private Paint paint = new Paint();
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);       
        RectF rf = new RectF(0,0,this.getWidth()-10,this.getHeight()-5);
        paintShadow.setShadowLayer(this.radius, 8, 8, Color.GRAY);
        rf.right -= 3;
        rf.bottom -= 3;
        paintShadow.setColor(this.frameColor);
        paintShadow.setStrokeWidth(5);
        canvas.drawRoundRect(rf,this.radius,this.radius, paintShadow);

        paint.setStyle(Paint.Style.FILL);
//      Rect r = new Rect(5,5,this.getWidth()-5,this.getHeight()-5);
        if(this.centerColor != 0){

            paint.setColor(this.centerColor);
//          canvas.drawRect(r, paint);
            rf.right -= 5;
            rf.left += 5;
            rf.top += 5;
            rf.bottom -=5;
            canvas.drawRoundRect(rf, this.radius, this.radius, paint);
        }
//      if(this.headerColor!=0){
//          Rect rr = new Rect(10,10,this.getWidth()-5,this.headerHeight-10);
//          paint.setColor(this.headerColor);
//          canvas.drawRect(rr, paint);
//      }
//      if(this.buttomColor != 0){
//          Rect rr = new Rect(10,this.getHeight()-this.buttomHeight,this.getWidth()-3,this.buttomHeight-3);
//          paint.setColor(this.headerColor);
//          canvas.drawRect(rr, paint);
//      }
//      


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mClip = new Path();
        RectF rect = new RectF(0, 0, w, h);
        mClip.addRoundRect(rect, this.radius,  this.radius, Direction.CW);

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save();

        canvas.clipPath(mClip);

        super.dispatchDraw(canvas);
        canvas.restore();
    }
}

最佳答案

我敢打赌您的问题出在使用 clipPath() 上。尝试在 View 中禁用硬件加速,我想您会发现它会按预期工作。

在你的 init() 方法中:

setLayerType(LAYER_TYPE_SOFTWARE, null);

有用的链接:

关于Android如何绘制圆形矩形和阴影Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14534512/

相关文章:

android - 在企业移动混合应用程序中使用 Google map Javascript API v3

Android:如何进行垂直合并?

Android onClickListener 自行删除

java - NPE 在自定义 AlertDialog 中设置嵌套 LinearLayout 的可见性时?

ios - 在 UIImage 上绘制删除透视,露出下面的 UIImage 图层

android - React PWA 无法在 Chrome 中打开外部 PDF(Android)

android - 带或不带 ConvertView 的 Listview == null

ios - Swift:在不改变大小的情况下绘制图像(纵横比填充)

android - 如何在扩展 Fragment 的类中创建一个 LinearLayout?

android - 在 RelativeLayout 中从一个 Angular 到另一个 Angular 绘制对 Angular 线