我一直致力于为我的手机创建一个简单的吉他调音器。我对 android 开发相当陌生,并试图了解使用https://code.google.com/p/android-guitar-tuner/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom创建吉他调音器的过程%2Fexample%2FGuitarTuner帮助很大。
我的问题是在项目运行时显示屏幕而不是黑屏。在日志中,我打印出最佳频率,并且它们打印正确。我已经看到使用自定义视图可以帮助我,但每次我尝试使用它时它都会崩溃。我有一个可绘制的图像,我想用作背景,而不是使用 setContentView(R.layout.activity_tuner) 我想找到一种方法来使用我的自定义视图分析来执行类似 setContentView(new Analyze(this ))。
这是我正在使用的主要活动、xml 和自定义视图类,我主要是寻求有关从哪里开始解决我的问题的指导或有关如何使用可绘制图像设置自定义视图的资源的帮助,如果那是可能的。
我也知道我的自定义视图类中没有任何内容,我使用位图尝试了一些东西,但无济于事。我删除了那里的代码并将 setContentView(new Analyze(this)) 替换为 setContentView(R.layout.activity_tuner) 以便我可以在 logcat 中用吉他测试频率。
活动调谐器.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.thenxttuner"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/graybackgroundtunerpage"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Tuner" >
<com.example.thenxttuner.Analyze
android:id="@+id/custView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ImageView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/dashdrawable" />
<Button
android:id="@+id/start"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/display"
android:layout_below="@+id/display"
android:layout_marginTop="21dp"
android:background="@color/limegreen"
android:text="Stop Tuning" />
</RelativeLayout>
调谐器.java:
public class Tuner extends Activity{
boolean recording = true;
Button start;
Thread myThread;
TextView text, texttwo, textthree;
private double pitch;
Analyze back;
private static final String TAG = "Tuner";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tuner);
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(recording==true){
start.setText("Stop Tuning");
recording=false;
onStart();
}else{
start.setText("Start Tuning");
recording=true;
onStop();
}
}
});
}
@Override
public void onStart(){
super.onStart();
//setContentView(R.layout.activity_tuner);
//Log.v(TAG, "in onStart()");
myThread = new Thread(new Detector(this, new Handler()));
myThread.run();
}
@Override
public void onStop(){
super.onStop();
//Log.v(TAG, "in onStop()");
myThread.interrupt();
}
public void ShowResult(final HashMap<Double, Double> frequencies, final double best){
Log.v(TAG, "best = "+best);
pitches[0][0] = 82.41;
pitches[0][1] = 87.31;
pitches[0][2] = 92.5;
pitches[0][3] = 98;
pitches[0][4] = 103.8;
pitches[1][0] = 110;
pitches[1][1] = 116.54;
pitches[1][2] = 123.48;
pitches[1][3] = 130.82;
pitches[1][4] = 138.59;
pitches[2][0] = 147.83;
pitches[2][1] = 155.56;
pitches[2][2] = 164.81;
pitches[2][3] = 174.62;
pitches[2][4] = 185;
pitches[3][0] = 196;
pitches[3][1] = 207;
pitches[3][2] = 220;
pitches[3][3] = 233.08;
pitches[4][0] = 246.96;
pitches[4][1] = 261.63;
pitches[4][2] = 277.18;
pitches[4][3] = 293.66;
pitches[4][4] = 311.13;
pitches[5][0] = 329.63;
pitches[5][1] = 349.23;
pitches[5][2] = 369.99;
pitches[5][3] = 392;
pitches[5][4] = 415.3;
pitches[5][5] = 440;
}
double[][] pitches = new double[6][6];
}
分析.java:
public class Analyze extends View{
private static final String TAG = "Analyze";
private final static int UI_UPDATE = 100;
private Handler AnaHandler;
private Timer timer;
public Analyze(Context context) {
// TODO Auto-generated constructor stub
super(context);
/**UI update
AnaHandler = new Handler();
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
AnaHandler.post(new Runnable(){
public void run(){
invalidate();
}
});
}
},
UI_UPDATE,
UI_UPDATE);**/
// not sure if needed
}
@Override
public void onDraw(Canvas canvas){
}
}