2010-06-03 123 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/