Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Android开发-自定义View-AndroidStudio(二十一)onDraw的演示

$
0
0
转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~


老规矩,先上图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
说明文字都写在代码显示出来了,作为了解自定义View这篇博文可以当做第一篇。


MainActivity.java:
package com.iwanghang.newview;

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);
        NewView newView = new NewView(this);
        setContentView(newView);
    }
}
NewView.java:
package com.iwanghang.newview;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class NewView extends View{
    public NewView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NewView(MainActivity mainActivity) {
        super(mainActivity);
    }

    // 当Android系统需要绘制一个View对象时,就会嗲用该对象的onDraw
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        canvas.drawLine(0,0,200,200,paint);

        Paint paint2 = new Paint();
        paint2.setARGB(150,0,100,200);
        canvas.drawRect(200,200,300,300,paint2); // 矩形起点是200,200

        Paint paint3 = new Paint();
        paint3.setARGB(255,51,204,200);
        paint3.setStyle(Paint.Style.STROKE);
        canvas.drawRect(300,300,400,400,paint3); // 矩形起点是300,300

        Paint paint4 = new Paint();
        paint4.setARGB(102,255,102,200);
        paint4.setStyle(Paint.Style.STROKE);
        paint4.setStrokeWidth(10);
        canvas.drawRect(200,400,300,500,paint4); // 矩形起点是200,400

        Paint paint5 = new Paint();
        paint5.setARGB(150,0,100,200);
        canvas.drawCircle(200,200,50,paint5);  // 圆形圆心是200,200

        Paint paint6 = new Paint();
        paint6.setARGB(255,51,204,200);
        paint6.setAntiAlias(true); // 抗锯齿
        canvas.drawCircle(300,300,50,paint6);  // 圆形圆心是300,300

        Paint paint7 = new Paint();
        paint7.setARGB(102,255,102,200);
        paint7.setStyle(Paint.Style.STROKE);
        paint7.setStrokeWidth(10);
        canvas.drawCircle(200,400,50,paint7);  // 圆形圆心是200,400

        Paint paint8 = new Paint();
        paint8.setTextSize(30);
        canvas.drawText("蓝色的这条斜线,是使用drawLine",100,50,paint8);
        canvas.drawLine(0,50,720,50,paint8);

        canvas.drawText("画矩形使用drawRect,画圆使用",300,200,paint8);
        canvas.drawLine(0,200,720,200,paint8);
        canvas.drawText("drawCircle,矩形起点为200,200",300,230,paint8);
        canvas.drawLine(0,230,720,230,paint8);
        canvas.drawText("圆心为200,200",300,260,paint8);
        canvas.drawLine(0,260,720,260,paint8);

        canvas.drawText("这个圆setAntiAlias抗锯齿",380,300,paint8);
        canvas.drawLine(0,300,720,300,paint8);
        canvas.drawText("这个矩形setStyle(Paint.",380,330,paint8);
        canvas.drawLine(0,330,720,330,paint8);
        canvas.drawText(".STROKE)设置为空心",380,360,paint8);
        canvas.drawLine(0,360,720,360,paint8);

        canvas.drawText("setStrokeWidth(10)",300,430,paint8);
        canvas.drawLine(0,430,720,430,paint8);

        canvas.drawText("这里有一个基准线的概念,中文不明显",100,600,paint8);
        canvas.drawLine(0,600,720,600,paint8);
        canvas.drawText("下面这行英文小写字母能看出来:",100,630,paint8);
        canvas.drawLine(0,630,720,630,paint8);
        canvas.drawText("abcdefghijklmnopqrstuvwxyz",100,660,paint8);
        canvas.drawLine(0,660,720,660,paint8);
        canvas.drawText("说明,绘制Text的y参数并不是底,而是基准线",100,690,paint8);
        canvas.drawLine(0,690,720,690,paint8);


        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher),100,700,paint);

        super.onDraw(canvas);
    }
}


转载请注明出处:http://blog.csdn.net/iwanghang/



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~

作者:iwanghang 发表于2017/1/4 14:20:25 原文链接
阅读:153 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles