用一条线加入两个视图

时间:2014-03-25 11:41:41

标签: android canvas graph

我想要显示的所有元素(文本和图像视图),我现在需要做的就是显示一行连接我的一些视图(我试图生成连接图形外观) 。没有使用画布,有没有其他方法可以做到这一点?我的另一个疑问是,如果我使用canvas进行自定义视图,我可以在同一活动中使用其他Image / Textview和canvas吗?

我是Android的新手,为什么会感到困惑,请告诉我一些进入的方向。

3 个答案:

答案 0 :(得分:1)

我和CustomView做了同样的事。看看:

Connectors

CustomView类源代码:

public class CustomView extends View
{
    Paint            paint     = new Paint();
    float            startingX, startingY, endingX, endingY;

    ArrayList<Lines> line_list = new ArrayList<Lines>();     ;

    boolean flag = false;
    boolean flag1 = false;
    public class Lines
    {
        float startingX, startingY, endingX, endingY;
    }
    Context context;
    public CustomView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.context=context;
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public void setNumberOfLine(int x )
    {
        line_list = new ArrayList<Lines>(); 
        int y = getHeight();
        int z = getWidth();
        System.out.println("length  => "+x);
        System.out.println("height  => "+y);
        System.out.println("width   => "+z);

        for (int i = 0; i < x; i++)
        {
            Lines l = new Lines();
            l.startingX = z / 2;
            l.startingY = 0;
            l.endingX = i*(z/x)+(z/(x*2));
            l.endingY = y;
            System.out.println("l.endingX   => " + l.endingX);
            System.out.println("l.endingY   => " + l.endingY);
            line_list.add(l);
        }
        flag = true;
        flag1 = false;
        invalidate();
    }

    @SuppressWarnings("deprecation")
    public void setNumberOfLine1(int x , int position , int total , float getX)
    {
        line_list = new ArrayList<Lines>(); 
        int y = getHeight();
//      Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
//      int z = display.getWidth();
        int z;

        float start=0;
//      
        if(x<5)
        {
            Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
            z = display.getWidth();
            start = position*(z/total)+(z/(total*2));
        }
        else
        {
            z = getWidth();
            start = getX;
        }

        System.out.println("length  => "+x);
        System.out.println("height  => "+y);
        System.out.println("width   => "+z);

        for (int i = 0; i < x; i++)
        {
            Lines l = new Lines();
            if(x==1)
            {
                l.startingX = start;
                l.startingY = 0;
                l.endingX = 2*(z/total)+(z/(total*2));
                l.endingY = y;
            }
            else if(x==2)
            {
                if(i==0)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 1*(z/total)+(z/(total*2));
                    l.endingY = y;
                }
                else if(i==1)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 3*(z/total)+(z/(total*2));
                    l.endingY = y;
                }

            }
            else if(x==3)
            {
                if(i==0)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 2*(z/total)+(z/(total*2));
                    l.endingY = y;
                }
                else if(i==2)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 3*(z/total)+(z/(total*2));
                    l.endingY = y;
                }
                else if(i==3)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 4*(z/total)+(z/(total*2));
                    l.endingY = y;
                }

            }
            else if(x==4)
            {
                if(i==0)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 1*(z/total)+(z/(total*2));
                    l.endingY = y;
                }
                else if(i==2)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 2*(z/total)+(z/(total*2));
                    l.endingY = y;
                }
                else if(i==3)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 3*(z/total)+(z/(total*2));
                    l.endingY = y;
                }
                else if(i==3)
                {
                    l.startingX = start;
                    l.startingY = 0;
                    l.endingX = 5*(z/total)+(z/(total*2));
                    l.endingY = y;
                }

            }
            else 
            {
//              l.startingX = start;
//              l.startingY = 0;
//              l.endingX = i*(z/total)+(z/(total*2));
//              l.endingY = y;
                l.startingX = start;
                l.startingY = 0;
                l.endingX = i*(z/x)+(z/(x*2));
                l.endingY = y;
            }
            line_list.add(l);   
            System.out.println("l.endingX   1=> " + l.endingX);
            System.out.println("l.endingY   1=> " + l.endingY);
        }
        flag1 = true;
        flag = false;
        invalidate();
    }

    public CustomView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
        paint.setColor(getResources().getColor(R.color.purple_dark));
        paint.setStrokeWidth(Utility.dpToPx(5, context));
    }
}

答案 1 :(得分:0)

您可以使用它来创建空白视图。为它添加宽度和其他属性,使其可以作为一条线显示。

<View android:layout_width="1dp"
    android:layout_height="wrap_content"/>

答案 2 :(得分:0)

使用此:

<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</LinearLayout>
 <View
    android:layout_width="1dp"
    android:layout_height="wrap_content" />
<LinearLayout
    android:id="@+id/layout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</LinearLayout>

查看标记分隔了两个布局,如小行