Android锁屏应用按钮hover和序列

我正在开发一个Android锁屏应用程序。 但现在我被卡住了。 基本上我在屏幕上有一大堆按钮,当他们将手指从一个按钮拖到下一个按钮时,我需要能够注册,以及按顺序。

我怎样才能做到这一点?

我试图在OnTouch方法中使用MotionEvent.ACTION_MOVE,但它无法正常工作。 (它只适用于按钮1,因为我打印出logcat正在hover的按钮的ID,但它不会打印除按钮1之外的任何其他按钮)

请告诉我如何做到这一点?

我这样做了一次,你需要得到你所有的位置(x和y与yourButton.getLeft()yourButton.getTop()但要小心,因为你可以得到布局创建后不要在onCreate()使用它然后在onTouch()方法中你有你的按钮的id,在onTouch()方法中你可以用me.getX()me.getY()得到手指的位置,但是这个值有第一个视图的亲戚,所以你必须有一些链接这个。

 int rightXPos = yourButton.getLeft()+ me.getX() // yourButton is Button that int rightYPos = yourButton.getTop()+ me.getY() // Toached firstTime 

使用log来捕获me.getX()me.getY()你理解你的自我。

然后用您的位置列表检查rightXPosrightYPos ,然后您可以找到触摸的女巫按钮

我希望这对你有用

编辑

这是执行此操作的示例代码,请尝试此操作。

MainActivity.class

 package activity; import java.util.ArrayList; import shayan.pourvatan.lowandhigh.R; import android.animation.ArgbEvaluator; import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.TextView; import farsiConverter.FarsiDigit; public class MainActivity extends FragmentActivity implements OnClickListener , OnTouchListener { ArrayList _clickedPos; ArrayList _leftDestanceLine , _topDestanceLine; TextView tv1 , tv2 , tv3 , tv4 , tv5 , tv6 , tv7 , tv8 , tv9; TextView line1 , line2 , line3 , line4 , line5 , line6 , line7, line8; TextView titleEn , titleFa; boolean _firstTime; int _count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); _clickedPos = new ArrayList(); _leftDestanceLine = new ArrayList(); _topDestanceLine = new ArrayList(); InitializeTextView(); InitilizeLine(); // detector = new GestureDetector(this,this); DefaultBackground(); _count = 0; _firstTime = true; } private void InitilizeLine() { line1 = (TextView) findViewById(R.id.textView21); line2 = (TextView) findViewById(R.id.textView22); line3 = (TextView) findViewById(R.id.textView23); line4 = (TextView) findViewById(R.id.textView24); line5 = (TextView) findViewById(R.id.textView25); line6 = (TextView) findViewById(R.id.textView26); line7 = (TextView) findViewById(R.id.textView27); line8 = (TextView) findViewById(R.id.textView28); } private void InitializeTextView() { tv1 = (TextView) findViewById(R.id.textView41); tv2 = (TextView) findViewById(R.id.textView42); tv3 = (TextView) findViewById(R.id.textView43); tv4 = (TextView) findViewById(R.id.textView44); tv5 = (TextView) findViewById(R.id.textView45); tv6 = (TextView) findViewById(R.id.textView46); tv7 = (TextView) findViewById(R.id.textView47); tv8 = (TextView) findViewById(R.id.textView48); tv9 = (TextView) findViewById(R.id.textView49); titleEn = (TextView) findViewById(R.id.TitleEn); titleFa = (TextView) findViewById(R.id.TitleFa); tv1.setTag(1); tv2.setTag(2); tv3.setTag(3); tv4.setTag(4); tv5.setTag(5); tv6.setTag(6); tv7.setTag(7); tv8.setTag(8); tv9.setTag(9); View main = findViewById(R.id.RelativeLayout1); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); tv4.setOnClickListener(this); tv5.setOnClickListener(this); tv6.setOnClickListener(this); tv7.setOnClickListener(this); tv8.setOnClickListener(this); tv9.setOnClickListener(this); tv1.setOnTouchListener(this); tv2.setOnTouchListener(this); tv3.setOnTouchListener(this); tv4.setOnTouchListener(this); tv5.setOnTouchListener(this); tv6.setOnTouchListener(this); tv7.setOnTouchListener(this); tv8.setOnTouchListener(this); tv9.setOnTouchListener(this); main.setOnTouchListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private void ChangingBackGround(int colorFrom , int colorTo, final View v) { ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); colorAnimation.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animator) { v.setBackgroundColor((Integer)animator.getAnimatedValue()); } }); colorAnimation.start(); } @Override protected void onResume() { overridePendingTransition(0,0); super.onResume(); } private void DefaultBackground() { tv1.setBackgroundColor(Color.parseColor("#000000")); tv3.setBackgroundColor(Color.parseColor("#000000")); tv5.setBackgroundColor(Color.parseColor("#000000")); tv7.setBackgroundColor(Color.parseColor("#000000")); tv9.setBackgroundColor(Color.parseColor("#000000")); tv2.setBackgroundColor(Color.parseColor("#ffffff")); tv4.setBackgroundColor(Color.parseColor("#ffffff")); tv6.setBackgroundColor(Color.parseColor("#ffffff")); tv8.setBackgroundColor(Color.parseColor("#ffffff")); } private View GetViewPos(int _tempPos) { switch (_tempPos) { case 1: return tv1; case 2: return tv2; case 3: return tv3; case 4: return tv4; case 5: return tv5; case 6: return tv6; case 7: return tv7; case 8: return tv8; case 9: return tv9; default: break; } return null; } private int CheckPosition(float X, float Y) { int pos = -1; pos = Position1checked(X , Y); pos = Position2checked(X , Y , pos); pos = Position3checked(X , Y, pos); pos = Position4checked(X , Y, pos); pos = Position5checked(X , Y, pos); pos = Position6checked(X , Y, pos); pos = Position7checked(X , Y, pos); pos = Position8checked(X , Y, pos); pos = Position9checked(X , Y, pos); return pos; } private int Position9checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(2) && x < _leftDestanceLine.get(3) && y > _topDestanceLine.get(2) && y < _topDestanceLine.get(3)) return 5; return -1; } private int Position8checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(1) && x < _leftDestanceLine.get(2) && y > _topDestanceLine.get(2) && y < _topDestanceLine.get(3)) return 6; return -1; } private int Position7checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(0) && x < _leftDestanceLine.get(1) && y > _topDestanceLine.get(2) && y < _topDestanceLine.get(3)) return 7; return -1; } private int Position6checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(2) && x < _leftDestanceLine.get(3) && y > _topDestanceLine.get(1) && y < _topDestanceLine.get(2)) return 4; return -1; } private int Position5checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(1) && x < _leftDestanceLine.get(2) && y > _topDestanceLine.get(1) && y < _topDestanceLine.get(2)) return 9; return -1; } private int Position4checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(0) && x < _leftDestanceLine.get(1) && y > _topDestanceLine.get(1) && y < _topDestanceLine.get(2)) return 8; return -1; } private int Position3checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(2) && x < _leftDestanceLine.get(3) && y > _topDestanceLine.get(0) && y < _topDestanceLine.get(1)) return 3; return -1; } private int Position2checked(float x, float y, int pos) { if (pos > -1) return pos; else if (x > _leftDestanceLine.get(1) && x < _leftDestanceLine.get(2) && y > _topDestanceLine.get(0) && y < _topDestanceLine.get(1)) return 2; return -1; } private int Position1checked(float x, float y) { if (x > _leftDestanceLine.get(0) && x < _leftDestanceLine.get(1) && y > _topDestanceLine.get(0) && y < _topDestanceLine.get(1)) return 1; return -1; } private void FillArrayPos() { _leftDestanceLine.add(line1.getLeft()); _leftDestanceLine.add(line5.getLeft()); _leftDestanceLine.add(line6.getLeft()); _leftDestanceLine.add(line3.getLeft()); _topDestanceLine.add(line2.getTop()); _topDestanceLine.add(line7.getTop()); _topDestanceLine.add(line8.getTop()); _topDestanceLine.add(line4.getTop()); for (int i = 0 ; i < 4 ; i++) { Log.d(""+_leftDestanceLine.get(i),""+_topDestanceLine.get(i) ); } _firstTime = false; } @Override public boolean onTouch(View v, MotionEvent me) { if (_firstTime) FillArrayPos(); if (me.getActionMasked() == MotionEvent.ACTION_UP) { Log.d("in action up", "123"); // CheckEquality(_count); // if _count == 1 then user just } int _tempPos = CheckPosition(me.getX()+v.getLeft() , me.getY()+v.getTop()); if (_clickedPos.size() > 0) { if (_tempPos == -1){} else if (_tempPos == _clickedPos.get(_clickedPos.size()-1)){ //change the background of current position } else { _count++; View v1 = GetViewPos(_tempPos); ChangingBackGround(Color.parseColor("#ffffff") , Color.parseColor("#ffa500") , v1 ); _clickedPos.add(_tempPos); } } else { if (_tempPos == -1){} else { _count++; View v1 = GetViewPos(_tempPos); ChangingBackGround(Color.parseColor("#ffffff") , Color.parseColor("#ffa500") , v1 ); _clickedPos.add(_tempPos); } } return true; } } 

splash.xml