2011-04-23 86 views
0

我現在有一個問題,正在繪製圓形和處理觸摸事件。基本上我正在聆聽觸摸事件,它選擇一個圓圈並讓用戶拖動來控制圓圈的位置/半徑。我也有一個菜單選項來「添加一個新的圈子」,它改變了拖動事件,以添加一個新的圈子來查看。當我測試它時,這會繼續拋出一個力量。我不知道如何看看是什麼導致了力量關閉(抱歉,我是一個剛好擅長破解事情並使其工作的新手)。有人可以看看這個,或者指出我正確調試這個方向嗎?Android:圓形圖形和觸摸事件錯誤

我認爲這可能與在當前焦點的視圖內創建的新圓形有關?如果那有意義的話。

這是我的代碼...初始值來自以前的活動(這不是問題所在,但問題出在我的TouchEventListener中)。

/*this is the working example of playing audio 
* and adjusting the volume, although playback 
* is a bit choppy and the different sounds loose 
* sync after a bit 
*/ 

package com.adam.PlaySound; 

import java.util.ArrayList; 
import java.util.Collections; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.AbsoluteLayout; 
import android.widget.TextView; 

@SuppressWarnings("deprecation") 
public class EditInterface extends Activity { 
/** Called when the activity is first created. */ 

/*do our web stuff*/ 
ConnectToUrl makeConnection = new ConnectToUrl(); 

TextView text; 
AbsoluteLayout circlesView; 
int numPlayers; 
ArrayList<Float> regionX = new ArrayList<Float>(); 
ArrayList<Float> regionY = new ArrayList<Float>(); 
ArrayList<Float> regionR = new ArrayList<Float>(); 
ArrayList<String> soundFiles = new ArrayList<String>(); 
ArrayList<LoopRegion> region = new ArrayList<LoopRegion>(); 
ArrayList<LoopRegion> border = new ArrayList<LoopRegion>(); 
MediaPlayer mediaPlayer = new MediaPlayer(); 
String key; 
int index; 
int actionState = 0; 

//We can be in one of these 3 states 
static final int NONE = 0; 
static final int DRAG = 1; 
static final int ZOOM = 2; 
int mode = NONE; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.edit); 

    text = (TextView) findViewById(R.id.text); 
    text.setText(""); 
    circlesView = (AbsoluteLayout) findViewById(R.id.absoluteLayout1); 
    circlesView.setOnTouchListener(Motion); 

    Bundle extras = getIntent().getExtras(); 
    if(extras != null) { 
     numPlayers = extras.getInt("numPlayers"); 
    } 

    for (int i = 0; i < numPlayers; i++){ 
     //get regionX values 
     key = "regionX" + i; 
     regionX.add(extras.getFloat(key)); 
     //get regionY values 
     key = "regionY" + i; 
     regionY.add(extras.getFloat(key)); 
     //get regionR values 
     key = "regionR" + i; 
     regionR.add(extras.getFloat(key)); 
     //get soundFiles values 
     key = "soundFiles" + i; 
     soundFiles.add(extras.getString(key)); 

     //add our circles 
     region.add(new LoopRegion(this,regionX.get(i),regionY.get(i),regionR.get(i), true, Color.WHITE)); 
     border.add(new LoopRegion(this,regionX.get(i),regionY.get(i),regionR.get(i), false, Color.RED)); 
     circlesView.addView(region.get(i)); 
     circlesView.addView(border.get(i)); 

    } 

} 

OnTouchListener Motion 
= new AbsoluteLayout.OnTouchListener(){ 
@Override 
    public boolean onTouch(View v, MotionEvent ev) { 
     float x1 = ev.getX(0); 
     float y1 = ev.getY(0); 
     float x2 = ev.getX(1); 
     float y2 = ev.getY(1); 
     final int action = ev.getAction(); 
     switch (action & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
      if (actionState == 0){ 
       /***select our circle***/ 
       mode = DRAG; 
       //for testing//text.setText("action down"); 

       //calculate distance to detect which circle we will look at 
       ArrayList<Float> distance = new ArrayList<Float>(); 
       for (int i = 0; i < numPlayers; i++){ 
        if (distance(regionX.get(i), regionY.get(i), x1, y1) > (regionR.get(i) + 5)){ 
         distance.add((float) 10000); 
        } 
        else { 
         distance.add(distance(regionX.get(i), regionY.get(i), x1, y1)); 
        } 
       } 
       Object object = Collections.min(distance); 
       index = distance.indexOf(object); 

       //for testing//text.setText("index: " + index); 

       //set up sound 
       String path = "http://soundclusters.adamlaskowitz.com/uploads/" + soundFiles.get(index); 
       mediaPlayer = MediaPlayer.create(EditInterface.this, Uri.parse(path)); 
       mediaPlayer.start(); 

       region.get(index).setColor(true, Color.RED); 
       region.get(index).invalidate(); 
       border.get(index).setColor(false, Color.WHITE); 
       border.get(index).invalidate(); 
      } 
      else if (actionState == 1){ 
       region.add(new LoopRegion(EditInterface.this, x1, y1, 50 , true, Color.WHITE)); 
       border.add(new LoopRegion(EditInterface.this, x1, y1, 50, false, Color.RED)); 

       circlesView.addView(region.get(numPlayers)); 
       circlesView.addView(border.get(numPlayers)); 
       circlesView.clearFocus(); 
       /////region.get(index).invalidate(); 
      /////border.get(index).invalidate(); 
       index = numPlayers; //since they are zero-indexed the previous 
       //amount of Players will equal the index of the new larger array 
       numPlayers = region.size(); 
       text.setText("length:" + numPlayers + ", state:" + actionState); 
       //text.setText("index:" + index + ", numPlayers:" + numPlayers + ", length:" + region.size()); 
      } 

      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      /***initiate zoom***/ 
      mode = ZOOM; 
      //for testing//text.setText("action 2 down"); 

      break; 

     case MotionEvent.ACTION_UP: 
      /***finish our current edit and deselect circle***/ 
      mode = NONE; 
      //for testing//text.setText("action up"); 

      //stop and release current sound for region 
      mediaPlayer.stop(); 
      mediaPlayer.release(); 

      region.get(index).setColor(true, Color.WHITE); 
      region.get(index).invalidate(); 
      border.get(index).setColor(false, Color.RED); 
      border.get(index).invalidate(); 

      actionState = 0; 

      text.setText("index:" + index + ", state:" + actionState); 

      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      /***set mode Drag***/ 
      mode = DRAG; 
      //for testing//text.setText("action 2 up"); 

      break; 

     case MotionEvent.ACTION_MOVE:    
      if (mode == DRAG){ 
       /***Drag the circle based on radius***/ 
       //for testing//text.setText("action move"); 
       region.get(index).setCoordinate(x1, y1, regionR.get(index)); 
       region.get(index).invalidate(); 
       border.get(index).setCoordinate(x1, y1, regionR.get(index)); 
       region.get(index).invalidate(); 
       regionX.set(index, x1); 
       regionY.set(index, y1); 

      } 
      else if (mode == ZOOM) { 
       /***Zoom and move the circle***/ 
       //for testing//text.setText("action 2 move"); 
       regionR.set(index, distance(x1,y1,x2,y2)); 
       float centerX = midpoint(x1,x2); 
       float centerY = midpoint(y1,y2); 
       float radius = regionR.get(index)/2; 
       region.get(index).setCoordinate(centerX, centerY, radius); 
       region.get(index).invalidate(); 
       border.get(index).setCoordinate(centerX, centerY, radius); 
       region.get(index).invalidate(); 
       regionX.set(index, centerX); 
       regionY.set(index, centerY); 
       regionR.set(index, radius); 
      } 
      break; 
     } 
     return true; 
    } 
}; 

//make our menu--------------------------------------------------------- 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.edit_menu, menu); 
    return true; 
} 

public float regionXNew[] = new float[numPlayers]; 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 
    case R.id.play: 
     //return to the GraphicInterface---------------------------------------- 
     Intent mIntent = new Intent(EditInterface.this, GraphicInterface.class); 
     String num = Integer.toString(numPlayers); 
     String xVal = ""; 
     String yVal = ""; 
     String rVal = ""; 
     String files = ""; 
     for (int i = 0; i < numPlayers; i++){ 
      //format values to send to web*/ 
      if (i == (numPlayers - 1)){ 
       xVal = xVal.concat(regionX.get(i) + ""); 
       yVal = yVal.concat(regionY.get(i) + ""); 
       rVal = rVal.concat(regionR.get(i) + ""); 
       files = files.concat(soundFiles.get(i) + ""); 
      } 
      else { 
       xVal = xVal.concat(regionX.get(i) + "="); 
       yVal = yVal.concat(regionY.get(i) + "="); 
       rVal = rVal.concat(regionR.get(i) + "="); 
       files = files.concat(soundFiles.get(i) + "="); 
      } 
     } 

     makeConnection.sendToWeb(num, xVal, yVal, rVal, files); 

     if (getParent() == null) { 
      setResult(RESULT_OK, mIntent); 
     } else { 
      getParent().setResult(RESULT_OK, mIntent); 
     } 
     finish(); 
     //return to the GraphicInterface--------------------------------------- 
     return true; 
    case R.id.help: 

     return true; 
    case R.id.add: 
     actionState = 1; 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 
//make our menu--------------------------------------------------------- 

public float distance(float x1, float y1, float x2, float y2){ 
    float dist; 
    float a = x1 - x2; 
    float b = y1 - y2; 

    dist = (float) (Math.pow(a, 2) + Math.pow(b, 2)); 
    dist = (float) Math.sqrt(dist); 

    return dist; 
} 

public float midpoint(float axis1, float axis2){ 
    float midpoint; 
    midpoint = (axis1 + axis2)/2; 
    return midpoint; 
} 

} 

謝謝你的迴應。

回答

0

你需要開發Android應用程序時要學會的第一件事就是閱讀logcathttp://developer.android.com/guide/developing/tools/adb.html#logcat

請,您的設備連接到PC,並運行adb logcat *:W > log.txt
讀取輸出,並查找拋出的任何異常。然後,在這裏粘貼代碼,以便我們更好地瞭解發生了什麼。 PS:如果您正在從Eclipse進行模擬器測試,則android調試透視圖將爲您打開一個包含logcat的窗口。用它來複制相關文本。它將是完整的logcat,因此它將具有比上述命令行更多的非相關輸出。