Api Demo - .graphics(18)

package com.example.android.apis.graphics;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
//import com.example.android.apis.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

public class PathFillTypes extends GraphicsActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }
    
    private static class SampleView extends View {
        private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private Path mPath;

        public SampleView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            mPath = new Path();
            mPath.addCircle(40, 40, 45, Path.Direction.CW);//添加路径,
            mPath.addCircle(80, 80, 45, Path.Direction.CW);
        }
        
        private void showPath(Canvas canvas, int x, int y, Path.FillType ft,
                              Paint paint) {
            canvas.save();
            canvas.translate(x, y);
            canvas.clipRect(0, 0, 120, 120);
            canvas.drawColor(Color.WHITE);
            mPath.setFillType(ft);//设置填充模式
            canvas.drawPath(mPath, paint);
            canvas.restore();
        }
        
        @Override protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;

            canvas.drawColor(0xFFCCCCCC);
            
            canvas.translate(20, 20);
            
            paint.setAntiAlias(true);

            showPath(canvas, 0, 0, Path.FillType.WINDING, paint);
            showPath(canvas, 160, 0, Path.FillType.EVEN_ODD, paint);
            showPath(canvas, 0, 160, Path.FillType.INVERSE_WINDING, paint);
            showPath(canvas, 160, 160, Path.FillType.INVERSE_EVEN_ODD, paint);
            /*FillType 用来处理当多个路径相互重叠时,如何确认路径内部与路径外部。
             * 有两种模式。
             * WINDING模式:无论重叠与否只要是被轮廓围住都属于路径内部。
             * EVEN_ODD模式,奇偶模式,当将新路径添加上去时,新路径与以前路径的重叠部分取逆。
             * 至于INVERSE_WINDING,INVERSE_EVEN_ODD顾名思义则为前两种模式的逆向模式。
             * 
             */
        }
    }
}

相关推荐