2012-12-21 32 views
6

Tôi đang cố vẽ một vòng tròn nhỏ hơn trong vòng tròn khác. Nó có vẻ khá đơn giản nhưng Im gặp rắc rối với điều này và couldnt tìm thấy một câu trả lời. Mã im sử dụng là:Làm thế nào để vẽ một ShapeDrawable nhỏ hơn bên trong một hình dạng khác Có thể vẽ được lập trình

ShapeDrawable biggerCircle= new ShapeDrawable(new OvalShape()); 
    biggerCircle.setIntrinsicHeight(60); 
    biggerCircle.setIntrinsicWidth(60); 
    biggerCircle.setBounds(new Rect(0, 0, 60, 60)); 
    biggerCircle.getPaint().setColor(Color.BLUE); 

    ShapeDrawable smallerCircle= new ShapeDrawable(new OvalShape()); 
    smallerCircle.setIntrinsicHeight(10); 
    smallerCircle.setIntrinsicWidth(10); 
    smallerCircle.setBounds(new Rect(0, 0, 10, 10)); 
    smallerCircle.getPaint().setColor(Color.BLACK); 
    smallerCircle.setPadding(50,50,50,50); 

    LayerDrawable composite1 = new LayerDrawable(new Drawable[] biggerCircle,smallerCircle,}); 

Nhưng đó didnt làm việc, những gì xảy ra là các vòng tròn nhỏ hơn có được lớn như vòng tròn lớn hơn. Vì vậy, điều duy nhất hiển thị là vòng tròn màu đen với kích thước của vòng tròn lớn hơn. Tôi xin lỗi nếu ai đó có thể giúp đỡ. Cảm ơn trước.

Trả lời

18

Thay đổi thứ tự,

Drawable[] d = {smallerCircle,biggerCircle}; 

LayerDrawable composite1 = new LayerDrawable(d); 

thử như thế này

 ShapeDrawable biggerCircle= new ShapeDrawable(new OvalShape()); 
     biggerCircle.setIntrinsicHeight(60); 
     biggerCircle.setIntrinsicWidth(60); 
     biggerCircle.setBounds(new Rect(0, 0, 60, 60)); 
     biggerCircle.getPaint().setColor(Color.BLUE); 

     ShapeDrawable smallerCircle= new ShapeDrawable(new OvalShape()); 
     smallerCircle.setIntrinsicHeight(10); 
     smallerCircle.setIntrinsicWidth(10); 
     smallerCircle.setBounds(new Rect(0, 0, 10, 10)); 
     smallerCircle.getPaint().setColor(Color.BLACK); 
     smallerCircle.setPadding(50,50,50,50); 
     Drawable[] d = {smallerCircle,biggerCircle}; 

     LayerDrawable composite1 = new LayerDrawable(d); 

     btn.setBackgroundDrawable(composite1); 

enter image description here

+0

Cảm ơn cho câu trả lời, nhưng tôi tryed và nó vẫn xảy ra điều tương tự. – Alan

+0

Nhìn vào sửa đổi xin, tôi đã thử nó hoạt động cho tôi – Talha

+0

Đối với những người khác mà sẽ đọc này chỉ cần trả attetion rằng các vòng tròn nhỏ hơn là actualy vòng tròn lớn hơn. Tôi đã sử dụng setPadding để vòng tròn smaler và nên được cách khác xung quanh. Cảm ơn rất nhiều vì câu trả lời. – Alan

Các vấn đề liên quan