将视图移动到AsyncTask

时间:2014-03-01 10:13:53

标签: android android-fragments android-asynctask ondraw

我有一个类(SpotDetails),其中包含一个以编程方式绘制的片段。直到现在我已经将片段绘图类(WindRose)作为主类的子代。

我想做的是将WindRose类移动到AsynTask中以获得更好的用户体验。现在,应用程序在主线程上遭受了太多的工作。

实施WindRose的代码:

WindRose windRose = new WindRose(SpotDetails.this);
    //Add a new windRose (Which is created under)
    FrameLayout.addView(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返回什么?

1 个答案:

答案 0 :(得分: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);
   }
}