标签:des android style blog color os for 2014 on
package com.example.coustomviewdemo; import android.R.interpolator; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.util.AttributeSet; import android.view.View; public class FightViewDemo extends View { public FightViewDemo(Context context) { super(context); // TODO Auto-generated constructor stub init(context,null); } public FightViewDemo(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init( context, attrs); } public FightViewDemo(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub init( context, attrs); } private Paint paint; private void init(Context context, AttributeSet attrs) { paint = new Paint(); paint.setColor(Color.RED); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //TODO 绘制文本 // 整个父控件 getHeight int height = getHeight(); // 整个父控件 getWidth int width = getWidth(); // 如何获取组件自身的高度和宽度? String text = "LoveHellOWorld"; FontMetrics metrics = paint.getFontMetrics(); int descent = (int)metrics.descent; // 初始化 widths 长度为 text文本的字符的大小。 float[] widths = new float[text.length()]; // 获取一哥字符串 每一个字符的像素宽度。 paint.getTextWidths(text, widths); // 获取自身组件的长度 int myWidth=0; for(int i=0;i<widths.length;i++) { myWidth+=widths[i]; } // 获取自身组件的高度 int th = (int)paint.getTextSize(); int posX =(width-myWidth)/2; int posY = height/2 + th/2 - descent; // 话自己的文本内容: canvas.drawText(text, posX, posY, paint); // 标准line canvas.drawLine(0, height/2, width, height/2, paint); canvas.drawLine(width/2, 0, width/2, height, paint); } }
标签:des android style blog color os for 2014 on
原文地址:http://blog.csdn.net/love_javc_you/article/details/40081057