2016-05-16 14 views
0

Ich wollte Rectangle um 360 Grad in 10 Sekunden drehen. Ich möchte auch eine andere Bildrate anwenden, zum Beispiel 5 fps, 10 fps usw. Aber egal welche Bildrate ist, das Rechteck sollte in 10 Sekunden rotieren.Wie eine zeitbasierte Animation in Android zu tun?

Also bitte sagen Sie mir, wie kann ich fps der Animation steuern? Welche Animationsmethode sollte ich verwenden?

Ich verwende folgende Methode. Schlagen Sie eine andere gute Methode dazu vor. Vielen Dank im Voraus.

public class Square extends View { 

private Rect rect; 
private Paint paint; 
float height1; 
float width1; 

public Square(Context context) 
{ 

super(context); 
DisplayMetrics displaymetrics = new DisplayMetrics(); 
((Activity)  getContext()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int height = displaymetrics.heightPixels; 
int width = displaymetrics.widthPixels; 
height1=height; 
width1=width; 

Point cos = new Point(width/2,height/2); 
int left = cos.x-(200); 
int top = cos.y - 200; 
int right =cos.x+200; 
int bottom = cos.y+200; 

rect = new Rect(left,top,right,bottom); 

// create a rectangle that we'll draw late rect = new Rect(x, y, sideLength, sideLength); 

// create the Paint and set its color 
paint = new Paint(); 






} 


protected void onDraw(final Canvas canvas) { 


super.onDraw(canvas); 
    /* canvas.drawColor(Color.BLACK); 
    canvas.drawRect(rect, paint);*/ 
    //canvas.drawColor(Color.BLACK); 
    long time1 = System.nanoTime(); 
    paint.setStyle(Paint.Style.FILL); 
paint.setColor(Color.WHITE); 
canvas.drawRect(rect, paint); 


// border 
paint.setStyle(Paint.Style.STROKE); 
paint.setColor(Color.GREEN); 
canvas.drawRect(rect, paint); 

long ti = animate().getDuration(); 
String x = "hello"; 

//Log.e("Tag",x+ti); 

long lsttime = System.nanoTime(); 
double fps = 100000000.0/(lsttime - time1); 
animate().rotation(360).setDuration(10000).start(); 


String fpss = "" + fps; 
Log.e("Tag", fpss); 
} 

} 
+0

Zeig uns..was hast du bisher probiert ?? –

Antwort

0
ObjectAnimator rotate = ObjectAnimator.ofFloat(YOUR_LAYOUT, View.ROTATION_Y, 0.0f, 90f, 180f, 360f); 
rotate .setDuration(10000); //10sec 
rotate .setInterpolator(new AccelerateDecelerateInterpolator()); 

sollten Sie ObjectAnimator verwenden. Interpolator beschreibt die Art und Weise wie die Animation gemacht wird, schau dir youtube bei allen Möglichkeiten an.

+0

Wie kann ich FPS damit steuern? –

+0

es hat nichts mit FPS zu tun. Sie können die Änderungen nach Zeit steuern. FPS ist Frames pro Sekunde, du solltest NIEMALS den Standard ändern, es sei denn, du optimierst die Grafik für ein kompliziertes Spiel. –

+0

Meine Zeit ist konstant, d. Und ich benutze Canvas, um Rechteck zu zeichnen und animate() -Methode, um es zu drehen. –