2015-12-08 3 views
0

Я пытаюсь создать пользовательский вид, в основном заполненный круг, который имеет ограниченный квадрат в нем, который обтекает какой-то текст. Итак, я создал CircleRelativeLayout, в котором находится CircleHolderRelativeLayout, который содержит текст. CircleHolderRelativeLayout имеет ширину (parent_width/2) * sqrt (2) - для границ.Рисование квадрата, связанного кругом

public class CircleRelativeLayout extends RelativeLayout { 

private int circleColor = Color.TRANSPARENT; 
private Paint drawPaint; 

private int mSize; 

public CircleRelativeLayout(Context context) { 
    super(context); 
} 

public CircleRelativeLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs, 0); 
} 

public CircleRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(context, attrs, 0); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public CircleRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    init(context, attrs, 0); 
} 

private void setupPaint() { 
    drawPaint = new Paint(); 
    drawPaint.setColor(circleColor); 
    drawPaint.setAntiAlias(true); 
    drawPaint.setStyle(Paint.Style.FILL); 
} 

private void init(Context context, AttributeSet attrs, int defStyle) { 
    TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleView, 0, 0); 
    circleColor = ta.getColor(R.styleable.CircleView_circleColor, 0); 
    ta.recycle(); 
    setupPaint(); 
} 

@Override 
protected void dispatchDraw(Canvas canvas) { 
    canvas.drawCircle(Math.min(this.getWidth(), this.getHeight())/2, Math.min(this.getWidth(), this.getHeight())/2, Math.min(this.getWidth(), this.getHeight())/2, drawPaint); 
} 

public void setCircleColor(int newColor){ 
    //update the instance variable 
    circleColor=newColor; 
    //redraw the view 
    invalidate(); 
    requestLayout(); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int originalWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int originalHeight = MeasureSpec.getSize(heightMeasureSpec); 
    int required = Math.min(originalWidth, originalHeight); 
    super.onMeasure(
      MeasureSpec.makeMeasureSpec(required, MeasureSpec.EXACTLY), 
      MeasureSpec.makeMeasureSpec(required, MeasureSpec.EXACTLY)); 
} 
} 

и

public class CircleHolderRelativeLayout extends RelativeLayout { 
public CircleHolderRelativeLayout(Context context) { 
    super(context); 
} 

public CircleHolderRelativeLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CircleHolderRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public CircleHolderRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int size = (int)((Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec))/2)*(Math.sqrt(2))); 

    super.onMeasure(MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY)); 
} 
    } 

и XML является

<ro.cloud.onebox.ui.layout.CircleRelativeLayout 
    android:id="@+id/circleView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    custom:circleColor="@color/status_new" 
    android:layout_margin="@dimen/main_margin"> 
    <ro.cloud.onebox.ui.layout.CircleHolderRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerInParent="true" 
     android:background="@color/colorAccent"> 
     <ro.cloud.onebox.ui.text.CustomTextViewNormal 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textColor="@color/primary_text_color_dark" 
      android:gravity="center" 
      android:layout_centerInParent="true" 
      android:text="1asdasdasasdasdasdd2"/> 
    </ro.cloud.onebox.ui.layout.CircleHolderRelativeLayout> 
</ro.cloud.onebox.ui.layout.CircleRelativeLayout> 

Вопрос заключается в том, что круг обращается за макетах детей. Следовательно, я не вижу текст. Если я поменю краску на STROKE, я ясно вижу, что текст есть. Помощь :)

+0

вы можете использовать этот LIB для рисования https://github.com/telly/MrVector –

+0

не решение, я хочу продолжить у.е. настроить мой взгляд – TibiG

ответ

1

Изменить код

@Override 
protected void dispatchDraw(Canvas canvas) { 
    canvas.drawCircle(Math.min(this.getWidth(), this.getHeight())/2, Math.min(this.getWidth(), this.getHeight())/2, Math.min(this.getWidth(), this.getHeight())/2, drawPaint); 
} 

в

@Override 
protected void dispatchDraw(Canvas canvas) { 
    canvas.drawCircle(Math.min(this.getWidth(), this.getHeight())/2, Math.min(this.getWidth(), this.getHeight())/2, Math.min(this.getWidth(), this.getHeight())/2, drawPaint); 
    for(int i = 0; i<getChildCount(); i++) { 
     getChildAt(i).draw(canvas); 
    } 
} 

Это покажет ваш текст.

+0

это решает рисунок на верхнем номере :) спасибо – TibiG

+0

приветствую :) – Tamal

0

Использование FrameLayout в качестве контейнера

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ro.cloud.onebox.ui.layout.CircleRelativeLayout 
     android:id="@+id/circleView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     custom:circleColor="@color/status_new" 
     android:layout_margin="@dimen/main_margin"> 

    </ro.cloud.onebox.ui.layout.CircleRelativeLayout> 
    <ro.cloud.onebox.ui.layout.CircleHolderRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_centerInParent="true" 
     android:background="@color/colorAccent"> 

    </ro.cloud.onebox.ui.layout.CircleHolderRelativeLayout> 

    <ro.cloud.onebox.ui.text.CustomTextViewNormal 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textColor="@color/primary_text_color_dark" 
     android:gravity="center" 
     android:layout_centerInParent="true" 
     android:text="1asdasdasasdasdasdd2"/> 
</FrameLayout> 
+0

это так, как я могу не пользовательский контент обертки для управления размером окружности , Предположим, что вокруг текста – TibiG