首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >启动/停止可绘制传感器事件的动画

启动/停止可绘制传感器事件的动画
EN

Stack Overflow用户
提问于 2016-04-11 20:10:59
回答 2查看 465关注 0票数 0

我正在使用google 这里编辑发布的文档所能理解的内容:我制作了第二个带有动画的.xml (称为shake_animation),并更新了主java类中的引用。

我认为它们的问题现在可能出现在主java类中,可能是图像视图和frameAnimation之间的某种冲突?

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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文件,我认为这可能是问题所在,但是google发布的页面并没有很好地描述应该放在这里的内容

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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(更新):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-11 20:18:17

将动画与布局代码分离是很重要的。

将动画列表放在可绘图文件夹中名为anime.xml的xml文件中,如下所示

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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代码使用以下代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    ImageView img = (ImageView) findViewById(R.id.shake1);
    img.setBackgroundResource(R.drawable.anime);
    AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
票数 0
EN

Stack Overflow用户

发布于 2016-04-11 21:05:03

您将得到以下错误:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference

这意味着您既没有初始化ImageView,也没有在setBackgroundResource(int)中传递的参数为null。粘贴活动代码以获得更多的错误清除。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36563492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文