2013-08-31 48 views
0

在我的Android应用程序中,我将着色鸟素描。我需要用各种颜色给鸟的每一部分上色。我用这个位图的屏幕坐标来标识那些部分。现在我需要的是,当用户触摸身体部位的区域,然后打开另一个具有颜色列表的窗口。为此,我需要将我的View类的数据传递给新的Activity类。我怎样才能做到这一点?请给予帮助!将视图类中的数据传递给Android中的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; 
    } 
+0

我需要传递数据以专门识别用户在屏幕上触摸了哪个部分。 – Shashika

+0

请粘贴代码已经完成 –

+0

添加我的视图类 – Shashika

回答

0

当你构建自己的类,你可以通过外活动的对象的引用, 所以当你在你自己的看法头筹,你可以通过该引用调用该方法并将数据传递给它。
如:

Class OuterActivity externd Activity{ 
public void setVal(int color){ 
} 
... 
... 
View yourView = new YourView(this); 
} 

所以在你看来:

在建筑工:

YourView(Activity inarg){ 
this.mOuterActivity =inarg; 
} 

...//and hold the event 

this.mOuterActivity.setVal(0x0099ff); 
+0

colorList = new CreateColorList(val); 当我传递这样的值时,它会给出运行时错误。 – Shashika

+0

在构造函数中,您应该保留传入的上下文的引用并将其类型转换为源Activity。因此,在您想要传递颜色的位置,可以调用该方法的公共方法。 – bixiaopeng

2

你可以通过这样的方式

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 

      } 
     }); 
    } 

} 
+0

你可以从视图类调用意图。你想从哪里调用Intent视图类? –

0

我有同样的问题,但其他解决方案(谁可能是最好的),我没有工作,所以我所做的是我犯了这样的一个全局变量,从我的观点传回数据我的活动:

public static ArrayList<Coordinates> coordinates = new ArrayList<Coordinates>(); 

它在叫GlobalStatic所以现在一个单独的类我可以调用该变量像这样无处不在:

GlobalStatic.coordinates.... 

,并获取信息到我的看法我只是疯了E在该视图的函数,调用一个在我的活动:在活动

BrushView S_map = new BrushView(this); 
     S_map.setStatmentId(statementId); 

鉴于

public void setStatementId(int id) { 
    statementId = id; 
} 

这不是一个很好的解决方案,但它为我工作。

相关问题