2014-02-21 78 views
3

我試圖用RectF和canvas.drawRoundRect()繪製一個圓角矩形。請參閱我的代碼如下:使用RectF和畫布繪製圓角矩形?

package com.example.test; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.RelativeLayout; 
import android.graphics.RectF; 

public class MainActivity extends Activity { 

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

     // Create main RL params 
     RelativeLayout.LayoutParams rlMainlayoutParams 
       = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

     // Create main relative layout 
     RelativeLayout rlMain = new RelativeLayout(this); 
     rlMain.setLayoutParams(rlMainlayoutParams); 
     //rlMain.setBackgroundResource(R.drawable.bgndlogin); 
     rlMain.setBackgroundColor(Color.WHITE); 

     RectF rectf = new RectF(200, 400, 200, 400); 
     CustomRectangle customRectangle = new CustomRectangle(this, rectf, 5, 5, "#FFFF00"); 

     // 
     rlMain.addView(customRectangle); 

     setContentView(rlMain); 
    } 

    //!< Draw the log in rectangle shaped panel 
    public class CustomRectangle extends View { 
     Paint paint; 
     float left_side, top_side; 
     String color; 
     RectF rectf; 

     //!< Constructor for the log in rectangle shaped panel 
     public CustomRectangle(Context context, RectF rectf, float left_side, float top_side, String color) { 
      super(context); 

      this.rectf = rectf; 
      this.left_side= left_side; 
      this.top_side = top_side; 
      this.color = color; 

     } 

     //!< Implement to draw the rectangle 
     @Override 
     public void onDraw(Canvas canvas) { 
      paint = new Paint(); 
      paint.setColor(Color.parseColor(color)); 
      paint.setStrokeWidth(3); 
      //paint.setAlpha(61); 

      canvas.drawRoundRect(rectf, left_side, top_side, paint); 

     } 
    } 

} 

該程序運行,但沒有任何繪製,即我只是得到我的白色背景屏幕。任何想法爲什麼?

注意:我以編程方式創建相對佈局,而不是使用XML進行縮放。

回答

10

其實這裏您RectF代表PointRectangle,這就是爲什麼你無法看到Rect ...

RectF rectF = new RectF(left, top, right, bottom); 

這裏RectF

RectF rectf = new RectF(200, 400, 200, 400); // representing Point 

這裏left = right = 200top = bottom = 400代表一個Point

,如果你想畫width = 200Rectheight = 400,那麼你的RectF應該是

RectF rectf = new RectF(0, 0, 200, 400); 

width = 400 and height = 200RectF

RectF rectf = new RectF(0, 0, 400, 200); 
0

您尚未創建正確的矩形。給你的矩形適當的左,頂,右,底點,如:RectF rectf = new RectF(0, 0, 480, 854);

0

乾杯的傢伙。發現。我已經更正了我的代碼,如下所示,現在我可以看到我的矩形。

package com.example.test; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.RelativeLayout; 
import android.graphics.RectF; 

public class MainActivity extends Activity { 

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

     // Create main RL params 
     RelativeLayout.LayoutParams rlMainlayoutParams 
       = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

     // Create main relative layout 
     RelativeLayout rlMain = new RelativeLayout(this); 
     rlMain.setLayoutParams(rlMainlayoutParams); 
     //rlMain.setBackgroundResource(R.drawable.bgndlogin); 
     rlMain.setBackgroundColor(Color.WHITE); 

     RectF rectf = new RectF(0, 0, 200, 300); 
     CustomRectangle customRectangle = new CustomRectangle(this, rectf, 15, 15, "#FFFF00"); 

     // 
     rlMain.addView(customRectangle); 

     setContentView(rlMain); 
    } 

    //!< Draw the log in rectangle shaped panel 
    public class CustomRectangle extends View { 
     Paint paint; 
     float left_side, top_side; 
     String color; 
     RectF rectf; 

     //!< Constructor for the log in rectangle shaped panel 
     public CustomRectangle(Context context, RectF rectf, float left_side, float top_side, String color) { 
      super(context); 

      this.rectf = rectf; 
      this.left_side= left_side; 
      this.top_side = top_side; 
      this.color = color; 

     } 

     //!< Implement to draw the rectangle 
     @Override 
     public void onDraw(Canvas canvas) { 
      paint = new Paint(); 
      paint.setColor(Color.parseColor(color)); 
      paint.setStrokeWidth(3); 
      //paint.setAlpha(61); 

      canvas.drawRoundRect(rectf, left_side, top_side, paint); 

     } 
    } 

} 
0

Android Docs

RectF保持四個浮點座標的矩形。該矩形由其4條邊的座標(左,上,右下)表示。

RectF rectf = new RectF(left, top, right, bottom); 

在片段中,RectF參數如文檔說表示矩形的邊緣座標。

作爲結論,
width = |左 - 右|
height = |頂部|底部|

如果寬度和高度分別爲0,如它並不代表一個視圖,也不是一個對象的問題,
,其在邏輯是不可能的物體存在的高度,寬度和0.

深度的