I have a problem and I'm not sure it's my problem or bug in the
framework. I have a screen that's filled with couple of layouts that
have some ShapeDrawable set as background (and I know, I should use
nine-patch images, this is just a test for something different). When
I try to dynamically add more layouts, my shape drawable code goes
nuts and draws borders all around the place. Here is the code, the bug
manifests mostly when the entire screen is filled with items:

DrawTest.java:
-----------------------------------------

package com.test.DrawTest;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DrawTest extends Activity {
        private int _counter = 1001;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View title = findViewById(R.id.titleContainer);
        title.setBackgroundDrawable( new ShapeDrawable( new SimpleRect
( Color.DKGRAY, Color.WHITE ) ) );

        Button b = (Button)findViewById(R.id.addButton);
        b.setOnClickListener( new OnClickListener() {
                        @Override public void onClick(View v) {
                                DrawTest.this.AddNewTask();
                        }
        });
    }

    protected void AddNewTask() {
        ViewGroup content = (ViewGroup)findViewById(R.id.content);

        LinearLayout item = (LinearLayout)LayoutInflater.from
(this).inflate(R.layout.item, content, false);
        item.setBackgroundDrawable( new ShapeDrawable( new SimpleRect
( Color.rgb(0, 96, 0), Color.WHITE ) ) );
        TextView txt = (TextView)item.findViewById(R.id.item_title);
        txt.setText( "Test " + _counter );
                content.addView( item );
                _counter++;
        }

        public static class SimpleRect extends Shape {
        private Paint _fillPaint, _borderPaint;

        public SimpleRect( int fillColor, int borderColor ) {
                _fillPaint = new Paint();
                _fillPaint.setColor( fillColor );
                _fillPaint.setStyle(Paint.Style.FILL);

                _borderPaint = new Paint();
                _borderPaint.setColor(borderColor);
                _borderPaint.setStyle(Paint.Style.STROKE);
                _borderPaint.setStrokeWidth(3);
        }

                @Override
                public void draw(Canvas canvas, Paint paint) {
                        RectF rc = new RectF( canvas.getClipBounds() );
                        rc.inset(2, 2);
                        Log.v("Draw", rc.toString());
                        canvas.drawRoundRect(rc, 4, 4, _fillPaint);
                        canvas.drawRoundRect(rc, 4, 4, _borderPaint);
                }
    }
}

---------------------

main.xml:
---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/titleContainer"
        android:padding="10dp"
        android:orientation="horizontal">

        <TextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:paddingRight="6dp"
             android:text="Hello, world"
             android:id="@+id/item_title" />

        <EditText
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:id="@+id/title_edit"/>
    </LinearLayout>

    <ScrollView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <LinearLayout
                android:orientation="vertical"
                android:id="@+id/content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

   </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp"
        android:paddingRight="30dp">

        <Button
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:id="@+id/addButton"
             android:text="Add content"
             android:textSize="14sp"
             android:padding="10dp" />
   </LinearLayout>
</LinearLayout>
------------------------------

item.xml:
------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_container"
    android:layout_margin="8dp">
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/item_title"
        android:padding="10dp"
        android:textSize="14sp" />
</LinearLayout>
-----------------------------

Interesting thing is - when the EditText is commented out, everything
seems to works fine. Also, just looking at the LogCat output it's
seems that drawable redraw is called every time when the cursor in the
EditText blinks.

Any ideas?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to