2014-03-01 16 views
0

我有一個類(SpotDetails),其中包含一個以編程方式繪製的片段。直到現在我已經將片段繪圖類(WindRose)作爲主要類的子代。將視圖移入AsyncTask

我想要做的是將WindRose類移入AsynTask以獲得更好的用戶體驗。現在應用程序正在主線程上工作太多。

WindRose windRose = new WindRose(SpotDetails.this); 
    //Add a new windRose (Which is created under) 
    FrameLayout.addView(windRose); 

WINDROSE代碼:實現WINDROSE

代碼

public class WindRose extends View { 

    public WindRose(Context context) { 
     super(context); 


    } 

    @Override 

    protected void onDraw(Canvas canvas) { 


     super.onDraw(canvas); 


     float height = (float) getHeight(); 
     float width = (float) getWidth(); 

     float radius; 

     if (width > height) { 
      radius = height/2; 

     } else { 
      radius = width/2; 
     } 

     // radius = (height)/ 2; 


     Path path = new Path(); 
     path.addCircle(width, height, radius, Path.Direction.CCW); 

     ///2 

     Resources resources = getResources(); 
     int color = resources.getColor(R.color.green_back); 

     Paint paint = new Paint(); 

     paint.setColor(color); 
     paint.setStrokeWidth(5); 

     paint.setStyle(Paint.Style.FILL); 
     float center_x, center_y; 
     center_x = width/2; 
     center_y = height/2; 

     final RectF oval = new RectF(); 

     //Formulas : 
     //SD = Start Degree 
     //ED = End Degree 

     //If cakepiece passes 0 (East) 
     //SD, 360-(SD+ED) 

     //Else : 
     //SD, (ED-SD) 

     oval.set(center_x - radius, center_y - radius, center_x + radius, center_y + radius); 

     if (End > Start) { 
      canvas.drawArc(oval, Start, (End - Start), true, paint); 

     } else if (End < Start) { 
      canvas.drawArc(oval, Start, ((360 - Start) + End), true, paint); 
     } 


    } 
} 

林不知道如果我解釋了正確的事情,所以請告訴我,如果事情不清楚:)

我試圖做到這一點:

public class WindRose extends Activity { 
float Start, End; 
Context context; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

public View DrawRose (Context context){ 
    this.context = context; 
    WindRoseDrawer windRoseDrawer = new WindRoseDrawer(context); 

    return null; //What should i return here ? 


} 


private class DrawWindRose extends AsyncTask<String, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(String... strings) { 


     return null; 

    } 


    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
    } 
} 



public class WindRoseDrawer extends View { 

    public WindRoseDrawer(Context context) { 
     super(context); 


    } 

    @Override 

    protected void onDraw(Canvas canvas) { 


     super.onDraw(canvas); 


     float height = (float) getHeight(); 
     float width = (float) getWidth(); 

     float radius; 

     if (width > height) { 
      radius = height/2; 

     } else { 
      radius = width/2; 
     } 

     // radius = (height)/ 2; 


     Path path = new Path(); 
     path.addCircle(width, height, radius, Path.Direction.CCW); 

     ///2 

     Resources resources = getResources(); 
     int color = resources.getColor(R.color.green_back); 

     Paint paint = new Paint(); 

     paint.setColor(color); 
     paint.setStrokeWidth(5); 

     paint.setStyle(Paint.Style.FILL); 
     float center_x, center_y; 
     center_x = width/2; 
     center_y = height/2; 

     final RectF oval = new RectF(); 

     //Formulas : 
     //SD = Start Degree 
     //ED = End Degree 

     //If cakepiece passes 0 (East) 
     //SD, 360-(SD+ED) 

     //Else : 
     //SD, (ED-SD) 

     oval.set(center_x - radius, center_y - radius, center_x + radius, center_y + radius); 

     if (End > Start) { 
      canvas.drawArc(oval, Start, (End - Start), true, paint); 

     } else if (End < Start) { 
      canvas.drawArc(oval, Start, ((360 - Start) + End), true, paint); 
     } 


    } 
} 


} 

但是,我應該如何實現這個回到SpotDetails?我應該從DrawRose返回什麼?

+0

不明確的,你通過移動視圖意思的AsyncTask – Raghunandan

+0

如果我得到你的意思正確,你可以傳遞給'AsynkTask'的'constructor'或者通過作爲參數傳遞給'doInBackground'方法 –

+0

我加什麼我試圖解決這個問題?這是你的意思嗎 ?我應該如何最終在主類中實現這一點? – Dukes

回答

0

您應該只在UI線程中繪製。如果使用View的Draw繼承方法,則不能在後臺繪製。最好使用帶鎖定/解鎖畫布的SurfaceView。它將使用允許進行背景繪製的優化算法。

@Override 
public void run() { 
    while(locker){ 
    //checks if the lockCanvas() method will be success,and if not, will check this statement again 
    if(!holder.getSurface().isValid()){ 
     continue; 
    } 
    /** Start editing pixels in this surface.*/ 
    Canvas canvas = holder.lockCanvas(); 

    //ALL PAINT-JOB MAKE IN draw(canvas); method. 
     draw(canvas); 

    // End of painting to canvas. system will paint with this canvas,to the surface. 
    holder.unlockCanvasAndPost(canvas); 
    } 
} 
+0

我應該如何在課堂上實現這個功能?今天保持它可能會更好嗎? – Dukes