Кнопка блокировки экрана Android на экране при наведении курсора

1

Я разрабатываю приложение Android для блокировки Android. Но теперь я застрял. В основном у меня есть целая куча кнопок на экране, и мне нужно зарегистрироваться, когда они перетаскивают палец с одной кнопки на другую и в каком порядке.

Как я могу это сделать?

Я пытаюсь использовать MotionEvent.ACTION_MOVE в методе OnTouch, но он не работает. (Он работает только для кнопки 1, так как я печатаю, чтобы зарегистрировать идентификатор кнопки, которая зависнет, но она не будет печатать для любой другой кнопки, кроме кнопки 1)

Пожалуйста, сообщите, как я могу это сделать?

Теги:

1 ответ

0

я делаю это один раз, вам нужно получить все позиции вашего вида (x и y с помощью yourButton.getLeft() и yourButton.getTop() но будьте осторожны, потому что вы можете получить после создания макета, не используйте это в onCreate()) то в onTouch() вас есть идентификатор вашей кнопки, в onTouch() вы можете получить положение пальца с me.getX(), me.getY(), но это значение имеет родственник с первым видом, поэтому вы должны есть что-то связать это.

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

используйте журнал для ловли me.getX(), me.getY() вы понимаете сами.

затем проверьте rightXPos и rightYPos со своим списком позиций, затем вы можете узнать, что нажата кнопка Witch

Я надеюсь, что это полезно для вас

редактировать

это пример кода, который делает это, попробуйте это.

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<Integer> _clickedPos;
    ArrayList<Integer> _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<Integer>();
        _leftDestanceLine = new ArrayList<Integer>();
        _topDestanceLine = new ArrayList<Integer>();


        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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TitleEn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TitleFa"
        android:layout_below="@+id/TitleFa"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/TitleFa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffa500"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#000" />

    <TextView
        android:id="@+id/textView25"
        android:layout_width="2dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView24"
        android:layout_alignTop="@+id/textView22"
        android:layout_marginRight="45dp"
        android:layout_toLeftOf="@+id/textView17"
        android:background="#000" />

    <TextView
        android:id="@+id/textView21"
        android:layout_width="2dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView24"
        android:layout_alignTop="@+id/textView22"
        android:layout_marginRight="90dp"
        android:layout_toLeftOf="@+id/textView25"
        android:background="#000"  />

    <TextView
        android:id="@+id/textView26"
        android:layout_width="2dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView24"
        android:layout_alignTop="@+id/textView22"
        android:layout_marginLeft="45dp"
        android:layout_toRightOf="@+id/textView17"
        android:background="#000"  />

    <TextView
        android:id="@+id/textView23"
        android:layout_width="2dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView24"
        android:layout_alignTop="@+id/textView22"
        android:layout_marginLeft="90dp"
        android:layout_toRightOf="@+id/textView26"
        android:background="#000"  />

    <TextView
        android:id="@+id/textView24"
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:layout_above="@+id/textView17"
        android:layout_alignLeft="@+id/textView21"
        android:layout_alignRight="@+id/textView23"
        android:layout_marginBottom="5dp"
        android:background="#000"  />

    <TextView
        android:id="@+id/textView28"
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:layout_above="@+id/textView24"
        android:layout_alignLeft="@+id/textView24"
        android:layout_alignRight="@+id/textView23"
        android:layout_marginBottom="90dp"
        android:background="#000"  />

    <TextView
        android:id="@+id/textView27"
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:layout_above="@+id/textView28"
        android:layout_alignLeft="@+id/textView28"
        android:layout_alignRight="@+id/textView23"
        android:layout_marginBottom="90dp"
        android:background="#000"  />

    <TextView
        android:id="@+id/textView22"
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:layout_above="@+id/textView27"
        android:layout_alignLeft="@+id/textView21"
        android:layout_alignRight="@+id/textView23"
        android:layout_marginBottom="90dp"
        android:background="#000"  />

    <TextView
        android:id="@+id/textView41"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView27"
        android:layout_alignLeft="@+id/textView22"
        android:layout_alignRight="@+id/textView25"
        android:layout_alignTop="@+id/textView25"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:layout_toLeftOf="@+id/textView25"
        android:background="#000"
        android:gravity="center"
        android:text="1"
        android:textColor="#fff"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView43"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView27"
        android:layout_alignLeft="@+id/textView26"
        android:layout_alignRight="@+id/textView23"
        android:layout_alignTop="@+id/textView23"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:background="#000"
        android:gravity="center"
        android:text="3"
        android:textColor="#fff"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView47"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView21"
        android:layout_alignLeft="@+id/textView28"
        android:layout_alignRight="@+id/textView41"
        android:layout_alignTop="@+id/textView45"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="2dp"
        android:background="#000"
        android:gravity="center"
        android:text="7"
        android:textColor="#fff"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView45"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView26"
        android:layout_alignLeft="@+id/textView43"
        android:layout_alignRight="@+id/textView28"
        android:layout_alignTop="@+id/textView28"
        android:layout_marginBottom="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="2dp"
        android:background="#000"
        android:gravity="center"
        android:text="9"
        android:textColor="#fff"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView42"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView27"
        android:layout_alignTop="@+id/textView41"
        android:layout_toLeftOf="@+id/textView43"
        android:layout_toRightOf="@+id/textView41"
        android:background="#fff"
        android:gravity="center"
        android:text="2"
        android:textColor="#000"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView48"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView28"
        android:layout_alignLeft="@+id/textView41"
        android:layout_alignRight="@+id/textView41"
        android:layout_below="@+id/textView41"
        android:background="#fff"
        android:gravity="center"
        android:text="4"
        android:textColor="#000"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView49"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView48"
        android:layout_alignRight="@+id/textView42"
        android:layout_below="@+id/textView41"
        android:layout_toRightOf="@+id/textView41"
        android:background="#000"
        android:gravity="center"
        android:text="5"
        android:textColor="#fff"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView44"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView49"
        android:layout_alignLeft="@+id/textView43"
        android:layout_alignRight="@+id/textView43"
        android:layout_alignTop="@+id/textView49"
        android:background="#fff"
        android:gravity="center"
        android:text="6"
        android:textColor="#000"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/textView46"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView24"
        android:layout_alignLeft="@+id/textView49"
        android:layout_alignRight="@+id/textView49"
        android:layout_alignTop="@+id/textView47"
        android:background="#fff"
        android:gravity="center"
        android:text="8"
        android:textColor="#000"
        android:textSize="50sp" />

</RelativeLayout>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню