gpt4 book ai didi

java - 单击按钮时使用动画吗?

转载 作者:行者123 更新时间:2023-12-01 09:37:10 26 4
gpt4 key购买 nike

我正在尝试使用这个名为 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();

}
}

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

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

如果我删除小爆炸动画,按钮和 Activity 更改就可以正常工作。我该怎么做才能让动画正常工作?我已将库添加到 Gradle 文件中。

最佳答案

工作代码

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();
}
});
}
}

关于java - 单击按钮时使用动画吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38788321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com