如何移动文本视图?
我在框架布局中有文本视图和图像视图,我想用手指在图像上移动文本。
<FrameLayout
android:id="@+id/framelayout"
android:layout_marginTop="30dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView
android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="@+id/text_view"
android:layout_marginTop="30dip"
android:layout_width="wrap_content"
android:maxLines="20"
android:scrollbars="vertical"
android:layout_height="wrap_content"/>
</FrameLayout>
我尝试了一些代码,但它无法正常工作,请告诉我正确的方法。
public boolean onTouch(View v, MotionEvent event) {
float x1 = 0, x2, y1 = 0, y2;
String direction;
switch(event.getAction()) {
case(MotionEvent.ACTION_DOWN):
x1 = event.getX();
y1 = event.getY();
break;
case(MotionEvent.ACTION_UP): {
x2 = event.getX();
y2 = event.getY();
float dx = x2-x1;
float dy = y2-y1;
// Use dx and dy to determine the direction
if(Math.abs(dx) > Math.abs(dy)) {
if(dx>0) direction = "right";
else direction = "left";
} else {
if(dy>0) direction = "down";
else direction = "up";
}
}
}
return false;
}
I have textview and imageview in the frame layout, i want move the text along with finger on the image.
<FrameLayout
android:id="@+id/framelayout"
android:layout_marginTop="30dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView
android:id="@+id/ImageView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="@+id/text_view"
android:layout_marginTop="30dip"
android:layout_width="wrap_content"
android:maxLines="20"
android:scrollbars="vertical"
android:layout_height="wrap_content"/>
</FrameLayout>
i try some code but it's not working properly tell me the correct way.
public boolean onTouch(View v, MotionEvent event) {
float x1 = 0, x2, y1 = 0, y2;
String direction;
switch(event.getAction()) {
case(MotionEvent.ACTION_DOWN):
x1 = event.getX();
y1 = event.getY();
break;
case(MotionEvent.ACTION_UP): {
x2 = event.getX();
y2 = event.getY();
float dx = x2-x1;
float dy = y2-y1;
// Use dx and dy to determine the direction
if(Math.abs(dx) > Math.abs(dy)) {
if(dx>0) direction = "right";
else direction = "left";
} else {
if(dy>0) direction = "down";
else direction = "up";
}
}
}
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用这段代码得到了答案
i got answer by using this code
检查这个代码片段,
Check this code snippet,