我的问题:
我一直试图制作一个应用,根据方向的变化,球会反弹。我遇到了一些问题所以我以不同的方式开始了这个项目。当我使用箭头键时,球向上/向下/向右/向左移动。球本身移动得很好,屏幕上可以看到文字(至少我一眼就看到了!)但是我不能在屏幕上同时看到这两个。
每次我将BouncingBall添加到activity_main.xml时,应用程序都会开始崩溃,屏幕上看不到任何内容。当我从activity_main.xml中删除BouncingBall时,我再次看到了球,也可以移动它但看不到文字。什么似乎是军官,问题?我的大脑不再正常工作了,代码中可能会有一些我不知道的东西。
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.maija.pomppupallo.MainActivity">
<com.example.maija.pomppupallo.BouncingBallView
android:id="@+id/bouncingBallView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/hellou"
android:layout_width="195dp"
android:layout_height="70dp"
android:text="@string/hello"
android:textColor="@color/colorAccent"
android:textSize="30sp"
android:translationZ="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textorientation"
android:layout_width="287dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="64dp"
android:text="@string/orientation"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp"
android:translationZ="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
主要Activity.java :
package com.example.maija.pomppupallo;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv1, tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
View BouncingBallView = new BouncingBallView(this);
setContentView (BouncingBallView);
}
}
BouncingBallView.java :
package com.example.maija.pomppupallo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.KeyEvent;
import android.view.View;
public class BouncingBallView extends View {
private int xMin = 0;
private int xMax;
private int yMin = 0;
private int yMax;
private float ballRadius = 130;
private float ballX = ballRadius + 20;
private float ballY = ballRadius + 40;
private float ballSpeedX = 0;
private float ballSpeedY = 0;
private RectF ballBounds;
private Paint paint;
public BouncingBallView(Context context) {
super (context);
ballBounds = new RectF();
paint = new Paint();
this.setFocusable(true);
this.requestFocus();
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_RIGHT:
ballSpeedY=0;
if (ballSpeedX > 0) {
;
}
else ballSpeedX = 20;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
ballSpeedY=0;
if (ballSpeedX < 0) {
;
}
else ballSpeedX = -20;
break;
case KeyEvent.KEYCODE_DPAD_UP:
ballSpeedX=0;
if (ballSpeedY < 0) {
;
}
else ballSpeedY = -20;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
ballSpeedX=0;
if (ballSpeedY > 0) {
;
}
else ballSpeedY = 20;
break;
case KeyEvent.KEYCODE_DPAD_CENTER:
ballSpeedX=0;
ballSpeedY=0;
break;
case KeyEvent.KEYCODE_A:
float maxRadius = (xMax>yMax) ? yMax/2*0.9f : xMax/2*0.9f;
if (ballRadius < maxRadius) {ballRadius*=1.05;}
break;
case KeyEvent.KEYCODE_Z:
if (ballRadius > 20){ballRadius*=0.95;}
break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
ballBounds.set (ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor (Color.CYAN);
canvas.drawOval (ballBounds, paint);
update();
try {
Thread.sleep (30);
}catch (InterruptedException e){}
invalidate ();
}
private void update(){
ballX+=ballSpeedX;
ballY+=ballSpeedY;
if(ballX+ballRadius > xMax){
ballSpeedX = -ballSpeedX;
ballX = xMax-ballRadius;
}else if (ballX-ballRadius < xMin){
ballSpeedX = -ballSpeedX;
ballX = xMin+ballRadius;
}
if(ballY+ballRadius > yMax){
ballSpeedY = -ballSpeedY;
ballY = yMax-ballRadius;
}else if (ballY-ballRadius < yMin){
ballSpeedY = -ballSpeedY;
ballY = yMin+ballRadius;
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
xMax = w-1;
yMax = h-1;
}
}
AndroidOrientationSensor.java :
package com.example.maija.pomppupallo;
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidOrientationSensor extends Activity{
TextView textviewOrientation, tv1, tv2;
OrientationEventListener myOrientationEventListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textviewOrientation = (TextView)findViewById(R.id.textorientation);
tv1 = (TextView) findViewById (R.id.textorientation);
tv2 = (TextView) findViewById (R.id.hellou);
myOrientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
@Override
public void onOrientationChanged(int arg0) {
// TODO Auto-generated method stub
textviewOrientation.setText("Orientation is: " + String.valueOf(arg0));
}};
if (myOrientationEventListener.canDetectOrientation()){
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
myOrientationEventListener.enable();
}
else{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myOrientationEventListener.disable();
}
}
我希望有人明白这里的问题。
注意:传感器文件还没有准备好,但有一些错误。
答案 0 :(得分:0)
Remove below lines from your code as you already add BouncingBallView in xml.
View BouncingBallView = new BouncingBallView(this);
setContentView (BouncingBallView);