将数据从View类传递到

将数据从View类传递到

在我的android应用程序中,我将为小鸟着色。我需要用各种颜色给鸟的每个部分上色。我用该位图的屏幕坐标来标识那些部分。现在我需要的是,当用户触摸身体部位的区域时,然后打开另一个具有颜色列表的窗口。为此,我需要将数据从View类传递到新的Activity类。我怎样才能做到这一点?请帮忙!

我的观点班

class BirdColors extends View{

    private Paint paint;
    public Bitmap mBitmap,nBitmap;
    public Canvas canvas;
    private int x,y;
    private CreateColorList colorList;
    int val;

    public BirdColors(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        paint=new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);

        mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.birdsketchfinal2).copy(Config.ARGB_8888, true);


        mBitmap= Bitmap.createScaledBitmap(mBitmap, 230, 230, true);
        //mBitmap=nBitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);


        this.canvas=canvas;
        canvas.drawBitmap(mBitmap,0,0, null);
        //canvas.drawText("Shashika", 10, 50, paint);
        canvas.drawText("Screen Cordinates" +x+"x"+y, 10, 220, paint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
         switch(event.getAction()){

        case MotionEvent.ACTION_DOWN:

            x=(int)event.getX();
            y=(int)event.getY();

            if((x>5 && x<23)&&(y>30 && y<47)){

                    val=1;
                    //colorList=new CreateColorList(val);
                    //In here I need to pass the val

            }
            invalidate();
         }
        return true;
    }

最佳答案

您可以通过这样创建界面来做到这一点

public class BirdColors extends View{

   //.........your code ............

// create local object of BirdColorListener

   private BirdColorsListener local;

// create seter/geter methods

   public void setBirdColorListener(BirdColorsListener birdColorListenr){
   this.local = birdColorListenr;
   }
   public BirdColorsListener setBirdColorListener{
     return this.local;
   }


    @Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
     switch(event.getAction()){

    case MotionEvent.ACTION_DOWN:

        x=(int)event.getX();
        y=(int)event.getY();

        if((x>5 && x<23)&&(y>30 && y<47)){

                val=1;
                //colorList=new CreateColorList(val);
                //In here I need to pass the val

             if(getBirdColorListener()!=null){
               getBirdColorListener().onBirdTouch(val);
             }


        }
        invalidate();
     }
    return true;
}


   //Add this things

 public interface BirdColorsListener{

   void onBirdTouch(int val);
 }

}


在您的活动中;

public class BirdActivity extends Activity{

  BirdColors bc = new BirdColors();




  protected void onCreate(){

    bc.setBirdColorListener(new BirdColorsListener() {

            @Override
            public void onBirdTouch(int val) {
                // you will get "val"  from your view

            }
        });
  }

}

关于java - 在Android中将数据从View类传递到Activity类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18544454/

10-13 00:08