zoukankan      html  css  js  c++  java
  • Android用canvas画哆啦A梦

    先上图:



    非常easy

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

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

    哆啦A梦就出来了 

    是不是非常easy...







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

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


    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
    }
    
    
    } 


    然后在onDraw函数中实现画图

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    }

    先初始化RectF和Paint

    mRect = new RectF(getWidth(), getHeight(), getRight(), getBottom());
    
    
    if (mPaint == null) {
    mPaint = new Paint();
    } else {
    mPaint.reset();
    }
    mPaint.setAntiAlias(true);// 边缘无锯齿</span>
     


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

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

    radius = getWidth() / 2;
    mPaint.setColor(mBlue);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, mPaint); 


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

    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);



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


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

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




  • 相关阅读:
    P2043 质因子分解
    CODE[VS] 3164 质因数分解
    借过
    CODE[VS] 1165 字符串的展开 || P1098 字符串的展开
    CODE[VS] 1144 守望者的逃离 || P1095 守望者的逃离
    CODE[VS] 2914 吹空调
    AC日记
    AC日记
    山科日记—回文
    山科日记—编写函数myFloor和myCeil(编程题)
  • 原文地址:https://www.cnblogs.com/llguanli/p/7018941.html
Copyright © 2011-2022 走看看