如何在android中的两个图像按钮之间画一条线?

时间:2014-05-29 04:46:46

标签: java android

我正在尝试在屏幕上的2个ImageButtons之间创建一条线。我将按钮放在屏幕上的xml文件中,我试图编写一种方法来绘制一组线,当我使用不同的屏幕尺寸时,这些线将正确缩放。当我尝试使用getX()时,它声明返回的值都是0.我是否有某些方法可以获得x和y值放置在xml中的按钮的位置?我是否必须手动为每种不同的屏幕尺寸设置线路?

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    if (container == null) {

    }
        super.onCreate(savedInstanceState);
        size = 0;
        View v = (RelativeLayout)inflater.inflate(R.layout.minor_fragment_two, container,     false);
        final ImageButton one = (ImageButton)v.findViewById(R.id.one);
        final ImageButton two = (ImageButton)v.findViewById(R.id.two);
        final ImageButton three = (ImageButton)v.findViewById(R.id.three);
        final ImageButton four = (ImageButton)v.findViewById(R.id.four);
        final ImageButton five = (ImageButton)v.findViewById(R.id.five);
        final ImageButton six = (ImageButton)v.findViewById(R.id.six);
        final ImageButton seven = (ImageButton)v.findViewById(R.id.seven);
        final ImageButton eight = (ImageButton)v.findViewById(R.id.eight);
    imageView = (ImageView) v.findViewById(R.id.imView);
        //Display currentDisplay = getWindowManager().getDefaultDisplay();
        Display currentDisplay = ((WindowManager) v.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        float dw = currentDisplay.getWidth();
        float dh = currentDisplay.getHeight();

        bitmap = Bitmap.createBitmap((int) dw, (int) dh,
                Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(10);
        onex = one.getX();
        oney = one.getY();
        twox = two.getX();
        twoy = two.getY();
        Toast.makeText(getActivity(),
                "Position :" + onex + " " + oney + " " + twox + " " + twoy, Toast.LENGTH_LONG)
                .show();
        canvas.drawLine(onex,oney,twox,twoy,paint);
        imageView.setImageBitmap(bitmap);

        //return (LinearLayout)inflater.inflate(R.layout.minor_fragment_two, container, false);
        return v;
    }    


    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/reduced"
    android:weightSum="1">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imView"/>

    <ImageButton
        android:layout_width="41dp"
        android:layout_height="41dp"
        android:id="@+id/one"
        android:background="@drawable/first_alpha"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="170dp"
        android:layout_gravity="center_horizontal" />

    <ImageButton
        android:layout_width="41dp"
        android:layout_height="41dp"
        android:id="@+id/two"
        android:background="@drawable/first_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="120dp" />

    <ImageButton
        android:layout_width="41dp"
        android:layout_height="41dp"
        android:id="@+id/three"
        android:background="@drawable/first_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="210dp"
        android:layout_marginLeft="85dp" />

    <ImageButton
        android:layout_width="41dp"
        android:layout_height="41dp"
        android:id="@+id/four"
        android:background="@drawable/first_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="330dp"
        android:layout_marginLeft="120dp" />

    <ImageButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/five"
        android:background="@drawable/first_alpha"
        android:layout_gravity="right"
        android:layout_marginTop="360dp"
        android:layout_marginLeft="145dp" />

    <ImageButton
        android:layout_width="41dp"
        android:layout_height="41dp"
        android:id="@+id/six"
        android:background="@drawable/first_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="380dp"
        android:layout_marginLeft="50dp" />

    <ImageButton
        android:layout_width="41dp"
        android:layout_height="41dp"
        android:id="@+id/seven"
        android:background="@drawable/first_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="410dp"
        android:layout_marginLeft="190dp" />

    <ImageButton
        android:layout_width="41dp"
        android:layout_height="41dp"
        android:id="@+id/eight"
        android:background="@drawable/first_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="460dp"
        android:layout_marginLeft="135dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您可以通过向xml添加视图来创建一行。

垂直线:

<View
    android:layout_width="1dip"
    android:layout_height="fill_parent"
    android:background="#FF0000FF"
/>

水平线:

<View
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#FF0000FF"
/>

将背景颜色调整为所需的线条颜色。