У меня есть изображение и пустой линейный макет, когда я перетаскиваю кнопку линейного макета также переместить.Мне нужно перетащить кнопку на линейный макет, и линейный макет должен иметь фиксированное положение.
Мой Android XML: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollViewRequestDriver"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dip"
android:paddingRight="10dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ballItem"
android:layout_width="50sp"
android:layout_height="50sp"
android:src="@drawable/icon" >
</ImageView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearlayoutItem"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/lvItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#FFFFFF"
android:dividerHeight="2dip" >
</ListView>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnAdd_Click"
android:text="Add New Item" >
</Button>
</LinearLayout>
</ScrollView>
</LinearLayout>
Мой код: -
package com.beanie.example;
import java.util.ArrayList;
import java.util.List;
import com.beanie.example.BusinessObjects.Item;
import com.beanie.example.provider.ItemAdapter;
import com.beanie.example.BusinessManager.ItemManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.OnHierarchyChangeListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
public class Dashboard extends Activity implements OnTouchListener {
int windowwidth;
int windowheight;
private ItemAdapter ia;
Context context=null;
List<Item> items = null;
ProgressDialog signInProgress = null;
public Handler h = new Handler();
private LinearLayout linearlayout1;
private LayoutParams layoutParams ;
private LayoutParams layoutParams1 ;
int x_cord;
int y_cord;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
ImageView ball= (ImageView)findViewById(R.id.ballItem);
ball.setOnTouchListener(this);
linearlayout1= (LinearLayout)findViewById(R.id.linearlayoutItem);
}
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId())
{
case R.id.ballItem:
ImageView ball= (ImageView)findViewById(R.id.ballItem);
layoutParams = (LayoutParams) ball.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
ball.setAlpha(255);
break;
case MotionEvent.ACTION_MOVE:
x_cord = (int)event.getRawX();
y_cord = (int)event.getRawY();
if(x_cord>windowwidth){x_cord=windowwidth;}
if(y_cord>windowheight){y_cord=windowheight;}
x_cord= x_cord -25;
y_cord=y_cord - 230;
layoutParams.leftMargin = x_cord ;
layoutParams.topMargin = y_cord ;
ball.setAlpha(45);
ball.setLayoutParams(layoutParams);
break;
case MotionEvent.ACTION_UP:
layoutParams.leftMargin = x_cord ;
layoutParams.topMargin = y_cord;
ImageView ball1= new ImageView(context);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(5000);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ball1.setImageBitmap(bm);
ball1.setAnimation(animation);
ball1.setLayoutParams(layoutParams);
linearlayout1.addView(ball1);
animation=null;
EditText tv= new EditText(context);
tv.setWidth(200);
tv.setHint("TextBox 1");
linearlayout1.addView(tv);
EditText tv1= new EditText(context);
tv1.setWidth(200);
tv1.setHint("TextBox 2");
//slide from top and bottom
AnimUtils.setLayoutAnim_slidedownfromtop(linearlayout1, context);
layoutParams.leftMargin = 0;
layoutParams.topMargin = 0;
ball.setAlpha(255);
ball.setLayoutParams(layoutParams);
break;
default : break;
}
}
return true;
}
}
Пожалуйста, предложите мне полезную ссылку или модификацию в моем коде.