自定义View界面大合集
今天我用自己写的一个Demo 和大家详细介绍一个Android中自定义View中的使用与绘制技巧。

1.自定义view绘制字符串       
       相信在实际开发过程中必然很多地方都须要用到系统字 为什么会用到系统字? 方便 省内存 我相信做过J2ME游戏开发的朋友应该深知内存有多么多么重要  而且使用它还可以带来一个更重要的好处就是很方便的可以实现多国语言的切换 笔者现在在正在做的一个产品就是可以多语言切换的软件  有英语 繁体中文 等等 设想如果使用图片字的话那每个语言都须要出一套图,我用一个例子简单介绍一下绘制字符串。

- package cn.m15.xys;
 - import android.app.Activity;
 - import android.content.Context;
 - import android.graphics.Canvas;
 - import android.graphics.Color;
 - import android.graphics.Paint;
 - import android.graphics.Paint.FontMetrics;
 - import android.os.Bundle;
 - import android.view.Display;
 - import android.view.View;
 - public class Font extends Activity {
 -        public int mScreenWidth = 0;
 -        public int mScreenHeight = 0;
 -        @Override
 -        protected void onCreate(Bundle savedInstanceState) {
 -              setContentView(new FontView(this));
 -              // 获取屏幕宽高
 -              Display display = getWindowManager().getDefaultDisplay();
 -              mScreenWidth = display.getWidth();
 -              mScreenHeight = display.getHeight();
 -              super.onCreate(savedInstanceState);
 -             
 -        }
 -       
 -        class FontView extends View {
 -              public final static String STR_WIDTH = "获取字符串宽为:"; 
 -              public final static String STR_HEIGHT = "获取字体高度为:"; 
 -              Paint mPaint = null;
 -              
 -              public FontView(Context context) {
 -                    super(context);
 -                    mPaint = new Paint();
 -              }
 -             
 -              @Override
 -              protected void onDraw(Canvas canvas) {
 -                    //设置字符串颜色
 -                    mPaint.setColor(Color.WHITE);
 -                    canvas.drawText("当前屏幕宽" + mScreenWidth, 0, 30, mPaint);
 -                    canvas.drawText("当前屏幕高"+ mScreenHeight, 0, 60, mPaint);
 -                    //设置字体大小
 -                    mPaint.setColor(Color.RED);
 -                    mPaint.setTextSize(18);
 -                    canvas.drawText("字体大小为18", 0, 90, mPaint);
 -                    //消除字体锯齿
 -                    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
 -                    canvas.drawText("消除字体锯齿后", 0, 120, mPaint);
 -                    //获取字符串宽度
 -                    canvas.drawText(STR_WIDTH + getStringWidth(STR_WIDTH), 0, 150, mPaint);
 -                    //获取字体高度
 -                    canvas.drawText(STR_HEIGHT + getFontHeight(), 0, 180, mPaint);
 -                    //从string.xml读取字符串绘制
 -                    mPaint.setColor(Color.YELLOW);
 -                    canvas.drawText(getResources().getString(R.string.string_font), 0, 210, mPaint);
 -                    super.onDraw(canvas);
 -              }
 -              
 -              /**
 -              * 获取字符串宽
 -              * @param str
 -              * @return
 -              */
 -              private int getStringWidth(String str) {
 -                    return (int) mPaint.measureText(STR_WIDTH); 
 -              }
 -              /*
 -              * 获取字体高度
 -              */
 -              private int getFontHeight() {
 -                    FontMetrics fm = mPaint.getFontMetrics();
 -                    return (int)Math.ceil(fm.descent - fm.top) + 2;
 -              }
 -        }
 - }
 
复制代码
2.绘制无规则几何图形
        绘制无规则几何图形似乎在实际工作中很少可以用到 原因是用程序去绘制图形即使在精准再好看也不会有美术出的图片好看 但是使用程序绘制图形作为学习来说却是基础中的基础,所以建议大家都看一看。

- package cn.m15.xys;
 - 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.graphics.RectF;
 - import android.os.Bundle;
 - import android.view.View;
 - public class Geometry extends Activity {
 -        public int mScreenWidth = 0;
 -        public int mScreenHeight = 0;
 -       
 -        @Override
 -        protected void onCreate(Bundle savedInstanceState) {
 -              setContentView(new GeometryView(this));
 -              super.onCreate(savedInstanceState);
 -             
 -        }
 -       
 -        class GeometryView extends View {
 -              Paint mPaint = null;
 -             
 -              public GeometryView(Context context) {
 -                    super(context);
 -                    mPaint = new Paint();
 -                    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
 -              }
 -             
 -              @Override
 -              protected void onDraw(Canvas canvas) {
 -                    super.onDraw(canvas);
 -                    
 -                    //设置画布颜色 也就是背景颜色
 -                    canvas.drawColor(Color.WHITE);
 -                    
 -                    mPaint.setColor(Color.BLACK);
 -                    canvas.drawText("绘制无规则几何图形喔!!!", 150, 30, mPaint);
 -                    
 -                    //绘制一条线
 -                    mPaint.setColor(Color.BLACK);
 -                    mPaint.setStrokeWidth(4);
 -                    canvas.drawLine(0, 0, 100, 100, mPaint);
 -                    
 -                    //绘制一个矩形
 -                    mPaint.setColor(Color.YELLOW);
 -                    canvas.drawRect(0, 120, 100, 200, mPaint);
 -                    
 -                    //绘制一个圆形
 -                    mPaint.setColor(Color.BLUE);
 -                    canvas.drawCircle(80, 300, 50, mPaint);
 -                    
 -                    //绘制一个椭圆
 -                    mPaint.setColor(Color.CYAN);
 -                    canvas.drawOval(new RectF(300,370,120,100), mPaint);
 -                    
 -                    //绘制多边形
 -                    mPaint.setColor(Color.BLACK);
 -                    Path path = new Path();
 -                    path.moveTo(150+5 , 400 -50);
 -                    path.lineTo(150+45, 400 - 50);
 -                    path.lineTo(150+30, 460 - 50);
 -                    path.lineTo(150+20, 460 - 50);
 -                    path.close();
 -                    canvas.drawPath(path, mPaint);
 -                    
 -              }
 -        }
 - }