[英]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.