繁体   English   中英

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

[英]How do I draw a line between two ImageButtons in android?

我正在尝试在屏幕上的2个ImageButton之间创建一条线。 我将按钮放在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
    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"
/>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM