我有框架佈局中的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;
}
你爲什麼不使用動畫得到了答案??? – PedroAGSantos
上面的代碼不工作?你遇到的問題?發佈錯誤如果有的話? – Randroid
沒有錯誤,沒有顯示在日誌貓。 –