0

我基本上是在尝试实现拖放功能..

我正在尝试的是我在屏幕上提供图像序列,如果我单击images将添加到Mains screen. 但是我遇到的问题是,当我将新视图添加到其中时Main Screen,其他所有视图views也都移到了左上角。

你能告诉我是什么问题吗...?或者请给我一个教程或链接,我可以在其中找到解决方案....或如何实现这一点?

我正在使用Framelayout,所以我也实现了图像重叠......

在此处输入图像描述

这是所有代码都在其中工作的类:

public class drag extends Activity implements OnClickListener, OnTouchListener
{
ImageView img1;
Button btn,btn2;
FrameLayout layout;
LayoutParams params;
ImageView im , im2, im3 ,im4;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout = (FrameLayout) findViewById(R.id.vg);
    layout.setDrawingCacheEnabled(true);
    im  = (ImageView)findViewById(R.id.img1);
    im.setDrawingCacheEnabled(true);
    im.setOnTouchListener(this);
    im.setOnClickListener(this);

    btn  = (Button)findViewById(R.id.btn1);
    btn.setDrawingCacheEnabled(true);
    btn.setOnClickListener(this);
    btn.setOnTouchListener(this);

    btn2  = (Button)findViewById(R.id.btn2);
    btn2.setDrawingCacheEnabled(true);
    btn2.setOnClickListener(this);
    btn2.setOnTouchListener(this);

    params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

    btn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            im2 = new ImageView(drag.this);
            Bitmap bm  = BitmapFactory.decodeResource(getResources(), R.drawable.image);
            im2.setImageBitmap(bm);
            im2.setOnTouchListener(drag.this);
            im2.setOnClickListener(drag.this);
            layout.addView(im2, params);

        }
    });

    btn2.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            Bitmap bm  = BitmapFactory.decodeResource(getResources(), R.drawable.image);                
            saveImage(bm);

        }
    });
}

public  void saveImage(Bitmap myBitmap) 
{
    MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, "mmsImage" , "mmsimage");       
}

int l, t, r, b;
int oldLeft, oldTop;
PointF p, curr;

@Override
public boolean onTouch(View view, MotionEvent me) 
{

    if (me.getAction() == MotionEvent.ACTION_DOWN) 
    {           
        //status = START_DRAGGING;
        Log.i("status"," AAA dOWN");
        img1 = new ImageView(this);
        view.setDrawingCacheEnabled(true);
        Bitmap mmsImage = Bitmap.createBitmap(view.getDrawingCache());

        img1.setImageBitmap(mmsImage);
        img1.setOnTouchListener(drag.this);
        img1.setOnClickListener(drag.this);

        oldLeft = (int)view.getLeft();
        oldTop = (int)view.getTop();

        p = new PointF(me.getRawX(), me.getRawY());


    }
    if (me.getAction() == MotionEvent.ACTION_MOVE) 
    {
        Log.i("status"," AAA draging");

        int xDiff = (int)(me.getRawX() - p.x);
        int yDiff  = (int)(me.getRawY() - p.y);

        p.x = me.getRawX();
        p.y = me.getRawY();

        l = view.getLeft();
        t = view.getTop();
        r = view.getRight();
        b = view.getBottom();

        view.layout(l + xDiff, t + yDiff , r + xDiff, b + yDiff);

    }
    if (me.getAction() == MotionEvent.ACTION_UP) 
    {
        Log.i("status"," AAA UP");
        //captureUserMove(view);
    } 

    return false;
}

}

这是 XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white" >

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

</FrameLayout>
4

2 回答 2

2

您设置params,但未指定如何定位添加的视图。尝试这个:

onCreate()

params = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);

params.gravity = Gravity.LEFT | Gravity.TOP; // you need this for margins to work

在点击监听器中:

// your x and y where you want the new view    
params.leftMargin = x;
params.topMargin = y;
于 2013-02-02T19:09:23.300 回答
0

推杆

params = new LayoutParams(LayoutParams.WRAP_CONTENT,
  LayoutParams.WRAP_CONTENT);

里面

@Override
    public void onClick(View v) 
    {
}

将工作。

于 2013-09-25T11:59:39.877 回答