Android 如何将值从animatedview传递到mainactivity

Android 如何将值从animatedview传递到mainactivity,android,android-animation,Android,Android Animation,我有一个名为AnimatedView.java的类,它是从ImageView扩展而来的,在这个类中,我有一个onTouchEvent方法,其中我有一个int类型的值,名为count.my requirement,每次执行事件时我都要将这个值传递给MainActivity 主要活动是: Context context; int score=0; EditText text; @Override protected void onCreate(Bundle

我有一个名为
AnimatedView.java
的类,它是从
ImageView
扩展而来的,在这个类中,我有一个
onTouchEvent
方法,其中我有一个int类型的值,名为count.my requirement,每次执行事件时我都要将这个值传递给MainActivity

主要活动是:

    Context context;
    int score=0;
    EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        Intent intent=new Intent();
        int i=intent.getIntExtra("score",score);
        EditText editText=(EditText) findViewById(R.id.editText1);
        editText.setText(i);
    }
在animatedview.java中:

public boolean onTouchEvent(MotionEvent event) {
    Log.d("bharat", "ontouch called");
    int touchType = event.getAction();

    switch (touchType) {
        case MotionEvent.ACTION_MOVE:
            a = event.getX();
            b = event.getY();
            touching = true;
            if (dm_touched) {
                x = (int) a - bm_offsetx;
                y = (int) b - bm_offsety;
            }
            break;

        case MotionEvent.ACTION_DOWN:
            //x and y give you your touch coordinates
            a = event.getX();
            b = event.getY();
            touching = true;
            Log.d("bharat", "action_down called");
            if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
                bm_offsetx = (int) x - x;
                bm_offsety = (int) y - y;
                count++;
                Log.i("bharat", "" + count);
                Intent intent1 = new Intent();
                intent1.putExtra("score", count);
            }
            dm_touched = true;

        case MotionEvent.ACTION_UP:
        default:
            dm_touched = false;
            touching = false;
    }

    return true;
}
    TouchCountListener mTouchCountListener;
     int animatedViewTouchCount=0;

   public void setTouchCountListener(TouchCountListener t){

       mTouchCountListener=t;
    }

public boolean onTouchEvent(MotionEvent event) {

       mTouchCountListener.setTouchCount(++animatedViewTouchCount);

     //...

}
public boolean onTouchEvent(运动事件){
Log.d(“bharat”,“ontouch被称为”);
int touchType=event.getAction();
开关(触摸式){
case MotionEvent.ACTION\u移动:
a=event.getX();
b=event.getY();
触摸=真实;
如果(dm_触摸){
x=(int)a-bm_offsetx;
y=(int)b-bm_offsety;
}
打破
case MotionEvent.ACTION\u DOWN:
//x和y为您提供触摸坐标
a=event.getX();
b=event.getY();
触摸=真实;
Log.d(“bharat”,“行动被称为”);
如果((a>x)和&(ay)和&(b
您创建了一个名为例如TouchCountListener的接口,如下所示:

TouchCountListener.java

public interface TouchCountListener {

    public void setTouchCount();
}
在以下情况下:

    public class yourActivity extends Activity implements TouchCountListener { 

        int animatedViewTouchCount=0;
         //..

        @Override
        protected void onCreate(Bundle savedInstanceState) {

        View  animatedView =  findViewById(R.id.ID_OFYOURANIMATEDVIEW);
        animatedView.setTouchCountListener(this);


           ///....
         }

   }
在animatedview.java中:

public boolean onTouchEvent(MotionEvent event) {
    Log.d("bharat", "ontouch called");
    int touchType = event.getAction();

    switch (touchType) {
        case MotionEvent.ACTION_MOVE:
            a = event.getX();
            b = event.getY();
            touching = true;
            if (dm_touched) {
                x = (int) a - bm_offsetx;
                y = (int) b - bm_offsety;
            }
            break;

        case MotionEvent.ACTION_DOWN:
            //x and y give you your touch coordinates
            a = event.getX();
            b = event.getY();
            touching = true;
            Log.d("bharat", "action_down called");
            if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
                bm_offsetx = (int) x - x;
                bm_offsety = (int) y - y;
                count++;
                Log.i("bharat", "" + count);
                Intent intent1 = new Intent();
                intent1.putExtra("score", count);
            }
            dm_touched = true;

        case MotionEvent.ACTION_UP:
        default:
            dm_touched = false;
            touching = false;
    }

    return true;
}
    TouchCountListener mTouchCountListener;
     int animatedViewTouchCount=0;

   public void setTouchCountListener(TouchCountListener t){

       mTouchCountListener=t;
    }

public boolean onTouchEvent(MotionEvent event) {

       mTouchCountListener.setTouchCount(++animatedViewTouchCount);

     //...

}