我找到了这段代码,让ImageView
可以移动。
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
//Definition MotionEvent.ACTION_MASK: Bit mask of the parts of the action code that are the action itself.
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
root.invalidate();
break;
现在我想为RelativeLayout
添加此代码。
在这种情况下,我将RelativeLayout
放入另一个RelativeLayout
,例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/test">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root">
<ImageView
android:id="@+id/skizze"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix" />
<ImageView
android:id="@+id/feuerloescherID"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentTop="false"
android:layout_gravity="center_horizontal|top" />
</RelativeLayout>
这是代码,但不是ImageView
,而是RelativeLayout
。
final int rootX = (int) event.getRawX();
final int rootY = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: //first finger down only
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
int left = layoutParams.leftMargin;
int top = layoutParams.topMargin;
_xDelta = rootX - layoutParams.leftMargin;
_yDelta = rootY - layoutParams.topMargin;
break;
case MotionEvent.ACTION_UP://first finger lifted
break;
case MotionEvent.ACTION_POINTER_UP: //second finger lifted
break;
case MotionEvent.ACTION_POINTER_DOWN: //first and second finger down
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams1.leftMargin = rootX - _xDelta;
layoutParams1.topMargin = rootY - _yDelta;
layoutParams1.rightMargin = -250;
layoutParams1.bottomMargin = -250;
view.setLayoutParams(layoutParams1);
break;
}
root.invalidate();
所以我可以再次使用相同的代码。 我的问题是,我无法在屏幕上移动布局。它是#34;停靠&#34;在屏幕的右下角。 如果我移动布局,它就会变得非常紧张。
有什么问题?
答案 0 :(得分:1)
public class Main extends Activity {
private int mCurrentX;
private int mCurrentY;
private View popUpView;
private Button button1;
private TextView textview1;
private PopupWindow popUpWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
popUpView = getLayoutInflater().inflate(R.layout.layout1, null);
button1 = (Button)popUpView.findViewById(R.id.button1);
textview1 = (TextView)popUpView.findViewById(R.id.textview1);
popUpWindow = new PopupWindow(popUpView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popUpWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popUpWindow.setOutsideTouchable(true);
popUpWindow.update();
OnTouchListener otl = new OnTouchListener() {
private float mDx;
private float mDy;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action==MotionEvent.ACTION_DOWN){
mDx = mCurrentX - event.getRawX();
mDy = mCurrentY - event.getRawY();
}else if(action== MotionEvent.ACTION_MOVE){
mCurrentX = (int) (event.getRawX()+mDx);
mCurrentY = (int) (event.getRawY()+mDy);
popUpWindow.update(mCurrentX, mCurrentY,-1 ,-1);
}
return true;
}
};
popUpView.setOnTouchListener(otl);
mCurrentX = 20;
mCurrentY = 50;
popUpView.post(new Runnable() {
@Override
public void run() {
popUpWindow.showAtLocation(popUpView, Gravity.NO_GRAVITY, mCurrentX, mCurrentY);
popUpWindow.update();
}
});
}
}
答案 1 :(得分:1)
是我,这是我的代码
这是移动布局的对象 可以移动此布局中的任何内容。
/**
* Created by DennyShum on 10/7/15.
*/
public class TouchMovingLayout extends RelativeLayout implements View.OnTouchListener{
public static interface OnChildViewClickListener{
public void onChildViewClick(View view);
public void onChildViewLongClick(View view);
}
private OnChildViewClickListener onChildViewClickListener;
private GestureDetector gestureDetector;
private ImageView recyclerIcon;
private View touchedView;
float xOffset = 0;
float yOffset = 0;
public TouchMovingLayout(Context context) {
this(context, null);
}
public TouchMovingLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TouchMovingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
recyclerIcon = new ImageView(getContext());
LayoutParams params = new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.bottomMargin = (int)(getContext().getResources().getDisplayMetrics().density * 24.0f);
recyclerIcon.setLayoutParams(params);
recyclerIcon.setImageResource(R.drawable.ic_delete_white_36dp);
recyclerIcon.setVisibility(INVISIBLE);
addView(recyclerIcon);
gestureDetector = new GestureDetector(getContext(), gestureListener);
gestureDetector.setIsLongpressEnabled(true);
setClickable(true);
setOnTouchListener(this);
}
public void setOnChildViewClickListener(OnChildViewClickListener onChildViewClickListener){
this.onChildViewClickListener = onChildViewClickListener;
}
private boolean shouldDeleteElement(View elementView){
float bottom = elementView.getY() + elementView.getHeight();
float left = elementView.getX();
float startSpot = recyclerIcon.getX() - elementView.getWidth();
float endSpot = recyclerIcon.getX() + recyclerIcon.getWidth();
if (bottom > recyclerIcon.getY() && (left > startSpot && left < endSpot)){
return true;
}else{
return false;
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
touchedView = ViewUtil.findViewByPosition
(this, motionEvent.getX(), motionEvent.getY());
if (touchedView != null){
if (touchedView == recyclerIcon){
touchedView = null;
return false;
}
xOffset = motionEvent.getX() - touchedView.getX();
yOffset = motionEvent.getY() - touchedView.getY();
//touchedView.setBackgroundResource(R.drawable.rectangle_background_white_black_das);
recyclerIcon.setVisibility(View.VISIBLE);
}
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP){
if (touchedView != null){
touchedView.setBackgroundColor(Color.TRANSPARENT);
if (shouldDeleteElement(touchedView)){
removeView(touchedView);
}
touchedView = null;
}
recyclerIcon.setVisibility(View.INVISIBLE);
}
if (touchedView != null){
touchedView.setX(motionEvent.getX() - xOffset);
touchedView.setY(motionEvent.getY() - yOffset);
if (shouldDeleteElement(touchedView)){
//recyclerIcon.setBackgroundResource(R.drawable.rectangle_background_white_black_das);
recyclerIcon.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
}else{
recyclerIcon.setBackgroundColor(Color.TRANSPARENT);
recyclerIcon.getDrawable().clearColorFilter();
}
}
gestureDetector.onTouchEvent(motionEvent);
return false;
}
private GestureDetector.OnGestureListener gestureListener =
new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
if (onChildViewClickListener != null){
onChildViewClickListener.onChildViewClick(
ViewUtil.findViewByPosition(TouchMovingLayout.this,
motionEvent.getX(), motionEvent.getY())
);
}
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
if (onChildViewClickListener != null){
onChildViewClickListener.onChildViewLongClick(
ViewUtil.findViewByPosition(TouchMovingLayout.this,
motionEvent.getX(), motionEvent.getY())
);
}
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
};
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
这是find findViewByPosition实用程序
/**
* Created by DennyShum on 10/7/15.
*/
public class ViewUtil {
public static View findViewByPosition(ViewGroup parentView, float x, float y){
if (parentView == null) return null;
if (parentView.getWidth() == 0 || parentView.getHeight() == 0) return null;
for (int i = parentView.getChildCount() - 1; i > -1 ; i--){
View view = parentView.getChildAt(i);
if (view.getWidth() == 0 || view.getHeight() ==0) continue;
float top = view.getY();
float bottom = top + view.getHeight();
float left = view.getX();
float right = left + view.getWidth();
if ((y >= top && y <= bottom) && (x >= left && x <= right)){
return view;
}
}
return null;
}
}
xml实现代码
<com.pompeiicity.smslibrary.widgets.TouchMovingLayout
android:id="@+id/floatElementHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/someColor"/>
<View
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/someColor"
android:layout_marginTop="90dp"/>
</com.pompeiicity.smslibrary.widgets.TouchMovingLayout>
快乐的编码。