Skip to content

jin404861445lan/CustomView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CustomView

Android自定义View

参考csdn

http://blog.csdn.net/guolin_blog/article/details/17357967#t0

image

代码:

public class CustomView extends View implements View.OnClickListener {
    private Paint mPaint;
    private Rect mBounds;
    private int mCount = 0;


    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBounds = new Rect();
        setOnClickListener(this);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.BLUE);
        canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
        mPaint.setColor(Color.YELLOW);
        mPaint.setTextSize(30);
        String text = String.valueOf(mCount);
        mPaint.getTextBounds(text, 0, text.length(), mBounds);
        float textWidth = mBounds.width();
        float textHeight = mBounds.height();
        canvas.drawText(text, getWidth() / 2 - textWidth / 2, getHeight() / 2
                + textHeight / 2, mPaint);
    }

    @Override
    public void onClick(View v) {
        mCount++;
        invalidate();//重绘
    }
}

###欢迎关注虚实科技微信公众号,获得最新的技术和科技资讯

About

Android自定义View

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages