[英]How to make the TextView drag in LinearLayout smooth, in android?
I have a situation which I am not able to fix, hopefully I will get some advises from you. 我有一个我无法解决的情况,希望我会得到你的一些建议。
The situation is simple: I have a LinearLayout in which I have a TextView with multiple lines of text. 情况很简单:我有一个LinearLayout,其中我有一个带有多行文本的TextView。 The user is able to drag the TextView around until he finds a position he likes.
用户可以拖动TextView,直到找到他喜欢的位置。 What is really important is that the TextView can be partially out of the LinearLayout (it will appear cut).
真正重要的是TextView可以部分地脱离LinearLayout(它将显示为cut)。
Here are some code samples: 以下是一些代码示例:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:clipChildren="false"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/text_color"
android:textSize="16sp" />
</LinearLayout>
As you can see the LinearLayout has clipChildren = false to allow cutoff of text. 如您所见,LinearLayout具有clipChildren = false以允许截断文本。 For the textview I set a touch listener
对于textview,我设置了一个触摸监听器
txt.setOnTouchListener(new View.OnTouchListener() {
int initialX = 0;
int initialY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
initialX = (int) event.getX();
initialY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
int currentX = (int) event.getX();
int currentY = (int) event.getY();
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) txt.getLayoutParams();
int left = lp.leftMargin + (currentX - initialX);
int top = lp.topMargin + (currentY - initialY);
int right = lp.rightMargin - (currentX - initialX);
int bottom = lp.bottomMargin - (currentY - initialY);
lp.rightMargin = right;
lp.leftMargin = left;
lp.bottomMargin = bottom;
lp.topMargin = top;
txt.setLayoutParams(lp);
break;
default:
break;
}
return true;
}
});
And here is my problem: As you can see I've set all the layout parameters (right, left, bottom, top margins). 这是我的问题:正如您所看到的,我已经设置了所有布局参数(右,左,下,上边距)。 Why is that ?
这是为什么 ?
1) If I use only left/top the drag is smooth enought but text get's wrapped at the right border instead of getting cut. 1)如果我只使用left / top,则拖动是平滑的,但是文本被包裹在右边框而不是被切割。 Probably due to right 0 margin value.
可能由于正确的0保证金价值。
2) If I use all margins, the text gets cut as I want, but the movement is not smooth, it just jumps around for a few pixels. 2)如果我使用所有边距,文本会根据需要进行剪切,但移动不平滑,只会跳转几个像素。
How can I fix this ? 我怎样才能解决这个问题 ?
Well. 好。 this is weird.
这很奇怪。 By inserting the TextView inside another LinearLayout with the same size as the initial one, makes everything smooth.
通过将TextView插入另一个与初始大小相同的LinearLayout中,使一切顺利。 I have no idea why.
我不知道为什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.