0

这是我更新的代码。它现在根本检测不到运动。也许我不应该让每个图像都成为一个实例?基本上我希望用户能够浏览所有图像以使它们消失。

感谢所有的帮助。

package com.picomputing.mythirdapplication;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by Paul on 8/13/13.
 */
public class Pin extends ImageView implements View.OnTouchListener {

    boolean isPinDown;

    public Pin(Context context) {
        super(context);
        this.isPinDown = false;
    }

    public Pin(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.isPinDown = false;
    }

    public Pin(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.isPinDown = false;
    }

    public boolean pinDown() {
        return this.isPinDown;
    }

    public void setPinDown() {
        this.isPinDown = true;
    }

    public void setPinUp() {
        this.isPinDown = false;
    }

    public void togglePin() {
        if (isPinDown == false)
        {
            isPinDown = true;
            this.setImageResource(Color.TRANSPARENT);
        }
        else
        {
            isPinDown = false;
            this.setImageResource(R.drawable.pin);
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                int x = (int) event.getX();  //--relative to mLayout--
                int y = (int) event.getY();  //--relative to mLayout--
                Rect r = new Rect();
                view.getHitRect(r);
                    if(r.contains(x,y) && view instanceof ImageView){
                        togglePin();
                    }
        }
        return true;
    }
}
4

3 回答 3

1

您需要侦听和使用ACTION_MOVE事件,以获得您要更改的任何内容的父视图。

这是一个在 a 中有几个ImageViewsLinerLayout作为父级的示例:

public class test extends Activity {
    LinearLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLayout = new LinearLayout(this);
        mLayout.setOrientation(LinearLayout.VERTICAL);

        for(int i = 0 ; i < 5; i++){
            ImageView iv = new ImageView(this);
            iv.setImageResource(android.R.drawable.ic_dialog_info);
            mLayout.addView(iv);
        }


        setContentView(mLayout);

        mLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_MOVE:
                        int x = (int) event.getX();  //--relative to mLayout--
                        int y = (int) event.getY();  //--relative to mLayout--
                        Rect  r = new Rect();
                        for(int i = 0 ; i < mLayout.getChildCount(); i++){
                            View v = mLayout.getChildAt(i);
                            v.getHitRect(r);
                            if(r.contains(x,y) && v instanceof ImageView){
                                ((ImageView) v).setImageResource(android.R.drawable.ic_dialog_alert);
                            }
                        }

                }
                return true; //-- this means that view is interested in more events of all kinds--
            }
        });
    }


}
于 2013-08-15T05:51:19.163 回答
0

我希望我没有误解你的问题,但如果你想做的是防止图像上出现多点,你可以添加这个属性

android:splitMotionEvents="false"

在imageview的父视图的xml中。例如 :

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:splitMotionEvents="false" 
    >
 // YOUR IMAGE VIEW HERE
</LinearLayout>

如果您有任何问题,请随时在评论中提问:)

于 2013-08-15T04:38:56.567 回答
0

OnTouch上主要有三个事件action_down、Action_move和Action_up。对动作下降事件进行编码,即当用户触摸您的视图时。请参阅此处的示例:

     @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        if (arg1.getAction()==MotionEvent.ACTION_DOWN) {

            //write your code here
        }
        else {
            if (arg1.getAction()==MotionEvent.ACTION_MOVE){

                do things

            }
            else {
                if (arg1.getAction()==MotionEvent.ACTION_UP){
                    do things
                }
            }
        }
于 2013-08-15T05:12:38.477 回答