三十功名尘与土 八千里路云和月

书生报国无他物 唯有手中笔如刀

Android_自定义View拖拽重绘

TODO

activity

package com.lxf;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;

/**
 * @address BeiJing
 * @author LiXufei
 * @function  自定义view
 */
public class cCustomView extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new customView(cCustomView.this));
    }

    class customView extends View{

        private Paint mPaint;

        public customView(Context context) {
            super(context);
            mPaint = new Paint(); 
        }

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

            //画文字
            mPaint.setColor(Color.RED); //设置画笔颜色
            mPaint.setTextSize(50.0f);  //设置字体大小
            mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);//消除锯齿
            canvas.drawText("自定义View", 200.0f, 60.0f,mPaint);   //1、文字内容    2、x点距离  3、y点距离 4、画笔

            //画直线
            mPaint.setColor(Color.BLUE);
            mPaint.setStrokeWidth(8.0f);
            canvas.drawLine(0.0f, 100.0f, 700.0f, 100.0f, mPaint);

            //画圆
            mPaint.setColor(Color.CYAN);
            canvas.drawCircle(300.0f, 300.0f, 100.0f, mPaint);

            //画矩形
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(200.0f, 420.0f,400.0f,600.0f, mPaint);  //前俩个是矩形左上角坐标,后面俩个是右下角坐标 

            //画多边形
            mPaint.setColor(Color.BLACK);
            Path path = new Path();
            float startX = 270.0f;
            float startY = 700.0f;
            path.moveTo(startX,startY);
            path.lineTo(startX, startY);
            path.lineTo(startX+50, startY);
            path.lineTo(startX, startY+50);
            path.lineTo(startX+50, startY+50);
            path.close();
            canvas.drawPath(path, mPaint);

        }

    } 

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33068255/article/details/51560303
文章标签: android
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Android_自定义View拖拽重绘

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭