2014-06-07 4 views
0

Im Asynctask로 약간 어려움을 겪고 있으며 도움이 필요합니다.Asynctask에서 UI 요소에 액세스하십시오.

일부 그래프를 Androidplot으로 플롯하고 있는데, 매우 느리기 때문에 Asynctask를 사용하여 빠르게 처리하려고합니다.

문제는 내가 Asynctask 클래스에서 onCreate()에서 선언 한 요소에 액세스하는 방법을 모르겠다는 것입니다. 그런 다음

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.waveform); 

    plot = (XYPlot) findViewById(R.id.XYPlot0); 
     widget = plot.getGraphWidget(); 
     plot2= (XYPlot) findViewById(R.id.XYPlot01); 
     widget2=plot2.getGraphWidget(); 
     plot3= (XYPlot) findViewById(R.id.XYPlot02); 
     widget3=plot3.getGraphWidget(); 


     Operaciones2 ope=new Operaciones2(); 
     ope.execute(); 

    // Turn the above arrays into XYSeries': 
     XYSeries series1 = new SimpleXYSeries(
       Arrays.asList(n),   // SimpleXYSeries takes a List so turn our array into a List 
       SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
       "Series1"); 

     XYSeries series2 = new SimpleXYSeries(
       Arrays.asList(l),   // SimpleXYSeries takes a List so turn our array into a List 
       SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
       "Series1"); 

     XYSeries series3 = new SimpleXYSeries(
       Arrays.asList(g2),   // SimpleXYSeries takes a List so turn our array into a List 
       SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value 
       "Series1"); 



     // Create a formatter to use for drawing a series using LineAndPointRenderer 
     // and configure it from xml: 
     LineAndPointFormatter series1Format = new LineAndPointFormatter(); 
     //series1Format.setLinePaint(paint); 
     series1Format.setPointLabelFormatter((PointLabelFormatter) null); // con esto en null borramos el valor de las muestras de encima de los puntitos 
     series1Format.getVertexPaint().setStrokeWidth(1); //Establece el tamaño de los puntos de cada muestra 
     Paint linePaint=new Paint(); 
     Paint verPaint=new Paint(); 
     switch(color){ 
     case Color.BLUE: 
      //linePaint.setColor(Color.BLUE); 
      //verPaint.setColor(this.getResources().getColor(R.color.DarkBlue)); 
      series1Format.configure(getApplicationContext(),R.xml.line_blue); 
     break; 
     case Color.RED: 
      //linePaint.setColor(Color.RED); 
      //verPaint.setColor(this.getResources().getColor(R.color.DarkRed)); 
      series1Format.configure(getApplicationContext(),R.xml.line_red); 
     break; 
     case Color.GREEN: 
      //linePaint.setColor(Color.GREEN); 
      //verPaint.setColor(this.getResources().getColor(R.color.DarkGreen)); 
      series1Format.configure(getApplicationContext(),R.xml.line_point_formatter_with_plf1); 
     break; 
     } 
     int i; 

내가 여기에 AsyncTask를 코드가 : 여기 코드입니다

private class Operaciones2 extends AsyncTask<Void, Integer, Void> { 
    int progress; 
    double [] st =new double [arr.length]; 
    int j=0; 


    protected void onPostExecute(Void... unused){ 
     plot.addSeries(series1, series1Format); 
     plot2.addSeries(series2, series1Format); 
     plot3.addSeries(series3, series1Format); 
     Toast.makeText(getApplicationContext(), "Todo cargado", Toast.LENGTH_LONG).show(); 
    } 

    protected Void doInBackground(Void... unused){ 


      for (int i = 44; i < s; i+=2) { 
       // convert byte pair to int 
       double audioSample = (double) (array[i+1] << 8 | array[i] & 0xff)/ 32767.0; 

       arr[j]=audioSample; //double 
       n[j] = (Number)arr[j]; //Number 

       st[j]=10*Math.log10(Math.abs((audioSample/Math.pow(10, -12)))); //double 
       l[j]=(Number)(10*Math.log10(Math.abs((audioSample/Math.pow(10, -12))))); //Number 

       if(audioSample == 0.0){ 
        if(j!=0){ 
        l[j]=l[j-1]; 
        st[j]=st[j-1]; 
        }else{ 
         l[j]=0.0; 
         st[j]=0.0; 
        } 
        } 

       j=j+1;} 
       min=Operaciones.minimum(arr); 
       max=Operaciones.maximum(arr); 
       min2=Operaciones.minimum(st); 
       max2=Operaciones.maximum(st); 
       /*****************************/ 
       arreaFFT(); 
      return null; 
    } 
} 

내 생각은 onPostExecute로 doInBackground에서 계산 된 계열을 추가하는 것입니다,하지만 문제는 내가하지는이다 AsyncTask에서 onCreate()에서 생성 된 series1-2-3 및 series1Format에 액세스하는 방법을 실제로 알고 있습니다 (변수로 해석 할 수 없다는 메시지가 표시됨).

어떤 아이디어 ??

+0

그냥 빨리 노트에서 액세스 할 수 있습니다 : 위의 기록으로, 만 FFT 코드가 이루어집니다 배경으로 사용하기에 합리적 일 수 있습니다. 그러나 렌더 스테이지에서 느린 속도가 느껴지는 경우 ('느림'은 주 스레드가 사용자, OS 또는 둘 모두에게 응답하지 않을 정도로 지연됨) Androidplot의 백그라운드 렌더링 모드를 사용하고자 할 수 있습니다. – Nick

+0

안녕하세요. Nick, 답변 해 주셔서 감사합니다. 당신이 말했듯이, 내가 원하는대로 빨리 진행되지는 않는다. 나는 백그라운드 렌더링 모드에 대해 조사하려고 노력해 왔지만, 이것이 실제로 어떻게 작동하고 어떻게 구현하는지에 대한 명확한 설명을 찾지 못했다. 더 많은 정보를 찾을 수있는 곳을 아십니까? btw : AndroidPlot의 위대한 작업 – paviflo

+0

최상의 리소스는 [DemoApp의 오리엔테이션 센서 예제 소스 코드]입니다 (https://bitbucket.org/androidplot/androidplot/src/d90fc9fe7705a3f82fe44406cd28a34d9169e056/Examples/DemoApp/src/com/androidplot/). 데모/OrientationSensorExampleActivity.java? at = master). 위 코드를 감안할 때 귀하의 음모가 역동적이지 않다고 가정합니다 - 그 점을 확인할 수 있습니까? 또한 각 시리즈에 얼마나 많은 점수가 있습니까? – Nick

답변

0

다른 옵션은 클래스의 구성원으로 series1Formatplot를 선언 할 것이다, 당신은 onPostExecute

public myActivity extends Activity{ 
    LineAndPointFormatter mSeries1Format; 
    // Declare plot as member 


    protected void onCreate(Bundle savedInstanceState) { 
     // Initialize mSeries1Format 
    }  

} 
+0

나는 이미 이것을 시도했지만 그것은 나에게 또 다른 많은 무리를 준다. – paviflo

+0

@paviflo 어느 것이지? – elbaulp

+0

06-07 20 : 49 : 43.237 : E/AndroidRuntime (14829) : 원인 : java.lang.NullPointerException android.util.TypedValue.applyDimension (TypedValue.java:333) at com.androidplot.util.PixelUtils .dpToPix (PixelUtils.java:103) at com.androidplot.xy.LineAndPointFormatter.initLinePaint (LineAndPointFormatter.java:101) at com.androidplot.xy.LineAndPointFormatter. (LineAndPointFormatter.java:63) (com.androidplot.xy.LineAndPointFormatter). (LineAndPointFormatter.java:74) (com.androidplot.xy.LineAndPointFormatter). (LineAndPointFormatter.java:70) at com.example.acoustics.Grafica. paviflo

0

onProgressUpdateonPostexecute 방법이 AsyncTask 인 경우에만 UI를 변경할 수 있습니다.

관련 문제