实现在Android的定制绘制 [英] Implementing a customized drawable in Android

查看:134
本文介绍了实现在Android的定制绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想弄个2D图形的Andr​​oid系统。   作为一个例子,我想实现一个自定义绘制,并显示在我的活动

I was trying to get hold of 2D graphics in Android. As a example i want to implement a custom drawable and show it in my Activity

我已经定义了一个自定义绘制由机器人绘制延伸如下所述

I have defined a customized drawable by extending from Android drawable as mentioned below

 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;
   }
 }

我试图让这个显示在我的活动,如下图所示。

I am trying to get this displayed in my activity, as shown below

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);

    }
}

但是,当我运行的应用程序,我看到的活性没有矩形,任何人都可以帮我吗? 我在哪里去了?

But when i run the app i see no rectangle on the activity, can anyone help me out? Where am i going wrong?

推荐答案

您的问题是在 getOpacity()方法。 100不是一个有效的值。您应该使用<一个href="http://developer.android.com/intl/fr/reference/android/graphics/PixelFormat.html">PixelFormat值。此外,你应该创建 RectF 油漆在构造函数中,然后就调整平局(),这样你就不会产生需要垃圾回收这么多的对象。像这样的:

Your problem is in the getOpacity() method. 100 is not a valid value. You should use a PixelFormat value. Also, you should create your RectF and Paint in the constructor and then just adjust the values in draw() so you don't create so many objects that need garbage collected. Like this:

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)
    {
    }
}

这篇关于实现在Android的定制绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆