【问题标题】:Android, overlapping imageViewsAndroid,重叠的图像视图
【发布时间】:2014-05-27 05:25:08
【问题描述】:

我正在以编程方式创建 ImageViews 并将它们拖动到相同的布局中。

我想禁用此ImageView 的重叠。当我以编程方式或其他方式创建每个 ImageView 时,是否有要应用的设置来禁用重叠?

public class MainActivity extends ActionBarActivity {

    private int offset_x = 0;
    private int offset_y = 0;
    RelativeLayout canvas;
    LinearLayout buttonslayout;
    boolean isselected = false;
    View selected_item = null;
    int test1,test2;
    Button save,newdoor,newwindow,rotate;
    TextView xcoord,ycoord;
    //static final int SNAP_GRID_INTERVAL = 43;
    static final int SNAP_GRID_INTERVAL = 44;
    static final int SNAP_GRID_INTERVAL2 = 25;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        canvas = (RelativeLayout) findViewById(R.id.container2);
        ViewGroup vg = (ViewGroup)findViewById(R.id.container3);
        //buttonslayout = (LinearLayout) findViewById(R.id.container3);

        save = (Button) findViewById(R.id.savebtn);
        newdoor = (Button) findViewById(R.id.btnnewdoor);
        newwindow = (Button) findViewById(R.id.btnnewwindow);
        xcoord = (TextView) findViewById(R.id.xcoord);
        ycoord = (TextView) findViewById(R.id.ycoord);
        rotate = (Button) findViewById(R.id.rotate);

        newwindow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int lungime = 66;
                final int inaltime = 20;
                final int []lastcoords = new int[2];
                final int canvasWidth = canvas.getWidth();
                final int canvasHeight = canvas.getHeight();
                RelativeLayout mylayout = (RelativeLayout) findViewById(R.id.container2);
                final View canvas = ((View) v.getParent());
                final int []coordwall = new int[2];
                mylayout.getLocationInWindow(coordwall);
                final ImageView image = new ImageView(getBaseContext());
                image.setBackgroundResource(R.drawable.element_window);
                mylayout.addView(image);
                image.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                         switch(event.getAction())
                            {
                                    case MotionEvent.ACTION_DOWN:
                                            offset_x = (int)event.getX();
                                            offset_y = (int)event.getY();
                                            selected_item = v;
                                            isselected = true;
                                            xcoord.setText("X: "+lastcoords[0]);
                                            ycoord.setText("Y: "+lastcoords[1]);
                                            Log.i("test","Isselected = "+isselected);
                                            break;

                                    default:
                                            break;
                            }

                            return false;
                    }
                });

                rotate.setOnClickListener(new View.OnClickListener() {

                    int grad=0;
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        if(grad!=1)
                        {
                            image.setRotation(90);
                            grad=1;
                        }
                        else
                        {
                            image.setRotation(180);
                            grad=0;
                        }

                    }
                });

                mylayout.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                        MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
                        switch(event.getAction())
                        {
                        case MotionEvent.ACTION_UP:
                            isselected = false;
                            Log.i("test","Isselected = "+isselected);
                            break;

                        case MotionEvent.ACTION_MOVE:
                            if(isselected == true)
                            {
                            View canvas = ((View) v.getParent());
                            final int width = v.getWidth() / 2, height = v.getHeight() / 2;


                            int x = (int)event.getX() - offset_x;
                            int y = (int)event.getY() - offset_y;

                            int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
                            int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
                            int rawX = (int) event.getRawX(), rawY = (int) event.getRawY();
                            int x_coord = (int) event.getRawX() - coordwall[0] - width;
                            int y_coord = (int) event.getRawY() - coordwall[1] - height;

                            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                                        new ViewGroup.MarginLayoutParams(
                                                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                        RelativeLayout.LayoutParams.WRAP_CONTENT));

                            if (rawY < coordwall[1] + inaltime / 2)
                                lp.topMargin = 0;
                            else
                                if (rawY >= coordwall[1] + canvasHeight - inaltime )
                                    lp.topMargin = canvasHeight - inaltime*2;
                                else
                                    lp.topMargin = y   / SNAP_GRID_INTERVAL  * SNAP_GRID_INTERVAL;

                            if(rawX < coordwall[0] + lungime / 2)
                                lp.leftMargin = 0;
                                else
                                    if(rawX >= coordwall[0] + canvasWidth - lungime)
                                    {
                                        lp.leftMargin = canvasWidth - lungime*2;
                                        Log.i("arry","leftmargin = " + lp.leftMargin);
                                    }
                                    else
                                        lp.leftMargin = x  / SNAP_GRID_INTERVAL  * SNAP_GRID_INTERVAL;
                            lastcoords[0] = lp.leftMargin;
                            lastcoords[1] = lp.topMargin;
                            xcoord.setText("X: "+lp.leftMargin);
                            ycoord.setText("Y: "+lp.topMargin);
                             lp.setMargins(lp.leftMargin, lp.topMargin, 0, 0);


                                                    selected_item.setLayoutParams(lp);
                            }
                                                break;

                                        default:
                                                break;
                        }
                        return true;
                }
                });

            }
        });

还有xml:

    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/container"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#000000"
       android:orientation="horizontal"
       tools:context="com.example.draganddrop.MainActivity"
       tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:id="@+id/container3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#848484"
        android:orientation="vertical" >

        <Button
            android:id="@+id/savebtn"
            style="@drawable/new_wall"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/new_wall"
            android:textSize="13sp" />

        <Button
            android:id="@+id/btnnewwindow"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/new_window"
            android:textSize="13sp" />

        <Button
            android:id="@+id/btnnewdoor"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/new_door"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/xcoord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="X: " />

        <TextView
            android:id="@+id/ycoord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Y: " />

        <Button
            android:id="@+id/rotate"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rotate" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/container2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF" >

    </RelativeLayout>

</LinearLayout>

【问题讨论】:

    标签: android drag-and-drop draggable overlap


    【解决方案1】:

    不清楚你到底想做什么,但如果我以正确的方式理解你,你想以编程方式将一些 ImageViews 设置为布局吗?为什么您的观点重叠?我认为您必须为您的视图容器设置布局参数:

        //after onCreate in the activity, create the LinearLayout (for example)
        //and set the LayoutParams, if You have no one declared in Your xml
    
        LinearLayout linearLayout= new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
    
        linearLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
    
        //then create the imageView, set the image inside and add it to the container
    
        ImageView imageView = new ImageView(this);        
        imageView.setImageResource(R.drawable.your_image);
        imageView.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT));
    
    
        linearLayout.addView(imageView);
    
        //set all as content
        setContentView(linearLayout);
    

    你有没有这样做过?如果没有,试试吧,如果你想设置多个 imageViews 或其他一些布局,你必须用 LayoutParams 玩一点。

    【讨论】:

    • 我以编程方式创建图像视图并将它们拖到屏幕上,我不想重叠 2 个图像视图。
    • 您的意思是当您将拖动的视图放在与其他视图相同的位置时?
    • 是的,没错!我使用相对布局,如果需要我发布我的代码,我有一个按下按钮并使用他自己的触摸监听器以编程方式创建一个 imageView,并使用 motionEvent 我拖动 imageView,但我不希望 2 个 imageView 重叠。
    • 我已经发布了。我有这样的xml:一个线性布局,保留2个布局,一个相对+一个线性布局,在线性布局中我有一个按钮,例如newwindow(它创建一个窗口对象( imageview )) 我在 RelativeLayout 中拖动它(我想创建一个房屋计划生成器,我不希望窗户/门重叠
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多