在本章,了解了很多Bitmap、Paint、Canvas等类的使用,以及作用是什么,本章源码较多,就分成上下两章放源码,下一章的地址为:【Android群英传】——第六章(下) Android绘图机制与处理技巧
,这篇博客也仅仅是记录代码和写代码遇到的问题,不做过多的笔记,源码在我的GitHub上
上
6.14 单位转换
这是一个工具类,专门用于px、dp、sp的转换
dp、sp、px单位转换
6.2 2D绘图基础
【注】Paint对象,一般都是默认的mPaint.setStyle(Paint.Style.FILL);//填充满
属性,会导致后面的很多效果不明显,所以应该设置Paint对象为mPaint.setStyle(Paint.Style.STROKE);//空心
,并且,将Paint对象的画笔宽度设置为mPaint.setStrokeWidth(10);
,否则画笔效果不明显
- 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.androiddrawdemo.MyCanvas
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
-
代码:
2D绘图基础
6.3 Android XML绘图
- bitmap
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/bg">
</bitmap>
- Shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FF5DA2FF"
android:endColor="#805FBBFF"
android:angle="45"/>
<!--实现阴影效果-->
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp"/>
<corners android:radius="8dp"/>
</shape>
- Layer
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/bg4"/>
<!--图层效果-->
<item
android:drawable="@drawable/bg7"
android:left="10dip"
android:top="10.0dip"
android:right="10.0dip"
android:bottom="10.0dip"/>
</layer-list>
- Selector,使用:
android:background="@drawable/myselector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#00FF00"/>
<corners android:radius="5dip"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#5F5F5F"/>
<corners android:radius="5dip"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
</shape>
</item>
</selector>
6.4 Android绘图技巧
1. Canvas:"画"仪表盘
- 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.androiddrawdemo.Dashboard
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
-
代码:
仪表盘
2. Layer图层:
【注】书上写的是:canvas.saveLayerAlpha(0,0,400,400,127,LAYER_FLAGS);
,但代码中,127就是半透明了(透明度范围[0,255]),不需要LAYER_FLAGS
这个属性,但也可以将127换成这个属性private static final int LAYER_FLAGS = 127;
属性
- 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.androiddrawdemo.MyLayer
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
-
代码:
Layer图层
网友评论