Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape

Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape

Android图形图像基础之OvalShape、RectShape、PaintDrawable、ArcShape。写一个例子说明。

准备一个布局,布局里面竖直方向排列若干TextView:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="100dp"  
  11.         android:layout_margin="10dp"  
  12.         android:gravity="center"  
  13.         android:padding="10dp"  
  14.         android:text="OvalShape"  
  15.         android:textColor="@android:color/white" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/textView2"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="100dp"  
  21.         android:layout_margin="10dp"  
  22.         android:gravity="center"  
  23.         android:padding="10dp"  
  24.         android:text="RectShape"  
  25.         android:textColor="@android:color/white" />  
  26.   
  27.     <TextView  
  28.         android:id="@+id/textView3"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="100dp"  
  31.         android:layout_margin="10dp"  
  32.         android:gravity="center"  
  33.         android:padding="10dp"  
  34.         android:text="PaintDrawable"  
  35.         android:textColor="@android:color/white" />  
  36.   
  37.     <TextView  
  38.         android:id="@+id/textView4"  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="100dp"  
  41.         android:layout_margin="10dp"  
  42.         android:gravity="center"  
  43.         android:padding="10dp"  
  44.         android:text="ArcDrawable"  
  45.         android:textColor="@android:color/white" />  
  46.   
  47. </LinearLayout>  


上层Java代码把OvalShape、RectShape、PaintDrawable、ArcShape分别作为背景Drawable:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package zhangphil.app;  
  2.   
  3. import android.graphics.Color;  
  4. import android.graphics.Paint;  
  5. import android.graphics.drawable.PaintDrawable;  
  6. import android.graphics.drawable.ShapeDrawable;  
  7. import android.graphics.drawable.shapes.ArcShape;  
  8. import android.graphics.drawable.shapes.OvalShape;  
  9. import android.graphics.drawable.shapes.RectShape;  
  10. import android.support.v7.app.AppCompatActivity;  
  11. import android.os.Bundle;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         //椭圆形形状  
  21.         OvalShape ovalShape = new OvalShape();  
  22.         ShapeDrawable drawable1 = new ShapeDrawable(ovalShape);  
  23.         drawable1.getPaint().setColor(Color.BLUE);  
  24.         drawable1.getPaint().setStyle(Paint.Style.FILL);  
  25.         findViewById(R.id.textView1).setBackgroundDrawable(drawable1);  
  26.   
  27.         //矩形形状  
  28.         RectShape rectShape = new RectShape();  
  29.         ShapeDrawable drawable2 = new ShapeDrawable(rectShape);  
  30.         drawable2.getPaint().setColor(Color.RED);  
  31.         drawable2.getPaint().setStyle(Paint.Style.FILL);  
  32.         findViewById(R.id.textView2).setBackgroundDrawable(drawable2);  
  33.   
  34.         //一个继承自ShapeDrawable更为通用、可以直接使用的形状  
  35.         PaintDrawable drawable3 = new PaintDrawable(Color.GREEN);  
  36.         drawable3.setCornerRadius(30);  
  37.         findViewById(R.id.textView3).setBackgroundDrawable(drawable3);  
  38.   
  39.         //扇形、扇面形状  
  40.         //顺时针,开始角度30, 扫描的弧度跨度180  
  41.         ArcShape arcShape = new ArcShape(30180);  
  42.         ShapeDrawable drawable4 = new ShapeDrawable(arcShape);  
  43.         drawable4.getPaint().setColor(Color.YELLOW);  
  44.         drawable4.getPaint().setStyle(Paint.Style.FILL);  
  45.         findViewById(R.id.textView4).setBackgroundDrawable(drawable4);  
  46.     }  
  47. }  



运行结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值