2015-08-08 86 views
0

我正在嘗試製作生長線的動畫,它將看起來像該線正在朝着畫布寬度的末端移動,並且該線的開始將僅是線的終點應該是增長 這裏是我的代碼在android PLZ上畫線的類將幫助任何幫助將被設置。如何在畫布上製作生長線動畫android

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(); 
} 
} 
+0

所以我才明白正確的;你有兩個點p0 =(x0,y0)和p1 =(x1,y1),你想要從p0增長到p1?如果是這樣,對_linear interpolation_做一些研究會有很大的幫助。 – harism

+0

我有兩個點p0 =(x0,y0)和p1 =(x1,y1),我想要一條從p0到p1的線,然後像p1(x1 + 5,y1 + 5)一樣以某種速度前進,直到它到達畫布寬度。 –

+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(); 
    } 
}