2013-10-25 84 views
0

顯示畫布矩形的TextView

imageView 
TextView 
Rectangle drawn from canvas 

我做

 public class MainActivity extends Activity { 
     @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

       drawView = new DrawView(this); 
       drawView.setBackgroundColor(Color.WHITE); 
       setContentView(drawView); 
    setContentView(R.layout.description);// If this get invoked then how will `drawView` will get overlapped. 
// then normal 


      } 


      public class DrawView extends View { 
       Paint paint = new Paint(); 

       public DrawView(Context context) { 
        super(context);    
       } 

       @Override 
       public void onDraw(Canvas canvas) { 
        paint.setColor(Color.BLACK); 
        paint.setStrokeWidth(3); 
        canvas.drawRect(30, 30, 80, 80, paint); 
        paint.setColor(Color.BLACK); 
        paint.setColor(Color.CYAN); 
        canvas.drawRect(33, 60, 77, 77, paint); 
        paint.setColor(Color.YELLOW); 
        canvas.drawRect(33, 33, 77, 60, paint); 




       } 

編輯1

protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_new_oct23); 

     episode_image=(ImageView)findViewById(R.id.imageView_EpisodeImage); 
     characters=(ImageView)findViewById(R.id.button_Characters); 
progressBarLayout = (LinearLayout) findViewById(R.id.timerDisplay); 
     DrawView drawView = new DrawView(this); 
    progressBarLayout = (LinearLayout) findViewById(R.id.timerDisplay); 
     DrawView drawView = new DrawView(this); 
     // drawView = new DrawView(this); 
     progressBarLayout.addView(drawView); 






    } 

    public class DrawView extends View { 
     Paint paint = new Paint(); 

     public DrawView(Context context) { 
      super(context);    
     } 

     @Override 
     public void onDraw(Canvas canvas) { 
      /* paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(3); 
      canvas.drawRect(30, 30, 80, 80, paint); 
      paint.setColor(Color.BLACK); 
      paint.setColor(Color.CYAN); 
      canvas.drawRect(33, 60, 77, 77, paint); 
      paint.setColor(Color.YELLOW); 
      canvas.drawRect(33, 33, 77, 60, paint); 

      */ 
      draw(canvas); 


     } 


     @SuppressLint("NewApi") 
     public void draw(Canvas canvas){ 

      int width = 0; 
      int height = 0; 
      Point size = new Point(); 
      WindowManager w = getWindowManager(); 

      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
       w.getDefaultDisplay().getSize(size); 
       width = size.x; 
       height=size.y; 

      }else{ 
       Display d = w.getDefaultDisplay(); 
       width = d.getWidth(); 
       height=d.getHeight(); 
      } 
      Random rnd = new Random(); 
      int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
      width=width-20; 
      int margin=width/SIZE; 
      int begin=10; 

      paint.setStrokeWidth(3);  
      for (int iTmp=0;iTmp<SIZE;iTmp++){ 
       begin=begin+(iTmp)*(margin); 
       // begin=begin-2; 
       System.out.println("onkar "+begin); 

       paint.setColor(Color.CYAN); 
       canvas.drawRect(begin, 20, margin-2, 30, paint); 

      } 

     } 

    } 

     // drawView = new DrawView(this); 
     progressBarLayout.addView(drawView);` 

回答

1

你應該有一個這樣的方法:

// set content view 
setContentView(R.layout.description); 
// get the parent of imageview and textview, i'll suppose its LinearLayout. 
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout); 
// then add your view, since its a linear layout it will automatically be added below textview. for other layouts you use LayoutParams to position your View. 
ll.addView(new DrawView()); 
+0

progressBarLayout =(LinearLayout中)findViewById(R.id.timerDisplay); \t \t DrawView drawView = new DrawView(this); \t \t drawView = new DrawView(this); \t \t progressBarLayout.addView(drawView); – onkar

0

可以使用FrameLayout裏堆放的意見和使用SurfaceView作爲底視圖進行繪製。