android - for循环绘图圈不工作?

时间:2011-04-15 04:40:36

标签: android draw

我正在尝试从数组中绘制多个圆圈,当我尝试运行代码时它会抛出“强制关闭”。这里有什么想法吗?

package com.adam.PlaySound;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ButtonPress extends Activity {
    /** Called when the activity is first created. */

    int numPlayers = 3;
    boolean toggle = true;
    LinearLayout parent;
    Button button;
    TextView text;
    TextView text2;
    DisplayMetrics dm = new DisplayMetrics(); 
    MediaPlayer mediaPlayer[] = new MediaPlayer[numPlayers];
    //this array is a placeholder for the loops in a cluster
    //essentially this array will be populated through a database
    //based on which cluster someone is within
    //and the region arrays will be populated by the same database
    String soundFiles[] = {"drums_1.mp3", "lead.mp3", "strings.mp3"};
    public LoopRegion region[] = new LoopRegion[numPlayers];
    float regionX[] = {20, 80, 150};
    float regionY[] = {20, 200, 350};
    int regionR[] = {50, 80, 100};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //setContentView(R.id.view1);

        parent = (LinearLayout) findViewById(R.id.parent);
        button = (Button) findViewById(R.id.button1);
        text = (TextView) findViewById(R.id.text);
        text2 = (TextView) findViewById(R.id.text2);
        parent.setOnTouchListener(Motion);
        button.setOnClickListener(on_off);


        for (int i = 0; i < numPlayers; i++){
            String path = "http://soundclusters.adamlaskowitz.com/uploads/" + soundFiles[i];
            mediaPlayer[i] = new  MediaPlayer();
            //mediaPlayer[i] = MediaPlayer.create(ButtonPress.this, R.raw.drums_2);
            mediaPlayer[i] = MediaPlayer.create(ButtonPress.this, Uri.parse(path));
            mediaPlayer[i].setLooping(true);
        }

        for (int i = 0; i < numPlayers; i++){
            region[i] = new LoopRegion(this,regionX[i],regionY[i],regionR[i]);
            parent.addView(region[i]);
        }

        getWindowManager().getDefaultDisplay().getMetrics(dm); 


    }

这是LoopRegion类。

package com.adam.PlaySound;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;


/*This class will create a circle region for
 *each loop within a cluster */
public class LoopRegion extends View {
    private float x;
    private float y;
    private int r;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

public LoopRegion(Context context, float x, float y, int r) {
    super(context);
    mPaint.setColor(0xFFFF0000);
    this.x = x;
    this.y = y;
    this.r = r;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(x, y, r, mPaint);
}

public void setCoordinate(float mx, float my){
    this.x = mx;
    this.y = my;
    //this.r = 300;
    }
}

这是XML

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/parent"
    >
<TextView android:id="@+id/text"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Not Playing"
    />
<TextView android:id="@+id/text2" 
    android:layout_height="wrap_content" 
    android:text="Location" android:layout_width="wrap_content"
    />
<Button android:text="Sound On/Off" 
    android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content">
    </Button>
<!-- <View android:id="@+id/view1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    </View>-->

</LinearLayout>

提前致谢!

1 个答案:

答案 0 :(得分:0)

我的猜测是你的LoopRegion视图将布局参数设置为FILL_PARENT,第一个将其他视图推离屏幕。这将有助于查看如何创建LoopRegion的代码,以及可能是父LinearLayout的XML。