android textview拖动,如何实现拖动的TextView的LinearLayout中在Android

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:paddingLeft="30dp" >

android:id="@+id/ll1"

android:layout_width="200dp"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/tv1"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text="Medium Text"

android:background="#000000"

android:singleLine="true"

android:textAppearance="?android:attr/textAppearanceMedium" />

我希望让TeextView TV1可以在LinearLayout中LL1只有横向拖动。

我如下实施:

ll1 = (LinearLayout)findViewById(R.id.ll1);

tv1 = (TextView)findViewById(R.id.tv1);

Rect rectf = new Rect();

ll1.getGlobalVisibleRect(rectf);

LayoutLeft = rectf.left;

tv1.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

float TouchX = event.getX();

System.out.println("TouchX " + TouchX);

switch(event.getAction()) {

case MotionEvent.ACTION_MOVE:

if(TouchX < LayoutLeft) {

ll1.setPadding(0, 0, 0, 0);

}

else if(TouchX > (LayoutLeft + 100)) {

ll1.setPadding(100, 0, 0, 0);

}

else {

ll1.setPadding((int)(TouchX - LayoutLeft), 0, 0, 0);

}

break;

case MotionEvent.ACTION_UP:

break;

case MotionEvent.ACTION_DOWN:

break;

}

return true;

}

});

但是我测试阻力由左到右,我得到的TouchX值如下:

04-03 14:24:18.061: I/System.out(18468): TouchX 43.554024

04-03 14:24:18.631: I/System.out(18468): TouchX 92.101845

04-03 14:24:19.731: I/System.out(18468): TouchX 49.150528

04-03 14:24:20.371: I/System.out(18468): TouchX 100.82299

04-03 14:24:22.311: I/System.out(18468): TouchX 54.747032

04-03 14:24:22.781: I/System.out(18468): TouchX 74.458405

04-03 14:24:22.801: I/System.out(18468): TouchX 6.1168213

04-03 14:24:22.821: I/System.out(18468): TouchX 80.44317

04-03 14:24:22.851: I/System.out(18468): TouchX 21.571915

04-03 14:24:22.881: I/System.out(18468): TouchX 89.24452

为什么TouchX价值不是为了?

我该如何修改它?

2013-04-03

brian

+0

通过扩展TextView并重写onTouchEvent方法来创建自己的TextView类。在那里你可以适当地处理你的touchX和touchY –

+0

如何处理onTouchEvent中的touchX和touchY? –

+0

我尝试没有ll1.setPadding行,TouchX是为了。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值