2013-01-15 107 views
0

我想將java代碼佈局和xml佈局合併到一個佈局中。如何將xml佈局和編碼佈局合併爲一個佈局

我有Java代碼顯示音頻流的可視化。我有xml佈局,所以我想將兩個佈局合併爲一個來顯示媒體控制按鈕和圖像波紋管可視化佈局。

Java代碼是

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.audiofx.Visualizer; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.SeekBar; 
import android.widget.TextView; 

import java.io.IOException; 

public class AudioFxDemoActivity extends Activity { 

    //Here is your URL defined 
String url = "http://vprbbc.streamguys.net/vprbbc24.mp3"; 

    //Constants for vizualizator - HEIGHT 50dip 
private static final float VISUALIZER_HEIGHT_DIP = 50f; 

    //Your MediaPlayer 
MediaPlayer mp; 

//Vizualization 
private Visualizer mVisualizer; 

    private LinearLayout mLinearLayout; 
    private VisualizerView mVisualizerView; 
    private TextView mStatusTextView; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Info textView 
    mStatusTextView = new TextView(this); 

    //Create new LinearLayout (because main.xml is empty) 
    mLinearLayout = new LinearLayout(this); 
    mLinearLayout.setOrientation(LinearLayout.VERTICAL); 
    mLinearLayout.addView(mStatusTextView); 

    //set content view to new Layout that we create 
    setContentView(mLinearLayout); 

    //start media player - like normal 
    mp = new MediaPlayer(); 
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 

    try { 
     mp.setDataSource(url); // set data source our URL defined 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  

    try { //tell your player to go to prepare state 
     mp.prepare(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
      //Start your stream/player 
    mp.start(); 

    //setup your Vizualizer - call method 
    setupVisualizerFxAndUI();   

      //enable vizualizer 
      mVisualizer.setEnabled(true); 

      //Info text 
    mStatusTextView.setText("Playing audio..."); 
} 

    //Our method that sets Vizualizer 
private void setupVisualizerFxAndUI() { 
    // Create a VisualizerView (defined below), which will render the simplified audio 
    // wave form to a Canvas. 

    //You need to have something where to show Audio WAVE - in this case Canvas 
    mVisualizerView = new VisualizerView(this); 
    mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 
      (int)(VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density))); 
    mLinearLayout.addView(mVisualizerView); 

    // Create the Visualizer object and attach it to our media player. 
    //YOU NEED android.permission.RECORD_AUDIO for that in AndroidManifest.xml 
    mVisualizer = new Visualizer(mp.getAudioSessionId()); 
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]); 
    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() { 
     public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, 
       int samplingRate) { 
      mVisualizerView.updateVisualizer(bytes); 
     } 

     public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {} 
    }, Visualizer.getMaxCaptureRate()/2, true, false); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 

    if (isFinishing() && mp != null) { 
     mVisualizer.release(); 
     // mEqualizer.release(); 
     mp.release(); 
     mp = null; 
    } 
} 

/** 
* A simple class that draws waveform data received from a 
* {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture } 
*/ 
class VisualizerView extends View { 
    private byte[] mBytes; 
    private float[] mPoints; 
    private Rect mRect = new Rect(); 

    private Paint mForePaint = new Paint(); 

    public VisualizerView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     mBytes = null; 

     mForePaint.setStrokeWidth(1f); 
     mForePaint.setAntiAlias(true); 
     mForePaint.setColor(Color.rgb(0, 128, 255)); 
    } 

    public void updateVisualizer(byte[] bytes) { 
     mBytes = bytes; 
     invalidate(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     if (mBytes == null) { 
      return; 
     } 

     if (mPoints == null || mPoints.length < mBytes.length * 4) { 
      mPoints = new float[mBytes.length * 4]; 
     } 

     mRect.set(0, 0, getWidth(), getHeight()); 

     for (int i = 0; i < mBytes.length - 1; i++) { 
      mPoints[i * 4] = mRect.width() * i/(mBytes.length - 1); 
      mPoints[i * 4 + 1] = mRect.height()/2 
        + ((byte) (mBytes[i] + 128)) * (mRect.height()/2)/128; 
      mPoints[i * 4 + 2] = mRect.width() * (i + 1)/(mBytes.length - 1); 
      mPoints[i * 4 + 3] = mRect.height()/2 
        + ((byte) (mBytes[i + 1] + 128)) * (mRect.height()/2)/128; 
     } 

     canvas.drawLines(mPoints, mForePaint); 
    } 
} 

}

和XML文件是將具有按鈕和其他的東西,我會在以後添加到XML。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="151dp" 
    android:layout_marginLeft="22dp" 
    android:text="Reverse" /> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button1" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_centerHorizontal="true" 
    android:text="play" /> 
<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button2" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="19dp" 
    android:text="forward" /> 

+1

-1,你有什麼試過?你知道'膨脹'的方法嗎? – thelawnmowerman

+0

@thelawnmowerman我知道膨脹方法可以幫助..但在我的情況下,我不知道如何實現這個代碼和XML ..可以請你指導我 –

回答

0

分裂您VisualizerView佈局和AudioFxDemoActivity佈局成兩個獨立的類。 將所有必要的構造函數添加到VisualizerView類中。 之後,添加類似的東西 <your.package.with.VisualizerView android:layout_width="wrap_content" android:layout_height="wrap_content" />

在您的佈局。您也可以在代碼或任何自定義中添加偵聽器。 請隨時提問=)。