java - 如何在 android 中的两个 ImageButton 之间画一条线?

标签 java android

我正在尝试在屏幕上的 2 个 ImageButtons 之间创建一条线。我将按钮放置在屏幕上的 xml 文件中,并且我正在尝试编写一种方法来绘制一组线条,当我使用不同的屏幕尺寸时,这些线条将正确缩放。当我尝试使用 getX() 时,它指出返回的值都是 0。我是否缺少某种方法来获取我在 xml 中放置按钮的位置的 x 和 y 值?我是否必须为每个不同的屏幕尺寸手动插入线条?

    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>

最佳答案

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

垂直线:

<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"
/>

将背景颜色调整为您想要的线条颜色。

关于java - 如何在 android 中的两个 ImageButton 之间画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925847/

相关文章:

android - 使用 OpenCV4Android,如何从轮廓创建 ROI(感兴趣区域或子垫)?

Android - 是否有可能制作无限 Canvas ?

java - 如何以对未闭合的双引号字符具有鲁棒性的方式解析大型 CSV 文件?

android - Android默认浏览器如何调试查看HTML和CSS源码?

java.lang.IllegalArgumentException : Cannot bind argument at index 0 because the index is out of range. 该语句有4个参数

java - 无法递归地对矩阵中的所有偶数求和

android - 平板电脑上的 Mapview : How can I center the map with an offset?

java - 试图停止线程,但它又开始了

java.io.FileNotFoundException : Could not open ServletContext resource [/resources/properties/database. 属性]

java - 从 OkHttp 到 HttpURLConnection