【问题标题】:How to display a message based on where an image is dropped on screen如何根据图像在屏幕上的放置位置显示消息
【发布时间】:2014-07-17 15:08:07
【问题描述】:

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

到目前为止,我已经能够拖放带有运动事件的图像。我创建了 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>

【问题讨论】:

    标签: java android eclipse motionevent


    【解决方案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;
    }
    

    【讨论】:

    • 谢谢! xml文件的图形布局上是否有一个地方可以实际查看不同坐标的位置?我只是无法在屏幕上直观地找到某个 y 坐标的位置。
    • xml 布局中无法看到。U 需要通过代码获取屏幕尺寸。屏幕坐标也从 0 开始,即屏幕顶部为 0,并且向下增加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多