6

我让一个类扩展了 LinearLayout 我想显示一个这样的 Rect:

   ---------------------     => 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();
    }
}
4

4 回答 4

4

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

在您的 init() 方法中:

setLayerType(LAYER_TYPE_SOFTWARE, null);

有用的链接:

于 2013-01-26T07:32:54.037 回答
3

使用九个补丁可绘制对象。这些将为您节省大量代码。查看九个补丁的一些教程

此外,看看你的<SDK folder>/platforms/<android-xx>/data/res开发平台附带了许多现成的默认主题的九个补丁可绘制对象,你可以将它们复制到你的应用程序中。

代码示例:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //-- draw with white color--
        Paint p = new Paint();
        p.setColor(Color.WHITE);

        //-- set shadow, 5dp down, 5 dp left, with radius of 15 dp--
        p.setShadowLayer(10,5,5,Color.BLACK);

        //-- warning, Honeycomb and above only
        //-- this will reduce draw performance of view
        //-- but is required to support drawing filters, like shadow, blur etc
        setLayerType(LAYER_TYPE_SOFTWARE,p);

        //--basic shapes don't have round corners yet, so use path--
        Path pt = new Path();

        //-- round rectangle path with 15dp padding (space for shadow)
        //-- and 10 dp corner radius
        pt.addRoundRect(new RectF(15,15,getWidth() - 15 ,getHeight() -15 ),
                        10,10, Path.Direction.CW);

        //--draw--
        canvas.drawPath(pt,p);

    }
于 2013-01-26T07:41:56.320 回答
0

而不是使用 setShadowlayer,而是直接绘制一个带有 alpha 颜色值的圆形矩形,如 color=0x66000000。一个简单的黑色阴影。

您可以通过更改rect参数来调整它,然后再容纳其他绘图以匹配它,否则它将被全部覆盖

于 2013-01-26T07:03:41.703 回答
0

有一个关于这个问题的很好的教程,由 Romain Guy 制作,在这里

我发现的唯一缺点是您需要将位图准备为您希望使用它的确切尺寸,否则它会涂抹。

于 2013-01-26T09:25:07.447 回答