[英]Increase/ decrease value with dragging
我想在我的应用程序中具有这样的功能:当用户在屏幕上向上或向下拖动手指时,某些参数将减少,当向下拖动时,参数将增加。 我将使用的是动态更改绘制元素的长度。 你能给我一些指导如何写这样的东西吗?
编辑:更准确地说。 我想我应该使用MotionEvent.ACTION_MOVE。 但是无法弄清楚如何做:根据移动Y的长度,我增加或减少比例值。 你可以帮帮我吗?
我找不到解决方案(),但是今天我做了以下操作:
package com.example.test1;
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;
public class MainActivity extends Activity implements OnGestureListener {
private TextView TextView01;
private TextView TextView02;
private TextView TextView03;
private TextView TextView04;
private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView01 = (TextView)findViewById(R.id.TextView01);
TextView02 = (TextView)findViewById(R.id.TextView02);
TextView03 = (TextView)findViewById(R.id.TextView03);
TextView04 = (TextView)findViewById(R.id.TextView04);
gestureScanner = new GestureDetector(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
int whereIsFirstX = 0;
int whereIsFirstY = 0;
int diffrenceBetweenFirstAndCurrentTouchPositionX = 0;
int diffrenceBetweenFirstAndCurrentTouchPositionY = 0;
int changeInHorizontal = 0;
int changeInVertical = 0;
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
if (eventaction == MotionEvent.ACTION_DOWN) {
whereIsFirstX = X;
whereIsFirstY = Y;
}
if (eventaction==MotionEvent.ACTION_MOVE){
diffrenceBetweenFirstAndCurrentTouchPositionX = X - whereIsFirstX;
diffrenceBetweenFirstAndCurrentTouchPositionY = whereIsFirstY - Y;
}
// FINAL RESULT TO USE IN YOUR APP; I USED IT IN VERTICAL DIRECTION TO CHANGE SPEED IN GAME
//Here the difference is also divided by 10 to depress the influence of change into result value:
changeInHorizontal = changeInHorizontal + diffrenceBetweenFirstAndCurrentTouchPositionX/10;
changeInVertical = changeInVertical + diffrenceBetweenFirstAndCurrentTouchPositionY/10;
TextView03.setText("Simple Horizontal: " + Integer.toString(changeInHorizontal));
TextView04.setText("SimpleVertical: " + Integer.toString(changeInVertical));
try { // quiet screen
Thread.sleep(100);
} catch (Exception e) {
}
//Note that onTouchEvent function has to return it for onScroll method:
return gestureScanner.onTouchEvent(event);
}
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
int changeInHorizontalScroll;
int changeInVerticalScroll;
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
changeInHorizontalScroll = (int)(changeInHorizontalScroll - distanceX);
changeInVerticalScroll = (int)(changeInVerticalScroll + distanceY);
TextView01.setText("onScroll Horizontal: " + Integer.toString(changeInHorizontalScroll));
TextView02.setText("onScroll Vertical: " + Integer.toString(changeInVerticalScroll));
return false;
}
}
活动必须具有TextView01,TextView02,TextView03和TextView04。
01和02使用手势onScroll
方法用法显示增加/减少的值。 我今天第一次使用它。 GestureDetector(this)
构造函数只有一个问题。 它已被弃用,您应该找到如何用现代版本替换它(应该不会很困难,但是由于我用Android做了一些事情而且我今天没有时间这样做,已经很长时间了,我的朋友等威士忌:D )。
我试过提到onScroll
因为我不记得以前通过简单的触摸事件是如何做到的。 所显示的在03和04 TextViews上显示结果的方法是正确的,但仅当用户不进行“返回移动”时才适用。 正确的操作只有在手指向下触摸,沿水平或垂直方向移动并返回时才停止,直到停止触摸屏幕为止。 这是因为始终将移动值添加到结果中,而无需检查它是变大还是变小。 我在二月份以某种方式通过添加更多代码来做到这一点,但现在谁知道呢...; P
请考虑我完全是菜鸟,可能还有其他更有效的方法,但希望对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.