如何在画布上制作增长线动画android

时间:2015-08-08 19:01:47

标签: android canvas

我正在尝试制作一条增长线的动画,看起来这条线正朝着画布宽度的末端移动,并且线的起点将是恒定的,只有线的末端才会增长 这是我的类代码,在PLZ上绘制线条帮助任何有用的帮助都可以上传。

 package com.example.line;

 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Paint.Align;
 import android.view.View;

 public class MyPlay extends View{

 float startx = 30;
 float starty = 60;
 float endx=0;
 float endy=0;
 public MyPlay(Context context) {
     super(context);
     // TODO Auto-generated constructor stub
 }

 @Override
 protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    Paint textPaint = new Paint();
    textPaint.setARGB(50, 254, 10, 50);
    textPaint.setColor(Color.RED);
    textPaint.setTextAlign(Align.CENTER);
    textPaint.setTextSize(20);
    canvas.drawText("Made by Skyrush", canvas.getWidth()/2, canvas.getHeight()/2, textPaint);

    Paint linePaint = new Paint();
    linePaint.setColor(Color.BLACK);
    linePaint.setStrokeWidth(2);

    //canvas.drawLine(startx, starty, endx, endy, linePaint);
    if(endx < canvas.getWidth()){

        endx = startx+5;
        endy = starty-5;
        canvas.drawLine(startx, starty, endx, endy, linePaint);
        invalidate();
    }
    invalidate();
 }
}

1 个答案:

答案 0 :(得分:0)

this网页中,可以制作线条动画。

您只需在自定义视图中实现以下方法:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    //  starting point
    x1 = 50;
    y1 = 50;

    //  ending point
    x2 = getWidth() / 2 + getWidth() / 4;
    y2 = getHeight() / 2 + getHeight() / 4;

    Log.d("line xy xy", x1 + " : "+y1+" : "+x2 + " : "+y2);

    divideLineIntoEqualParts();
}


//  dividing line into 50 equal parts
private void divideLineIntoEqualParts() {

/*
 * Courtesy : www.dummies.com
 * (x,y) = (x1 + k(x2 - x1),y1 + k(y2 - y1))
 * */

    listOfPoints.clear();
    for (int k = 1; k <= 50; k++) {
        listOfPoints.add(new PointF(x1 + ((k * (x2 - x1)) / 50),y1 + (k * (y2 - y1)) / 50));            
    }

    Log.d("listOfPoints : size : ",listOfPoints.size()+"");
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(inte < listOfPoints.size()){
        canvas.drawLine(listOfPoints.get(0).x, listOfPoints.get(0).y, listOfPoints.get(inte).x,listOfPoints.get(inte).y, paint);
        inte++;

    if(inte < listOfPoints.size()){
        invalidate();
    }   
}