2016-04-02 18 views
0

Nesnemi animasyon yapmaya çalışıyorum. Bir nesne çizmek için bir tuval kullanmam gerekiyor. Sadece onDraw işlevinden nesne yapıcısına tuval gönderebilirim. Ama bunu yaparken, animasyonun her adımında yeni bir nesne yaratıyor.OnDraw işlevinden nesne için Canvas nasıl gönderilir?

public class DrawView extends View { 
    public static Paint paint; 
    public DrawView(Context context) { 
     super(context); 
     paint = new Paint(); 
     paint.setColor(Color.BLACK); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     Line s = new Line(10, 10, canvas); 
     super.onDraw(canvas); 
     s.move(); 
     invalidate(); 
    } 
} 

class Line{ 

    private float x, y; 
    private Canvas canvas; 

    public Line(float x, float y, Canvas canvas) { 
     this.x = x; 
     this.y = y; 
     this.canvas = canvas; 
    } 

    public void move(){ 
     draw();  
     x++; 
     y++; 
    } 

    public void draw(){ 
     canvas.drawLine(x, y, x + 5, y + 5, DrawView.paint); 
    } 

} 

cevap

0

Bunu mu demek istediniz?

public void move(Canvas canvas){ 
    draw(Canvas canvas);  
    x++; 
    y++; 
} 

public void draw(Canvas canvas){ 
    canvas.drawLine(x, y, x + 5, y + 5, DrawView.paint); 
}