先上图:



很简单

第一步:找到一张哆啦A梦的照片

第二部:image.setBackgroundResource(R.drawable.XX);

哆啦A梦就出来了

是不是很简单...







以上是玩笑话..下面我们用代码来实现

首先,自定义一个View,并实现构造方法

<span style="font-size:14px;">public class MyView extends View {public MyView(Context context) {super(context);// TODO Auto-generated constructor stub}public MyView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);// TODO Auto-generated constructor stub}public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}}</span>


然后在onDraw函数中实现绘图

<span style="font-size:14px;color:#000000;">@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}</span>

先初始化RectF和Paint

<span style="font-size:14px;"><span style="color:#000000;">mRect = new RectF(getWidth(), getHeight(), getRight(), getBottom());if (mPaint == null) {mPaint = new Paint();} else {mPaint.reset();}mPaint.setAntiAlias(true);// 边缘无锯齿</span></span>


然后先画最外面蓝色的圆:

首先给圆的半径radius赋值,然后设置颜色以及style,最后画出一个半径为radius的圆

<span style="font-size:14px;">radius = getWidth() / 2;mPaint.setColor(mBlue);mPaint.setStyle(Paint.Style.FILL);canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mPaint);</span>


然后再画一个半径比这个圆大1的空心圆,显示描边的效果

<span style="font-size:14px;">mPaint.setColor(mBlack);mPaint.setStrokeWidth(mStrokeWidth);mPaint.setStyle(Paint.Style.STROKE);// 设置空心mRect.set(getWidth() / 8, (getHeight() - radius) * 3 / 4,getWidth() * 3 / 4, getHeight() * 3 / 4 + radius / 2);</span>



然后画出第二层白色的椭圆,剩下的就不贴代码了,在这里就只是说一下思路


首先画出最外层蓝色的圆以及描边,然后里面画一个白色的椭圆和描边,然后先画左眼和右眼以及眼珠和瞳孔,接下来画鼻子和胡须,最后再画嘴巴,主要用到的知识点有paint画圆、椭圆、斜线、贝塞尔曲线,代码中有些值我这里为了方便写死了...所以在不同机型上显示出来的效果可能有所差异,但重在思想...

源码下载地址:http://download.csdn.net/detail/qq_18612815/9420631




更多相关文章

  1. MTK android代码架构
  2. 编写高效的Android代码(Designing for Performance)
  3. [Android] Adapter:SimpleAdapter SimpleCursorAdapter ArrayAda
  4. 安卓基础(十)
  5. Android获得位置信息(GPS+wifi/基站)
  6. Android获取手机和系统版本等信息的代码
  7. [疑难杂症] Android(安卓)WebView 无法打开天猫页面
  8. Android常见UI组件之ListView(二)——定制ListView
  9. 代码设置EditText只输入数字、字母

随机推荐

  1. Android之WiFi相关类:WiFiConfiguration类
  2. android客户端控制树莓派GPIO点亮LED灯
  3. Android的常用方法(转载)
  4. Android中数据存储——SQLite数据库存储
  5. Android常用复杂控件使用(三)--Fragment
  6. 开放的Android更易中招 安全防护刻不容缓
  7. Qt for Android获取手机序列号/手机型号/
  8. android实现向右滑动返回功能
  9. 东拼西凑的第一个Android应用程序
  10. Android大牛的博客