android Path里面的各种PathEffect

来源:互联网 发布:查理一世 知乎 编辑:程序博客网 时间:2024/06/08 00:46

首先上图...



MyView.java

public class PathView2 extends View {    private int itemHeight;    int ROW_GAP=10;    int POINT_NUM=20;    int[] pointX=new int[POINT_NUM];    int[] pointY=new int[POINT_NUM];    int[] colors=new int[7];    PathEffect[] pathEffects={null,new CornerPathEffect(132),new DiscretePathEffect(2f,5f),        new DashPathEffect(new float[]{11,13,22,15,14},10f),null,null,null};    String[] titles={"null","CornerPathEffect","DiscretePathEffect","DashPathEffect","PathDashPathEffect","SumPathEffect","ComposePathEffect"};    int TEXTSIZE=25;    int TEXT_HEIGHT=33;    float curPercent=0f;    int duration=400;    float speed=1f/(200*duration/1000);    public PathView2(Context context, AttributeSet attrs) {        super(context, attrs);        ROW_GAP=Utils.dp2px(context,ROW_GAP);        itemHeight= (int) ((Utils.getScreenHeight(context)-7*ROW_GAP-7*TEXT_HEIGHT)/7.5);        int pointGap=Utils.getScreenWidth(context)/POINT_NUM;        //        pointX[0]=pointY[0]=0;        for (int i=1;i<POINT_NUM;i++){            pointX[i]=i*pointGap;            pointY[i]= (int) (Math.random()*itemHeight);        }        for (int i=0;i<7;i++){            colors[i]= (int) (Math.random()*0xffffffff);            //no transparent color            colors[i]|=0xff000000;        }        Path p=new Path();        p.addRect(0,0,11,11, Path.Direction.CCW);        pathEffects[4]=new PathDashPathEffect(p,12,12, PathDashPathEffect.Style.ROTATE);        pathEffects[5]=new SumPathEffect(pathEffects[4],pathEffects[1]);        pathEffects[6]=new ComposePathEffect(pathEffects[2],pathEffects[3]);        //set timer        final Timer timer=new Timer();        timer.schedule(new TimerTask() {            @Override            public void run() {                curPercent+=speed;                if (curPercent>=1f){                    curPercent=1f;                    timer.cancel();                }                postInvalidate();            }        },10,16);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int height= Utils.getScreenHeight(getContext());        int width=Utils.getScreenWidth(getContext());        RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) getLayoutParams();        setMeasuredDimension(width-layoutParams.leftMargin-layoutParams.rightMargin, height-layoutParams.topMargin-layoutParams.bottomMargin);    }    @Override    protected void onDraw(Canvas canvas) {        //set paint        Paint paint=new Paint();        paint.setAntiAlias(true);        paint.setStyle(Paint.Style.STROKE);        paint.setTextSize(TEXTSIZE);        //set path        Path path=new Path();        path.moveTo(0, 0);        for (int i=1;i<POINT_NUM;i++){            path.lineTo(pointX[i],pointY[i]*curPercent);        }        for (int i=0;i<pathEffects.length;i++){            canvas.drawText(titles[i], 0, TEXT_HEIGHT / 2, paint);            canvas.translate(0, TEXT_HEIGHT);            paint.setColor(colors[i]);            paint.setPathEffect(pathEffects[i]);            canvas.drawPath(path, paint);            canvas.translate(0, itemHeight + ROW_GAP);            paint.setPathEffect(null);            paint.setColor(0xff000000);        }    }}

无聊就加了点动画效果......



谢谢!

0 0
原创粉丝点击