自定义视图仅显示一次

发布于 2024-11-18 06:15:50 字数 2547 浏览 2 评论 0原文

我制作了一个非常简单的自定义视图,一个灰色矩形,矩形内有任意数量的红色标记,并以百分比标记。

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);
}

但是

我不知道如何使用该视图。每次我尝试在线性布局或相对布局中添加多个视图时,我只看到其中一个视图。

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>

布局代码:

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);

}

结果为:

上述线性布局代码的结果..

这是错误的路线吗?我是否错过了多次使用此视图的一些明显的关键?如果这不是正确的路线,是否还有其他方法来制作自定义形状?也许扩展 rectShape?

I've made a very simple customView, a gray rectangle 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:

Result of linearlayout code above..

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2024-11-25 06:15:50

按照 Mibollma 的建议,我观看了上面的视频,这是 Google I/O 2009 上有关加速 UI 的视频。

这些信息在两年后绝对仍然适用。通过使用 ViewHolder,我不仅能够加快所有 ListView 的速度,而且还能够找到问题的答案。

创建自定义视图时,必须重写两个方法,第一个方法如上面列出:onDraw。

缺少的方法? onMeasure()。可以在此处找到更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文