开发者

Custom Views only showing up once

I've made a very simple customView, a gray re开发者_开发知识库ctangle with an arbitrary amount of red markings inside the rectangle marked by percentages.

public class DemoView extends View {
private ShapeDrawable mDrawable;
private ArrayList<ShapeDrawable> mMarks;

public DemoView(Context context, int[] marks) {
    super(context);
    int x = 0;
    int y = 0;
    int width = 100;
    int height = 10;
    // Timeline Initially empty

    mDrawable = new ShapeDrawable(new RectShape());
    mDrawable.getPaint().setColor(Color.GRAY);
    mDrawable.setBounds(x, y, x + width, y + height);
    // Add marks
    if (marks != null && marks.length % 2 == 0) {
        mMarks = new ArrayList<ShapeDrawable>(marks.length / 2);
        ShapeDrawable mark;
        for (int i = 1; i < marks.length; i = i + 2) {
            mark = new ShapeDrawable(new RectShape());
            mark.getPaint().setColor(Color.RED);
            mark.setBounds(x + marks[i - 1], y, x + marks[i], y + height);
            mMarks.add(mark);
        }
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mDrawable.draw(canvas);
    if (mMarks != null)
        for (ShapeDrawable mark : mMarks)
            mark.draw(canvas);
}

}

However I can't figure out how to make use of the view. Each time I try to add more than one of the view in a linearlayout or relativelayout, I only see one of the views.

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"
android:id="@+id/llayout"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
</LinearLayout>

Layout code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LinearLayout ll = (LinearLayout) findViewById(R.id.llayout);
    demoview = new DemoView(this, new int[]{10,15,35,60});
    demoview.setId(ID_NUM++);
    ll.addView(demoview);
    demoview2 = new DemoView(this, new int[]{0,1,3,6});
    demoview2.setId(ID_NUM++);
    ll.addView(demoview2);
    demoview3 = new DemoView(this, new int[]{25,60});
    demoview3.setId(ID_NUM++);
    ll.addView(demoview3);
    demoview4 = new DemoView(this, new int[]{15,60});
    demoview4.setId(ID_NUM++);
    ll.addView(demoview4);

}

Results in:

Custom Views only showing up once

Is this the wrong route to take? Am I missing some obvious key to using this view multiple times? If this is not the correct route is there some other method to making a custom shape? Perhaps extending rectShape?


Following Mibollma's advice, I watched the video above, a video from Google I/O 2009 about speeding up your UI.

The information is most definitely still applicable two years later. Not only was I able to speed up all of my ListViews through the use of ViewHolder, I was able to find the answer to my question.

When creating a custom view, two methods must be overriden, the first is listed above: onDraw.

The missing method? onMeasure(). More information can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜