关闭

Android_自定义View拖拽重绘

标签: android
122人阅读 评论(0) 收藏 举报
分类:

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

        }

    } 

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3303次
    • 积分:157
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论