2014-05-11 19 views
1

我想實現一個自定義JoystickView爲我的項目,並通過我的搜索,我發現這個庫:https://github.com/zerokol/JoystickView定製joystickView不XML工作 - Android電子

我添加的jar到lib文件夾,將jar文件添加到構建路徑。問題是,當我嘗試將它添加到我的屏幕上時,它不起作用。我是Android新手,現在卡住了。你能告訴我我錯了嗎?

以下代碼是在除了XML文件中的鏈接的示例代碼相同

我的主要類: 包com.example.joystickdeneme;

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.TextView; 

import com.zerokol.views.JoystickView; 
import com.zerokol.views.JoystickView.OnJoystickMoveListener; 

public class MainActivity extends Activity { 



private TextView angleTextView; 
private TextView powerTextView; 
private TextView directionTextView; 
// Importing as others views 
private JoystickView joystick; 

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

    angleTextView = (TextView) findViewById(R.id.angleTextView); 
    powerTextView = (TextView) findViewById(R.id.powerTextView); 
    directionTextView = (TextView) findViewById(R.id.directionTextView); 
    // referring as others views 
    joystick = (JoystickView) findViewById(R.id.joystickView); 

    // Listener of events, it'll return the angle in graus and power in percents 
    // return to the direction of the moviment 
    joystick.setOnJoystickMoveListener(new OnJoystickMoveListener() { 
      @Override 
      public void onValueChanged(int angle, int power, int direction) { 
       angleTextView.setText(" " + String.valueOf(angle) + "°"); 
       powerTextView.setText(" " + String.valueOf(power) + "%"); 
       switch (direction) { 
        case JoystickView.FRONT: 
         directionTextView.setText(R.string.front_lab); 
         break; 

        case JoystickView.FRONT_RIGHT: 
         directionTextView.setText(R.string.front_right_lab); 
         break; 

        case JoystickView.RIGHT: 
         directionTextView.setText(R.string.right_lab); 
          break; 

        case JoystickView.RIGHT_BOTTOM: 
         directionTextView.setText(R.string.right_bottom_lab); 
         break; 

        case JoystickView.BOTTOM: 
         directionTextView.setText(R.string.bottom_lab); 
         break; 

        case JoystickView.BOTTOM_LEFT: 
         directionTextView.setText(R.string.bottom_left_lab); 
         break; 

        case JoystickView.LEFT: 
         directionTextView.setText(R.string.left_lab); 
         break; 

        case JoystickView.LEFT_FRONT: 
         directionTextView.setText(R.string.left_front_lab); 
         break; 

        default: 
         directionTextView.setText(R.string.center_lab); 
       } 
      } 
    }, JoystickView.DEFAULT_LOOP_INTERVAL); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

和我的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
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=".MainActivity" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" 
    android:id="@+id/angleTextView" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" 
    android:id="@+id/powerTextView" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" 
    android:id="@+id/directionTextView" /> 


<com.example.joystickdeneme 

    zerokol.views.joystickview=".zerokol.views.joystickview" 
    android:id="@+id/joystickView" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" > 
</com.example.joystickdeneme> 

</LinearLayout> 

回答

1

我瞭解到在我的錯誤是在佈局文件,其中我所說的操縱桿應該是:<com.zerokol.views.JoystickView android:id="@+id/joystickView" android:layout_width="150dp" android:layout_height="150dp" />