我能够拖动图像但是当我拖动图像时,我的图像尺寸会不断变化。图像尺寸不固定。请让我知道我错在哪里。当我拖动图像时,它会不断改变特定图像的大小。布局参数有些东西,但我无法修复它。
我的拖拽区域的Xml。
<LinearLayout
android:id="@+id/vg1"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:gravity="center"
android:weightSum="1"
android:layout_alignTop="@+id/scrollView1"
android:layout_toLeftOf="@+id/scrollView2"
android:layout_toRightOf="@+id/scrollView1"
android:gravity="right"
android:background="@drawable/shape" >
<ImageView
android:id="@+id/dragimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:src="@drawable/log_off_button" />
</LinearLayout>
我的拖动图片代码:
OnTouchListener dragt = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(v.getLayoutParams());
v.setLayoutParams(par);
switch(v.getId())
{//What is being touched
case R.id.dragimg:
{//Which action is being taken
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
par.topMargin = (int)event.getRawY() + windowheight - (vg.getHeight()/2);
par.leftMargin = (int) ((int)event.getRawX() +windowwidth - (vg.getWidth()));
v.setLayoutParams(par);
break;
}//inner case MOVE
case MotionEvent.ACTION_UP:
{
par.topMargin = (int)event.getRawY()+ windowheight - (vg.getHeight()/2);
par.leftMargin = (int) ((int)event.getRawX() + windowwidth - (vg.getWidth()));
v.setLayoutParams(par);
break;
}//inner case UP
case MotionEvent.ACTION_DOWN:
{
// par.height = 60;
// par.width = 60;
v.setLayoutParams(par);
break;
}
}
break;
}
}
return true;
}
};
请让我知道我错在哪里或更好的方式来做到这一点。拖放只有1个图像。
答案 0 :(得分:0)
我有同样的问题,这是我找到的解决方案:
private View.OnTouchListener onTouchListener() {
return new View.OnTouchListener() {
@Override
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_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;
}
image_layout.invalidate();
return true;
}
};
}
添加这两行是最重要的:
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
这会使imageView在进入右上角或右下角的角落时无法改变其大小。
希望它会对你有所帮助。