2010-06-03 95 views
11

我試圖在Android中獲得2D圖形。 作爲一個例子,我想實現一個自定義繪製,並在我的活動表明它下面在Android中實現自定義繪圖

class myDrawable extends Drawable { 

    private static final String TAG = myDrawable.class.getSimpleName(); 
    private ColorFilter cf; 
    @Override 
    public void draw(Canvas canvas) { 


    //First you define a colour for the outline of your rectangle 

    Paint rectanglePaint = new Paint(); 
    rectanglePaint.setARGB(255, 255, 0, 0); 
    rectanglePaint.setStrokeWidth(2); 
    rectanglePaint.setStyle(Style.FILL); 

    //Then create yourself a Rectangle 
    RectF rectangle = new RectF(15.0f, 50.0f, 55.0f, 75.0f); //in pixels 


    Log.d(TAG,"On Draw method"); 
    // TODO Auto-generated method stub 
    Paint paintHandl = new Paint(); 
    // paintHandl.setColor(0xaabbcc); 
    paintHandl.setARGB(125, 234, 213, 34); 
    RectF rectObj = new RectF(5,5,25,25); 
    canvas.drawRoundRect(rectangle, 0.5f, 0.5f, rectanglePaint); 

    } 

    @Override 
    public int getOpacity() { 
    // TODO Auto-generated method stub 
    return 100; 
    } 

    @Override 
    public void setAlpha(int alpha) { 
    // TODO Auto-generated method stub 
    } 

    @Override 
    public void setColorFilter(ColorFilter cf) { 
    // TODO Auto-generated method stub 
    this.cf = cf; 
    } 
} 

我試圖得到儘可能提到

我已經從Android的繪製延長定義自定義繪製這顯示在我的活動,如下圖所示

public class custDrawable extends Activity { 
/** Called when the activity is first created. */ 


LinearLayout layObj = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     layObj = (LinearLayout) findViewById(R.id.parentLay); 
     ImageView imageView = (ImageView) findViewById(R.id.icon2); 
     myDrawable myDrawObj = new myDrawable(); 
     imageView.setImageDrawable(myDrawObj); 
     imageView.invalidate(); 
// layObj.addView(myDrawObj, params); 

    } 
} 

但是當我運行的應用程序,我看到的活動沒有矩形,任何人都可以幫我嗎? 我哪裏錯了?

回答

11

你的問題是在getOpacity()方法。 100不是有效值。您應該使用PixelFormat值。另外,您應該在構造函數中創建RectFPaint,然後調整draw()中的值,以免創建需要垃圾回收的太多對象。像這樣:

public class Square extends Drawable 
{ 
    private final Paint mPaint; 
    private final RectF mRect; 

    public Square() 
    { 
     mPaint = new Paint(); 
     mRect = new RectF(); 
    } 

    @Override 
    public void draw(Canvas canvas) 
    { 
     // Set the correct values in the Paint 
     mPaint.setARGB(255, 255, 0, 0); 
     mPaint.setStrokeWidth(2); 
     mPaint.setStyle(Style.FILL); 

     // Adjust the rect 
     mRect.left = 15.0f; 
     mRect.top = 50.0f; 
     mRect.right = 55.0f; 
     mRect.bottom = 75.0f; 

     // Draw it 
     canvas.drawRoundRect(mRect, 0.5f, 0.5f, mPaint); 
    } 

    @Override 
    public int getOpacity() 
    { 
     return PixelFormat.OPAQUE; 
    } 

    @Override 
    public void setAlpha(int arg0) 
    { 
    } 

    @Override 
    public void setColorFilter(ColorFilter arg0) 
    { 
    } 
} 
+0

我做了修改提到casey,但我仍然無法看到任何有效的東西繪製在我的看法。 – Girish 2010-06-04 06:29:06

+0

另一個問題是,它似乎永遠不會將'ImageView'添加到'LinearLayout'中。你需要添加'layObj.addView(imageView);'到'onCreate()'的末端' – CaseyB 2010-06-07 16:17:52

+0

我已經添加了imageView到線性佈局,它基本上是一個設置界限的問題 在xml中,我硬編碼的寬度和高度現在它顯示出來了,所以我的setBounds有一些問題呢? – Girish 2010-06-10 16:38:53

0

您可能必須實現其他覆蓋,如getIntrinsicWidth()和getIntrinsicHeight()。一種方法是,將layout_width和layout_height設置爲某個常量(如果使用的是Java佈局,則使用XML將layout_width =「42dip」layout_height =「42dip」或將layoutParams設置爲某個值)。一些視圖類型處理沒有getIntrinsic *實現比其他人,所以嘗試它們!這包括一個直觀的視圖

如果沒有特定的寬度或高度,則可以嘗試返回-1。

很難說,如果問題不斷得到解決,但我通過谷歌試圖幫助自己記住做一個自定義繪製對象的細節來到這裏,再加上我想幫助人們避免這種情況:http://xkcd.com/979/