Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape
2016-07-25 16:47
615 查看
Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape
Android图形图像基础之OvalShape、RectShape、PaintDrawable、ArcShape。写一个例子说明。
准备一个布局,布局里面竖直方向排列若干TextView:
上层Java代码把OvalShape、RectShape、PaintDrawable、ArcShape分别作为背景Drawable:
运行结果:
Android ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape
Android图形图像基础之OvalShape、RectShape、PaintDrawable、ArcShape。写一个例子说明。
准备一个布局,布局里面竖直方向排列若干TextView:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_margin="10dp" android:gravity="center" android:padding="10dp" android:text="OvalShape" android:textColor="@android:color/white" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_margin="10dp" android:gravity="center" android:padding="10dp" android:text="RectShape" android:textColor="@android:color/white" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_margin="10dp" android:gravity="center" android:padding="10dp" android:text="PaintDrawable" android:textColor="@android:color/white" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_margin="10dp" android:gravity="center" android:padding="10dp" android:text="ArcDrawable" android:textColor="@android:color/white" /> </LinearLayout>
上层Java代码把OvalShape、RectShape、PaintDrawable、ArcShape分别作为背景Drawable:
package zhangphil.app; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.PaintDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.ArcShape; import android.graphics.drawable.shapes.OvalShape; import android.graphics.drawable.shapes.RectShape; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //椭圆形形状 OvalShape ovalShape = new OvalShape(); ShapeDrawable drawable1 = new ShapeDrawable(ovalShape); drawable1.getPaint().setColor(Color.BLUE); drawable1.getPaint().setStyle(Paint.Style.FILL); findViewById(R.id.textView1).setBackgroundDrawable(drawable1); //矩形形状 RectShape rectShape = new RectShape(); ShapeDrawable drawable2 = new ShapeDrawable(rectShape); drawable2.getPaint().setColor(Color.RED); drawable2.getPaint().setStyle(Paint.Style.FILL); findViewById(R.id.textView2).setBackgroundDrawable(drawable2); //一个继承自ShapeDrawable更为通用、可以直接使用的形状 PaintDrawable drawable3 = new PaintDrawable(Color.GREEN); drawable3.setCornerRadius(30); findViewById(R.id.textView3).setBackgroundDrawable(drawable3); //扇形、扇面形状 //顺时针,开始角度30, 扫描的弧度跨度180 ArcShape arcShape = new ArcShape(30, 180); ShapeDrawable drawable4 = new ShapeDrawable(arcShape); drawable4.getPaint().setColor(Color.YELLOW); drawable4.getPaint().setStyle(Paint.Style.FILL); findViewById(R.id.textView4).setBackgroundDrawable(drawable4); } }
运行结果:
相关文章推荐
- Android滑动选择控件 WheelView
- Android-eventBus的使用
- 你真的会用Gson吗?Gson使用指南(三)
- 布局简介之线性布局
- Android获取PopUpWindow的资源ID以及弹出WheelView
- Android studio 模块引用
- Android Camera 相机开发详解
- 在android中运行java main方法
- android中jni,内存优化,设计模式总结
- android在代码中怎么使得控件居中,以及代码设置控件selector
- 安卓下拉刷新开源库对比
- Android性能优化典范之多线程篇
- LogUtils类统一管理log日志
- Android开发笔记(一百一十四)发布工具
- Android Binder通信机制浅析
- Glide 框架的使用-1
- Android图片压缩(质量压缩和尺寸压缩)
- Android启动Activity中间黑屏
- ListView常用拓展(Android群英传)
- Android 使用Vitamio打造自己的万能播放器(8)――细节优化