
我在框架布局中有textvIEw和imagevIEw,我想用手指在图像上移动文本.
<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;}解决方法:
我通过使用此代码得到答案
vg = (VIEwGroup)findVIEwByID(R.ID.vg); vg.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { switch(event.getActionMasked()) { case MotionEvent.ACTION_MOVE: int x = (int)event.getX() - offset_x; int y = (int)event.getY() - offset_y; int w = getwindowManager().getDefaultdisplay().getWIDth(); int h = getwindowManager().getDefaultdisplay().getHeight(); if(x > w) x = w; if(y > h) y = h; linearLayout.LayoutParams lp = new linearLayout.LayoutParams( new VIEwGroup.marginLayoutParams( linearLayout.LayoutParams.FILL_PARENT, linearLayout.LayoutParams.FILL_PARENT)); lp.setmargins(x, y, 0, 0); selected_item.setLayoutParams(lp); break; default: break; } return true; } }); tv = (TextVIEw)findVIEwByID(R.ID.text_vIEw2); tv.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: offset_x = (int)event.getX(); offset_y = (int)event.getY(); selected_item = v; break; default: break; } return false; } });} 总结 以上是内存溢出为你收集整理的android – 如何移动文本视图?全部内容,希望文章能够帮你解决android – 如何移动文本视图?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)