我使用了我对谷歌here发布的文档的理解 编辑:我用其中的动画制作了第二个.xml(称为shake_animation)并更新了主java类中的引用
我认为他们的问题现在可能在主java类中,也许在图像视图和frameAnimation之间发生了某种冲突?
package shake.shake;
/**
* Created by ink on 3/24/16.
*/
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class main extends Activity implements SensorEventListener {
private float mLastX, mLastY, mLastZ;
private boolean mInitialized;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private final float NOISE = (float) 4.0;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInitialized = false;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// can be safely ignored for this demo
}
@Override
public void onSensorChanged(SensorEvent event) {
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(R.id.shake1);
img.setBackgroundResource(R.drawable.shake_animation);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
TextView tvX= (TextView)findViewById(R.id.x_axis);
TextView tvY= (TextView)findViewById(R.id.calories);
TextView tvZ= (TextView)findViewById(R.id.z_axis);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if (!mInitialized) {
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText("0.0");
tvY.setText("0.0");
tvZ.setText("0.0");
mInitialized = true;
} else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
if (deltaX < NOISE) deltaX = (float)0.0;
if (deltaY < NOISE) deltaY = (float)0.0;
if (deltaZ < NOISE) deltaZ = (float)0.0;
mLastX = x;
mLastY = y;
mLastZ = z;
tvX.setText(Float.toString(deltaX));
tvY.setText(Float.toString(deltaY));
tvZ.setText(Float.toString(deltaZ));
if (deltaX > deltaY) {
frameAnimation.start();
} else if (deltaY > deltaX) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
}
}
这是我的xml文件,我想这可能就是问题出在哪里了,但谷歌推出的页面并没有对这里放置什么有如此好的描述
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/shake1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/shake1" />
<item
android:duration="50"
android:drawable="@drawable/shake1"/>
<item
android:duration="50"
android:drawable="@drawable/shake1"/>
<item
android:duration="50"
android:drawable="@drawable/shake2"/>
<item
android:duration="50"
android:drawable="@drawable/shake3"/>
<item
android:duration="50"
android:drawable="@drawable/shake4"/>
</selector>
这是logcat(更新):
04-12 01:18:57.824 2081-2081/shake.shake E/AndroidRuntime: FATAL EXCEPTION: main
Process: shake.shake, PID: 2081
java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
at shake.shake.main.onSensorChanged(main.java:62)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:481)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是可绘制列表.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<animation-list android:id="@+id/shake_animation" android:oneshot="false">
<item android:drawable="@drawable/shake1" android:duration="50" />
<item android:drawable="@drawable/shake2" android:duration="50" />
<item android:drawable="@drawable/shake3" android:duration="50" />
<item android:drawable="@drawable/shake4" android:duration="50" />
</animation-list>
<image
android:id="@+id/shake1"
android:src="@drawable/shake1"
/>
</selector>
答案 0 :(得分:0)
将动画与布局代码分开非常重要。
将动画列表放在drawable文件夹中名为anime.xml的xml文件中,如下所示
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/shake1" android:duration="50" />
<item android:drawable="@drawable/shake2" android:duration="50" />
<item android:drawable="@drawable/shake3" android:duration="50" />
<item android:drawable="@drawable/shake4" android:duration="50" />
</animation-list>
让你的java代码使用以下
ImageView img = (ImageView) findViewById(R.id.shake1);
img.setBackgroundResource(R.drawable.anime);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
答案 1 :(得分:0)
您收到此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
这意味着您没有初始化ImageView
或者您传递的参数setBackgroundResource(int)
为空。粘贴活动代码以获得更多错误清除。