2014-02-26 42 views
0

我有一個名爲MainActivity帆布幀率下降

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 
Customview cust_view; 
CustomThread cust_thread; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    cust_view=new Customview(this); 

    setContentView(cust_view); 

    cust_thread= new CustomThread(); 

    cust_thread.start(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

    } 

我已經定義了一個名爲Customview通過延長view.Inside的方法的onDraw自定義視圖的一個活動,我畫了一個圈......改變它,SX合作縱座標...調用invalidate.thus我的圓圈從屏幕的一個邊緣反彈到其他。

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

public class Customview extends View 
{ 
Paint paint = new Paint(); 
float circle_x; 
int mul=1; 
public Customview(Context context) { 
    super(context); 
} 


public void onDraw(Canvas canvas) 
{ 
    circle_x+=2*mul; 

    if(circle_x==canvas.getWidth()) 
     mul=mul*-1; 
    if(circle_x==0) 
     mul=mul*-1; 

    canvas.drawCircle(circle_x, 100, 400, paint); 
invalidate(); 
} 

} 

我也有while循環,它一直循環反覆CustomThread命名延長thread.Inside的run()中做了一個自定義線程是空的。

public class CustomThread extends Thread{ 


@Override 
public void run() { 

while(true) 
    { 

    } 

       } 
} 

現在,如果我在我的主要活動啓動線程,有圓的運動過程中幀丟棄......這是like..move舉動move..slight停頓了moment..then再次移動..這個重複。

如果我不開始線程,它會平穩移動。

而且奇怪的事情太多:

如果我啓動線程,但是這一次,如果我把

Thread.sleep(0); 

在我的run()方法,它是光滑的一次。

這是怎麼回事?

我該如何解決?

+0

那是什麼線程? – pskink

+0

沒有yet.just測試 – umerbanday

+1

如果你的線程沒有被任何阻塞操作中斷,它吃cpu,你會發現一切都變慢了 – pskink

回答

0

使用「invalidate();」在onDraw()不是你的設備的好主意,你應該用postInvalidateDelayed(20)替換;

+0

我試過了,但滯後仍然存在 – umerbanday