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 ShapeDrawable之OvalShape、RectShape、PaintDrawable、ArcShape的更多相关文章

  1. Android ShapeDrawable无法上色

    在Android中的View设置背景时,使用ShapeDrawable有可能出现无法上色的问题(最终背景为黑色),例如,使用如下的代码为控件设置颜色时,控件背景将会变成黑色 ShapeDrawable ...

  2. Android ShapeDrawable

    今天做项目碰到一个这样的情况,就是颜色指示框,用的是正方形边框是黑色的,里面填充颜色,颜色值是动态的,为了解决这个问题,查了好多资料,终于找到解决的方法,利用ShapeDrawable,我们自定义一个 ...

  3. Android渐变GradientDrawable叠加组合环ring

     Android渐变GradientDrawable叠加组合环ring 写一个Android环形shape之间的叠加组合形成新图像的例子.代码: <?xml version="1. ...

  4. Android GradientDrawable的XML实现

     Android GradientDrawable的XML实现 Android GradientDrawable与附录文章1类似,这次以XML而非Java代码形式实现.比如写好一个shape文件放 ...

  5. Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现

     Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现 LayerDrawable实现的结果和附录文章1,2,3中的layer-list一致. ...

  6. Android开发 ShapeDrawable详解

    前言 ShapeDrawable一开始我以为它是对应xml文件属性里的shape的画图,后来发现我错了... 其实ShapeDrawable更像是一共自由度更大跟偏向与实现draw()方法的一共图像绘 ...

  7. Android APK开发 Drawable文件夹下的自定义Drawable文件

    版本:2018/2/11 Drawable的分类 自定义Drawable SVG矢量图 个人总结的知识点外,部分知识点选自<Android开发艺术探索>-第六章 Drawable 1.Dr ...

  8. Android学好Shape不再依赖美工

    原创 2014年03月27日 15:33:41 标签: Android Shape用法 20427 先上图 其实以上效果没有让美工提供任何图片 只要学会Shape你就能实现 想怎么样就怎么样 下面介绍 ...

  9. Android Shape 形状

    Shape继承体系: Shape (android.graphics.drawable.shapes) ----PathShape (android.graphics.drawable.shapes) ...

随机推荐

  1. 为RecyclerView打造通用Adapter 让RecyclerView更加好用

    原文出处: 张鸿洋 (Granker,@鸿洋_ ) 一.概述 记得好久以前针对ListView类控件写过一篇打造万能的ListView GridView 适配器,如今RecyclerView异军突起, ...

  2. AJPFX谈Java 性能优化之基本类型 vs 引用类型

    ★名词定义 先明确一下什么是“基本类型”,什么是“引用类型”. 简单地说,所谓基本类型就是 Java 语言中如下的8种内置类型: booleancharbyteshortintlongfloatdou ...

  3. Android SlidingTabLayout的使用--替代ActionBar的Tab导航

    最近在使用ActionBar的时候,如果使用的是最新版V7包或者最新的SDK平台,就会发现 ActionBar的导航功能已经不建议使用了.主要的原因是ActionBar自带Tab导航自定义性差(只能通 ...

  4. 在vscode中显示空格和tab符号

    转自:https://blog.csdn.net/bmzk123/article/details/86501706 使用python时最烦人的就是代码对齐,而且tab和空格还不一样,为了便于对其,希望 ...

  5. guanbi selinux

    编辑/etc/sysconfig/selinux,把第一条选项改为 disabled

  6. laravel的scout包安装及laravel-es包安装

    安装laravel/scout 作用:搜索驱动,可随时更换驱动,上层业务逻辑可不用改变 官网文档:https://laravel-china.org/docs/laravel/5.4/scout/12 ...

  7. elasticsearch插入索引文档 对数字字符串的处理

    对于字符串在搜索匹配的时候,字符串是数字的话需要匹配的是精准匹配,如果是部分匹配字符串的话,需要进行处理,把数字型字符串作为一个字符中的数组表示插入之后显示如下: 如果插入之后显示如画线部分的话,则表 ...

  8. java 面试题整理

    java面试题 1.接口和抽象类的区别 抽象类 接口 抽象类中可以有默认方法 在java8之前,不能有默认方法 extends implements 抽象类中可以有构造器 接口中不能有构造器 抽象类中 ...

  9. index 定义 v-for 未使用变量 实际是没有 :key="index"

    需要有 :key="index" <Checkbox :label="item.key" :key="index" v-for=&qu ...

  10. Day.js 是一个轻量的处理时间和日期的 JavaScript 库

    Day.js 是一个轻量的处理时间和日期的 JavaScript 库,和 Moment.js 的 API 设计保持完全一样. 如果您曾经用过 Moment.js, 那么您已经知道如何使用 Day.js ...