来自iOS背景,我试图了解如何在我的应用中向RelativeLayout
添加平移手势。
我首先提出了这个问题:
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
RelativeLayout draggableLayout = null;
private int _xDelta;
private int _yDelta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
draggableLayout = findViewById(R.id.draggable_layout);
draggableLayout.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
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;
}
draggableLayout.invalidate();
return true;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main"
android:background="@android:color/holo_blue_dark">
<RelativeLayout
android:id="@+id/draggable_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="100dp"
android:layout_marginStart="24dp"
android:background="@android:color/white" />
</RelativeLayout>
这很好用,但现在我想把这个相对布局放在视图的底部并水平居中,所以我换了:
android:layout_marginTop="100dp"
android:layout_marginStart="24dp"
通过:
android:layout_alignParentBottom="true"
android:layout_marginBottom="32dp"
android:layout_centerHorizontal="true"
使用此规则,布局首先位于屏幕上,但现在我无法再拖动布局了。因此,我尝试在移动规则时以编程方式删除规则:
// Added under case MotionEvent.ACTION_MOVE:
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
虽然拖动视图再次起作用,但删除规则会在第一次拖动时重置视图左上角的布局。
我不知道该怎么做。
答案 0 :(得分:0)
拖放
在Android上拖放是一项简单的任务。您需要拥有的是一个拖动项(将被拖动的视图)和一个放置目标(放下时将接收拖动项的视图) 例如
drop_target.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
int action = dragEvent.getAction();
View viewdrag = (View) dragEvent.getLocalState();
return true;
}
});
ClipData mClipdata = ClipData.newPlainText("","");
drag_item.DragShadowBuilder shadowBuilder = new
View.DragShadowBuilder(drag_item);
drag_item.startDrag(mClipdata,shadowBuilder,drag_item,0);
drag_item
是将被拖入drop_target
视图
捏合和缩放
您需要实施ScaleGestureDetector
private ScaleGestureDetector mScaleDetector;
mScaleDetector = new ScaleGestureDetector(context, new OnScaleGestureListener() {
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
Log.d(LOG_KEY, "zoom ongoing, scale: " + detector.getScaleFactor());
return false;
}
});
在您的触控式监听器上
public boolean onTouchEvent(MotionEvent event) {
mScaleDetector.onTouchEvent(event);
return true;
}
您的content_main应该是这样的
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main"
android:background="@android:color/holo_blue_dark">
<RelativeLayout
android:id="@+id/draggable_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@android:color/white"
android:layout_gravity="bottom|center" />
</FrameLayout>
您不需要外部RelativeLayout