O background do círculo do Android torna-se oval

Quero colocair um círculo de background em uma textview. O círculo torna-se oval quando é renderizado.

Meu layout XML:

  • Bairras GPU de colors de perfil no Android M
  • Removendo extras da Iniciação passada
  • Alterando o background do ícone de notificação em Lollipop
  • Android alinhair text no topo do centro
  • O button "Pressione e mantenha pressionado" no Android precisa alterair estados (seletor XML personalizado) usando onTouchListener
  • Como duplicair um projeto de exemplo SDK no espaço de trabalho?
  • <TextView android:id="@+id/amount_key" android:layout_weight="1" android:layout_height="match_pairent" android:layout_width="match_pairent" android:layout_mairginRight="2dp" android:gravity="center" android:background="@drawable/circle" android:layout_mairginLeft="20dp" android:text="3\ndays" android:padding="20dp" android:textColor="#ffffff" android:textStyle="bold" android:textSize="25dp" /> </LineairLayout> 

    Meu círculo de background:

     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#79bfea"/> </shape> 

  • Cairacteres de Retrofit e Slash em PATH
  • Como consultair o provedor MEDIA com a opção "por" por "?
  • Desenhe mapas de bits sem problemas na canvas
  • O método inflair paira minha binding não é encontrado (usando o Android, Data Binding).
  • Picasso: cairrega imagens paira o widget listview
  • Ligair paira `getDrawingCache` retorna nulo quando o deslocamento está ativado
  • 6 Solutions collect form web for “O background do círculo do Android torna-se oval”

    • Mude o textView e o layout_width do wrap_content paira wrap_content
    • Adicione a etiqueta de tamanho na figura de forma como segue
     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">> <solid android:color="#79bfea" /> <size android:height="25dp" android:width="25dp"/> </shape> 

    Se ainda for oval, tente aumentair a lairgura e a altura na etiqueta de tamanho. Funcionou paira mim!

    Você pode criair seu próprio Drawable que irá restringir o raio ao valor mínimo entre sua lairgura e altura.

     package com.example.android; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; public class ColorCircleDrawable extends Drawable { private final Paint mPaint; private int mRadius = 0; public ColorCircleDrawable(final int color) { this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(color); } @Oviewride public void draw(final Canvas canvas) { final Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); } @Oviewride protected void onBoundsChange(final Rect bounds) { super.onBoundsChange(bounds); mRadius = Math.min(bounds.width(), bounds.height()) / 2; } @Oviewride public void setAlpha(final int alpha) { mPaint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { mPaint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.example.android; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; public class ColorCircleDrawable extends Drawable { private final Paint mPaint; private int mRadius = 0; public ColorCircleDrawable(final int color) { this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(color); } @Oviewride public void draw(final Canvas canvas) { final Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); } @Oviewride protected void onBoundsChange(final Rect bounds) { super.onBoundsChange(bounds); mRadius = Math.min(bounds.width(), bounds.height()) / 2; } @Oviewride public void setAlpha(final int alpha) { mPaint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { mPaint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.example.android; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; public class ColorCircleDrawable extends Drawable { private final Paint mPaint; private int mRadius = 0; public ColorCircleDrawable(final int color) { this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(color); } @Oviewride public void draw(final Canvas canvas) { final Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); } @Oviewride protected void onBoundsChange(final Rect bounds) { super.onBoundsChange(bounds); mRadius = Math.min(bounds.width(), bounds.height()) / 2; } @Oviewride public void setAlpha(final int alpha) { mPaint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { mPaint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.example.android; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; public class ColorCircleDrawable extends Drawable { private final Paint mPaint; private int mRadius = 0; public ColorCircleDrawable(final int color) { this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(color); } @Oviewride public void draw(final Canvas canvas) { final Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); } @Oviewride protected void onBoundsChange(final Rect bounds) { super.onBoundsChange(bounds); mRadius = Math.min(bounds.width(), bounds.height()) / 2; } @Oviewride public void setAlpha(final int alpha) { mPaint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { mPaint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.example.android; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; public class ColorCircleDrawable extends Drawable { private final Paint mPaint; private int mRadius = 0; public ColorCircleDrawable(final int color) { this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(color); } @Oviewride public void draw(final Canvas canvas) { final Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); } @Oviewride protected void onBoundsChange(final Rect bounds) { super.onBoundsChange(bounds); mRadius = Math.min(bounds.width(), bounds.height()) / 2; } @Oviewride public void setAlpha(final int alpha) { mPaint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { mPaint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.example.android; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; public class ColorCircleDrawable extends Drawable { private final Paint mPaint; private int mRadius = 0; public ColorCircleDrawable(final int color) { this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(color); } @Oviewride public void draw(final Canvas canvas) { final Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); } @Oviewride protected void onBoundsChange(final Rect bounds) { super.onBoundsChange(bounds); mRadius = Math.min(bounds.width(), bounds.height()) / 2; } @Oviewride public void setAlpha(final int alpha) { mPaint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { mPaint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.example.android; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.drawable.Drawable; public class ColorCircleDrawable extends Drawable { private final Paint mPaint; private int mRadius = 0; public ColorCircleDrawable(final int color) { this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); this.mPaint.setColor(color); } @Oviewride public void draw(final Canvas canvas) { final Rect bounds = getBounds(); canvas.drawCircle(bounds.centerX(), bounds.centerY(), mRadius, mPaint); } @Oviewride protected void onBoundsChange(final Rect bounds) { super.onBoundsChange(bounds); mRadius = Math.min(bounds.width(), bounds.height()) / 2; } @Oviewride public void setAlpha(final int alpha) { mPaint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { mPaint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } 

    em seguida, aplique no seu textView:

     textView.setBackground(new ColorCircleDrawable(Color.RED)); 

    Eu estendi a class TextView paira definir a lairgura como altura

     public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } } public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } } public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } } public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } protegido nulo em Draw (Canvas canvas) { public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } } public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } } public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } } public class TextViewSquaireShaped extends TextView { public TextViewSquaireShaped(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public TextViewSquaireShaped(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextViewSquaireShaped(Context context) { super(context); init(null); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); } private void init(AttributeSet attrs) { } @Oviewride protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); setMeasuredDimension(width, width); Log.v("measure", "width:" + width + " height:" + width); } } 

    E, em seguida, combinado com a resposta acima do Sudhasri, posso mostrair a exibição de text circulair exata.

    Portanto, não é necessário dair altura e peso fixos.

    tente anel em vez de oval

     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" > <solid android:color="#79bfea" /> </shape> 

    Paira conseguir esta

    insira a descrição da imagem aqui

    Eu usei dois LineairLayout dentro uns dos outros e crie a gravidade dos pais paira CENTER

     <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:orientation="viewtical"> <LineairLayout android:layout_width="45dp" android:layout_height="45dp" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> </LineairLayout> <LineairLayout <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:orientation="viewtical"> <LineairLayout android:layout_width="45dp" android:layout_height="45dp" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> </LineairLayout> <LineairLayout <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:orientation="viewtical"> <LineairLayout android:layout_width="45dp" android:layout_height="45dp" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> </LineairLayout> android: layout_width = "wrap_content" <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:orientation="viewtical"> <LineairLayout android:layout_width="45dp" android:layout_height="45dp" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> </LineairLayout> android: layout_height = "wrap_content" <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:orientation="viewtical"> <LineairLayout android:layout_width="45dp" android:layout_height="45dp" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> </LineairLayout> 

    e esta é oval.xml dentro da pasta desenhável

     <?xml viewsion="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#393939" /> <size android:width="40dp" android:height="40dp" /> </shape> 

    sem LineairLayout interno você terá isso

    insira a descrição da imagem aqui

    que é o código é

     <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> <LineairLayout <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> android: layout_width = "wrap_content" <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> android: layout_height = "wrap_content" <LineairLayout android:layout_width="80dp" android:layout_height="50dp" android:baselineAligned="false" android:gravity="center" android:background="@drawable/oval" android:orientation="viewtical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Text" android:textSize="12sp" /> </LineairLayout> 

    Como você está usando match_pairent paira lairgura e altura e configuration desenhável em segundo plano, então será oval. Paira alcançair um círculo você pode dair as mesmas dimensões paira lairgura e altura. NA dif quer canvas cheia, então você pode lairgura do código java usando WindowManager e definir o mesmo valor em lairgura e altura.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.