4

I create A shape for Text View Background

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#800e1520"
    android:endColor="#801e252f"
    android:angle="45"/>
<padding android:left="7dp"
    android:top="7dp"
    android:right="7dp"
    android:bottom="7dp" />
<corners android:radius="8dp" />

and my textview is :

 <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/rel1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:background="@drawable/rounded_corners"
        android:gravity="right"
        android:lineSpacingExtra="6dp"
        android:supportsRtl="true"
        android:text="@string/hello_world"
        android:textColor="#FFFFFF" />

when text is short like this TextBoxImage

but when text is too large not showing background and eclipse logcat show

Shape round rect too large to be rendered into a texture (424x5884, max=2048x2048)

how to solve it? thank you

Mostafa Rostami
  • 1,906
  • 20
  • 29

3 Answers3

5

Edit: The easiest solution is to get rid of the rounded corners. If you remove the rounded corners and use a simple rectangle, the hardware renderer will no longer create a single large texture for the background layer, and won't run into the texture size limit any more.


One simple workaround should be to revert to software rendering for that view:

View view = findViewById(R.id.textView1);
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

...but we've run into a similar problem here and we got the same result you did, the view (and its children) didn't render.

You can also set the layer type of a view from XML:

<TextView android:layerType="software" />

Setting the layerType to "none" instead of software seems to cause the view to draw, but it drew without the rounded corners in a quick test we just tried.

Another approach might be to use a different method of rendering the rounded rectangle, e.g.

  • clipping and drawing the path yourself in onDraw
  • using a PaintDrawable (which supports rounded corners, but must be set from code)
  • breaking the rectangle into three slices -- a top (with rounded corners), middle (just a solid color), and bottom (with rounded corners)
Lorne Laliberte
  • 6,261
  • 2
  • 35
  • 36
  • thanks for yr reply but not work and clear everything and minapi level are 11 but you can explain how do work this with ondraw with an example sorry for my bad english – Mostafa Rostami Jan 29 '13 at 09:09
  • Yes the minimum API level for that call is 11, but the hardware acceleration in question didn't exist before that so it isn't needed on older versions. The XML approach would work better if you are also targeting older versions. However, it seems this workaround may not actually solve the problem. I would recommend the "slices" approach instead: building the background out of three views, so the middle part can be a plain rectangle, in order to avoid the texture limit. – Lorne Laliberte Jan 29 '13 at 23:40
  • @MostafaRostami I've updated my answer to point out that you could also solve the problem by removing the rounded corners. – Lorne Laliberte Feb 04 '13 at 23:16
  • Yes Thats my last option.And I remove The rounded corners and use a simple Rectangle.Thanks For ur reply i accept ur answer and thanks – Mostafa Rostami Feb 05 '13 at 17:13
  • custom onDraw is the better method, as drawing to the canvas lets the system handle how to do this efficiently – Chris.Jenkins Nov 04 '13 at 19:00
2

My Solution is to draw onto the canvas. See below.

If you need to do gradents etc then look at Shader's, https://developer.android.com/reference/android/graphics/LinearGradient.html Should do what you need it too.

/**
 * Created by chris on 04/11/2013
 */
public class WidgetLinearLayout extends LinearLayout {

//Dither and smooth :)
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
private final RectF mBound = new RectF();
private final float radius;

public WidgetLinearLayout(Context context) {
    this(context, null);
}

public WidgetLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    this(context, attrs);
}

public WidgetLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundDrawable(null);
    mPaint.setColor(getResources().getColor(R.color.white));
    mPaint.setStyle(Paint.Style.FILL);
    radius = getResources().getDimension(R.dimen.widget_corner_radius);
    setWillNotDraw(false);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    mBound.set(l, t, r, b);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRoundRect(mBound, radius, radius, mPaint);
}
}
Chris.Jenkins
  • 13,051
  • 4
  • 60
  • 61
0

You could also try to make your background .9.png

Bolton
  • 2,226
  • 3
  • 25
  • 29
  • @MostafaRostami What's the problem when you use .9.png. I've encountered the same problem when I have a textview that's 5 times as high as the screen, and when I use .9.png all comes OK. – Bolton May 21 '13 at 06:51