如何在自定义视图中使用可绘制图像作为背景? [英] How can I use a drawable image in a custom view for the background?

查看:142
本文介绍了如何在自定义视图中使用可绘制图像作为背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试根据 https中的代码创建基于Android的吉他调音器: //code.google.com/p/android-guitar-tuner/ [ ^ ]和我在互联网上找到的FFT算法,我不记得它的实际链接。我正在使用一个名为Detector.java的类来输入声音并将其发送给FFT算法,然后将其发送回我的主要活动中的ShowResult。



当我运行程序时,我看到正在显示的日志正确显示最佳频率,但是当我保留setContentView(R.layout)时,手机屏幕保持黑屏.activity_tuner),但是当我向Analyze.java添加任何内容并尝试将其用作自定义视图时,它会崩溃。我已经多次搜索谷歌试图弄清楚如何设置自定义视图,虽然当我尝试一些我见过的例子时它似乎不起作用。



我知道我的java文件Analyze中没有太多内容,这是我的自定义视图我想尝试显示我的背景图像。我只是觉得在哪里开始让它显示图像。我对使用onDraw的整个想法仍然相当新,任何指导都将不胜感激。



我的代码如下:



activity_tuner.xml:

I've been working on trying to create a guitar tuner for android based off of the code in https://code.google.com/p/android-guitar-tuner/[^] and the FFT algorithm I've found on the internet, I don't remember the actual link to it. I was using a class called Detector.java to input the sound and send it the the FFT algorithm then send it back into ShowResult in my main activity.

When I run the program I see the Logs coming up showing the best frequencies correctly, but the screen on my phone stays black when I keep the setContentView(R.layout.activity_tuner), but when I add anything to Analyze.java and try to use it as a custom view it crashes. I've searched google multiple times trying to figure out how to get a custom view set up though it doesn't seem to work when I try some of the examples I've seen.

I know my java file Analyze doesn't have much in it and that is my custom view I would like to try and display my background image. I'm just confused on where to start to get it to display the image. I'm still fairly new to the whole idea of using onDraw any guidance would be appreciated.

My code is as follows:

activity_tuner.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:removed="@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"

	    android:layout_margin="5dp"/>
    <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> 





Tuner.java:



Tuner.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);
                back = new Analyze(this);
		setContentView(back);
		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];
}





Analyze.java:



Analyze.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){
		
		
	}


}

推荐答案

这篇关于如何在自定义视图中使用可绘制图像作为背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆