点击按钮时使用动画?

时间:2016-08-05 11:36:58

标签: java android animation

我正在尝试使用名为SmallBang的这个很酷的动画 在我的图像按钮上。以下是我的代码现在的样子:

import xyz.hanks.library.SmallBang;

public class MainActivity extends AppCompatActivity {

private SmallBang smallBang;
private ImageButton imageButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    smallBang = SmallBang.attach2Window(this);
    smallBang.bang((ImageButton) findViewById(R.id.imageButton));
}

public void onSettingsButtonClick(View view) {
    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
    startActivity(intent);
    finish();

}
}

我想使用该动画然后更改为另一个活动。当我运行此代码时,我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference

如果我删除了smallbang动画,按钮和活动更改工作正常。我该怎么做才能让动画有效?我已将库添加到Gradle文件中。

2 个答案:

答案 0 :(得分:1)

工作代码

public class MainActivity extends Activity {

    private SmallBang mSmallBang;
    private ImageView ImageButton;

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

        mSmallBang = SmallBang.attach2Window(this);

        ImageButton = (ImageView) findViewById(R.id.imageButton);
        ImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mSmallBang.bang(v);
                mSmallBang.setmListener(new SmallBangListener() {
                    @Override
                    public void onAnimationStart() {
                    }

                    @Override
                    public void onAnimationEnd() {

                    }
                });
            }
        });
    }
}

<强> activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/image_1">


    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

更新您的代码修改:

public class MainActivity extends Activity {

    private SmallBang mSmallBang;
    private ImageView ImageButton;

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

        mSmallBang = SmallBang.attach2Window(this);
        ImageButton = (ImageView) findViewById(R.id.imageButton);
    }

    public void onSettingsButtonClick(View view) {
        mSmallBang.bang(view);
        mSmallBang.setmListener(new SmallBangListener() {
            @Override
            public void onAnimationStart() {
            }

            @Override
            public void onAnimationEnd() {
                Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}

答案 1 :(得分:0)

尝试这种方式

import xyz.hanks.library.SmallBang;

public class MainActivity extends AppCompatActivity {

private SmallBang smallBang;
private ImageButton imageButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    smallBang = SmallBang.attach2Window(this);
    imageButton = (ImageButton) findViewById(R.id.imageButton);
    smallBang.bang(view,new SmallBangListener() {
    @Override
    public void onAnimationStart() {
    }

    @Override
    public void onAnimationEnd() {
        onSettingsButtonClick(imageButton);
    }
    });
}

public void onSettingsButtonClick(View view) {
    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
    startActivity(intent);
    finish();

}
}