Android的锁屏应用按钮悬停和序列 [英] Android Lock Screen App Button Hover and Sequence

查看:311
本文介绍了Android的锁屏应用按钮悬停和序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android锁屏应用。但现在我卡住了。基本上我有一大堆屏幕上的按钮,我需要能够当他们拖他们的手指从一个按钮旁边的注册,并在其中的顺序。

I am developing an android lock screen app. But now I am stuck. Basically I have a whole bunch of buttons on the screen, and I need to be able to register when they drag their finger from one button to the next, and in which order.

我怎样才能做到这一点?

How can I do this?

我想在OnTouch方法使用 MotionEvent.ACTION_MOVE ,但它不工作。 (这仅适用于按键1,因为我打印出来的logcat正在上空盘旋按钮的ID,但它不会打印任何其他按钮比按钮1)

I am trying to use the MotionEvent.ACTION_MOVE in the OnTouch method, but it isn't working. (It only works for button 1, as I am printing out to logcat the ID of the button that is being hovered over, but it wont print for any other button than button 1)

请我如何能做到这一点建议?

Please advise on how I can do this?

推荐答案

我这样做一次,你需要得到所有位置查看(x和y与 yourButton.getLeft() yourButton.getTop()但要小心,因为你可以布局生成后获得的onCreate不要使用这个()),然后在 onTouch()方法,你有你的按钮的编号, onTouch()方法你可以得到手指的位置 me.getX() me.getY(),但这个值有相对与第一种观点,所以你必须有一些链接这一点。

i do this one time, you need get all position of you view ( x and y with yourButton.getLeft() and yourButton.getTop() but be careful because you can get after layout creating don't use this in onCreate()) then in onTouch() method you have id of your button, in onTouch() method you can get position of finger with me.getX(), me.getY(), but this value have a relative with first view, so you must have something link this.

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

使用日志追赶 me.getX() me.getY()你了解你的自我。

use log for catching me.getX(), me.getY() you understand your self.

然后检查 rightXPos rightYPos 您的位置列表,然后你可以找到巫按钮感动

then check the rightXPos and rightYPos with your list of position, then you can find out witch button touched

我希望这对你有用

修改

这是一个简单的code,它做到这一点,试试这个。

this is a sample code that do this, try this.

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

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>

这篇关于Android的锁屏应用按钮悬停和序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆