Android图形系统中形状Drawable使用详解

当你想动态画二维图形,ShapeDrawable对象是可能是你合适的选择.使用ShapeDrawable,你可以随意画出原始的形状并且应用到任何风格.

ShapeDrawable是一个Drawable的派生类,所以你可以用于任何想使用Drawable的地方—比如可能是一个View的背景,通过setBackgroundDrawable()所设置.当然,你也可以把你的形状作为它自己的自定义View绘制,然后以你喜欢的方式添加到你的Layout.因为ShapeDrawable有它自己的draw()方法,你可以创建一个View的子类然后在View.onDraw()的方法中画这个ShapeDrawable.下面是一个简单的View派生类,它仅把ShapeDrawable当作一个View绘出:

 
 
  1. [java] public class CustomDrawableView extends View {
  2. private ShapeDrawable mDrawable;
  3.  
  4. public CustomDrawableView(Context context) {
  5. super(context);
  6.  
  7. int x = 10;
  8. int y = 10;
  9. int width = 300;
  10. int height = 50;
  11.  
  12. mDrawable = new ShapeDrawable(new OvalShape());
  13. mDrawable.getPaint().setColor(0xff74AC23);
  14. mDrawable.setBounds(x, y, x + width, y + height);
  15. }
  16.  
  17. protected void onDraw(Canvas canvas) {
  18. mDrawable.draw(canvas);
  19. }
  20. }
  21. public class CustomDrawableView extends View {
  22. private ShapeDrawable mDrawable;
  23.  
  24. public CustomDrawableView(Context context) {
  25. super(context);
  26.  
  27. int x = 10;
  28. int y = 10;
  29. int width = 300;
  30. int height = 50;
  31.  
  32. mDrawable = new ShapeDrawable(new OvalShape());
  33. mDrawable.getPaint().setColor(0xff74AC23);
  34. mDrawable.setBounds(x, y, x + width, y + height);
  35. }
  36.  
  37. protected void onDraw(Canvas canvas) {
  38. mDrawable.draw(canvas);
  39. }
  40. }

在构造函数中,以OvalShape定义了一个ShapeDrawable.然后给它设置了颜色和形状的边界.如果你不设置边界,形状是不会被画出的,如果你不设置颜色,将以黑色画之.
使用自定义View,你可以画任何东西.在上面的例子中,我们可以在一个Activity中用程序随意作画:

 
 
  1. [java] CustomDrawableView mCustomDrawableView;
  2.  
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. mCustomDrawableView = new CustomDrawableView(this);
  6.  
  7. setContentView(mCustomDrawableView);
  8. }
  9. CustomDrawableView mCustomDrawableView;
  10.  
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. mCustomDrawableView = new CustomDrawableView(this);
  14.  
  15. setContentView(mCustomDrawableView);
  16. }

如果你想在XMLlayout中画这个自定义drawable而不是在Activity中,那么这个CustomDrawableView类必须重写View(Context,AttributeSet)构造方法,此方法在从XML初始化一个View时被调用.然后添加一个CustomDrawable元素到XML,像这样:

[html] <com.example.shapedrawable.CustomDrawableView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
<com.example.shapedrawable.CustomDrawableView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>

ShapeDrawable类(就像android.graphics.drawable包中的其它一些Drawable类型)允许你使用公开方法来定义drawable的各种属性.你要调整的一些属性可能有alpha透明,颜色过虑,抖动,opacity和color等.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值