如何根据图像在屏幕上的放置位置显示消息

时间:2014-07-17 15:08:08

标签: java android eclipse motionevent

我正在尝试根据拖动图像的位置显示不同的消息。

到目前为止,我已经能够使用运动事件拖放图像。我创建了3个在应用程序启动时看不见的文本视图(太高,太完美,太低)。

用户移动图像后,我想显示不同的信息,具体取决于它是否属于某个y坐标。

那么如何判断图像是否在所需的y坐标范围内,然后将文本设置为在这些坐标内是否可见?

到目前为止,这是我的代码:

主要活动:

package com.example.touchanddragimage;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;



public class MainActivity extends ActionBarActivity implements OnTouchListener {


ImageView arrow;
TextView hightext;
TextView lowtext;
TextView perfecttext;
TextView guide;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrow = (ImageView)findViewById(R.id.arrow);
    arrow.setOnTouchListener(this);

    hightext = (TextView) findViewById(R.id.high) ;
    lowtext = (TextView) findViewById(R.id.low);
    perfecttext = (TextView) findViewById(R.id.perfect);
    guide = (TextView) findViewById(R.id.textView2);

    hightext.setVisibility(View.INVISIBLE);
    lowtext.setVisibility(View.INVISIBLE);
    perfecttext.setVisibility(View.INVISIBLE);

}

  float x,y=0.0f;
boolean moving = false;


@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    switch(arg1.getAction()){
    case MotionEvent.ACTION_DOWN:
        moving=true;
        break;

    case MotionEvent.ACTION_MOVE:
        if(moving){
            x=arg1.getRawX()-arrow.getWidth()/2;
            y=arg1.getRawY()-arrow.getHeight()*3/2;
            arrow.setX(x);
            arrow.setY(y);

        }

        break;

    case MotionEvent.ACTION_UP:
        moving=false;

        break;
    }
    return true;
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.touchanddragimage.MainActivity" >

<ImageView
    android:id="@+id/arrow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="351dp"
    android:src="@drawable/greenarrow" />

<TextView
    android:id="@+id/high"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/arrow"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="99dp"
    android:layout_marginTop="56dp"
    android:text="@string/high" 
    android:textSize="24sp"
    />

     <TextView
         android:id="@+id/perfect"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/high"
         android:layout_marginTop="131dp"
         android:layout_toLeftOf="@+id/low"
         android:text="@string/perfect"
         android:textSize="24sp" />

     <TextView
         android:id="@+id/low"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/high"
         android:layout_below="@+id/arrow"
         android:layout_marginLeft="46dp"
         android:layout_marginTop="182dp"
         android:text="@string/low"
         android:textSize="24sp" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

在MotionEvent.ACTION_UP中检查y坐标的值,如果它在所需的范围内,则设置textview的可见性,例如

 case MotionEvent.ACTION_UP:
    moving=false;

    if(y < 100){
       high.setVisibility(View.Visible);
    }else if(y < 200){
       perfect.setVisibility(View.Visible);
    }else{
       low.setVisibility(View.Visible);
    }

    break;
}