android Path里面的各种PathEffect

首先上图...



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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值