Android_自定义View拖拽重绘

Android_自定义View拖拽重绘_第1张图片

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

        }

    } 

}

你可能感兴趣的:(Android,android)