Android中Path的使用

利用Path可以画出各种复杂的二维图形,写了一个简单的例子,效果如下:

程序目录结构:

<ignore_js_op style="word-wrap: break-word;">


其中MainActivity代码如下:

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(new MyLayout(this));  
    }    
}
 

利用自定义的MyLayout填充Activity。MyLayout代码如下:

public class MyLayout extends LinearLayout {
    public MyLayout(Context context) { 
        super(context); 
        // TODO Auto-generated constructor stub 
    }
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas); 
        Log.v("tag", ">>>>>>>>>>>>>>>>>>>>>>"); 
        Path mPath = new Path(); 
        mPath.moveTo(20, 150); 
        mPath.lineTo(60, 200); 
        mPath.lineTo(100, 350); 
        mPath.lineTo(200, 60); 
        mPath.lineTo(140, 160); 
        mPath.close();
        Paint paint = new Paint();
        canvas.drawColor(Color.WHITE);
        paint.setAntiAlias(true); 
        paint.setColor(Color.BLACK); 
        paint.setStyle(Paint.Style.STROKE);
        // 画布上用指定画笔绘出路径 
        canvas.drawPath(mPath, paint);
    }
}

 
其中:

Path mPath = new Path(); 
        mPath.moveTo(20, 150); 
        mPath.lineTo(60, 200); 
        mPath.lineTo(100, 350); 
        mPath.lineTo(200, 60); 
        mPath.lineTo(140, 160); 
        mPath.close();

path用来描述画笔的路径,close方法表示将最后的点和起点封闭起来。

paint.setAntiAlias(true); 
paint.setColor(Color.BLACK); 
paint.setStyle(Paint.Style.STROKE);

 
定义画笔的类型和风格。

原文地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=18630

本文地址:http://bajiewuneng.iteye.com/blog/1919505

猜你喜欢

转载自uwind.iteye.com/blog/1919505