2014-03-01 6 views
0

필자는 프로그램 적으로 그려지는 조각을 포함하는 클래스 (SpotDetails)를 가지고 있습니다. 지금까지는 조각 클래스 (WindRose)를 메인 클래스의 자식으로 사용했습니다.AsyncTask로 뷰 이동하기

더 나은 사용자 경험을 위해 WindRose 클래스를 AsynTask로 옮기고 싶습니다. 이제 응용 프로그램은 주 스레드에서 너무 많은 작업을 겪고 있습니다.

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

풍배 코드 : 다음 풍배을 구현하는

코드 내가 일이 :)

I 불분명 한 경우에는 바로 그렇게 말씀 해주십시오 가지를 설명하면

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

의 당신은 내가 당신의 평균 바로 당신이 AsynkTask''의 constructor''에 그것을 전달하거나 내가 추가 한 – Raghunandan

+0

을 AsyncTask를 위해 이동보기로 뜻 불분명 문제를 해결하려는 나의 시도? 그게 당신이 의미 한 것입니까? 결국 메인 클래스에서 이것을 어떻게 구현해야합니까? –

+0

doInBackground' 방법 '에 매개 변수로 전달할 수 있습니다 얻을 경우 – Dukes

답변

0

UI 스레드에서만 그려야합니다. 보기에서 상속 그리기 방법을 사용하는 경우 백그라운드에서 그릴 수 없습니다. 잠금/잠금 해제 캔버스가있는 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

관련 문제