Android Drag and drop images on the Screen?

后端 未结 5 1491
太阳男子
太阳男子 2020-11-27 19:36

I m working on project user to move the image in one position to Another position on the screen. I have written a sample code to move the image but the problem here is if I

5条回答
  •  醉梦人生
    2020-11-27 19:51

    I took the liberty of alternating your code to manage multiple imageviews in a RelativeLayout and random places. Also I added a better way of getting the window size, since Display.getHeight() is deprecated.

        if(Build.VERSION.SDK_INT >= 13){
            android.graphics.Point p = new android.graphics.Point();
            this.getWindowManager().getDefaultDisplay().getSize(p); 
            width = p.x;
            height = p.y;
        }
        else{
            Display display = getWindowManager().getDefaultDisplay();
            width = display.getWidth();
            height = display.getHeight();
        }
    
        RelativeLayout rel = new RelativeLayout(this);
        rel.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
        rel.setBackgroundResource(R.drawable.bg);
    
        pic = new ImageView[10];
        layoutParams1 = new LayoutParams[10];
    
        for(int i = 0; i < 10; i++){
            pic[i] = new ImageView(this);
            pic[i].setImageResource(R.drawable.img);  
            pic[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
            pic[i].setAdjustViewBounds(true);  
            pic[i].setScaleType(ScaleType.FIT_XY);  
            pic[i].setMaxHeight(88);  
            pic[i].setMaxWidth(100);
            pic[i].setMinimumHeight(88);
            pic[i].setMinimumWidth(100);
            pic[i].setTag(i);
            int x = rand.nextInt(width);
            while(x > width - 88){
                x = rand.nextInt(width);
            }
            int y = rand.nextInt(height);
            while(y > height - 100){
                y = rand.nextInt(height);
            }
            layoutParams1[i] = (RelativeLayout.LayoutParams) pic[i].getLayoutParams();
            layoutParams1[i].leftMargin = x;
            layoutParams1[i].topMargin = y;
            pic[i].setLayoutParams(layoutParams1[i]);
            pic[i].setOnTouchListener(new View.OnTouchListener() {         
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int index = Integer.valueOf(v.getTag().toString());
                    layoutParams1[index] = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    switch(event.getActionMasked())
                    {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int) event.getRawX();
                            int y_cord = (int) event.getRawY();
                            if (x_cord > width) {
                                x_cord = width;
                            }
                            if (y_cord > height) {
                                y_cord = height;
                            }
                            layoutParams1[index].leftMargin = x_cord - 44;
                            layoutParams1[index].topMargin = y_cord - 50;
                            pic[index].setLayoutParams(layoutParams1[index]);
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            });
    
            rel.addView(pic[i]);
        }
    
    
        setContentView(rel);
    

提交回复
热议问题