一个动画可绘制对象的两个不同按钮

时间:2018-11-25 01:05:38

标签: android-studio

我的代码有问题,我在android studio上开发了游戏,并且遇到了以下问题。

我的应用程序上有两个按钮,一个“打孔”按钮和一个“踢动”按钮,以及一个用于对图像进行动画处理的Imageview。

当我单击“打孔”按钮时,将创建两个图像的可绘制动画 当我单击“踢”按钮时,我也会创建一个包含两个图像的可绘制动画。

问题是当我用两个可绘制的动画执行此代码时,应用程序崩溃了!

public class MainActivity extends AppCompatActivity {

private Button btnPunch;
private Button btnKick;
private ImageView imageView1 ;
private ImageView imageView2 ;

private AnimationDrawable[] animationDrawable = new 
AnimationDrawable[2];

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

btnPunch = (Button) findViewById(R.id.btnPunch);


imageView1 = (ImageView) 
findViewById(R.id.imageView1);

 imageView1.setBackgroundResource( 
         R.drawable.animationpunch);
animationDrawable[0] = (AnimationDrawable) 
imageView1.getBackground();

imageView2 = (ImageView) 
findViewById(R.id.imageView2);


imageView2.setBackground
Resource(R.drawable.animatio 
       nkick);
animationDrawable[1] = (AnimationDrawable) 
imageView2.getBackground();


btnPunch.setOnClickListener(new View.OnClickListener() 
{
@Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
}
});



btnKick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animationDrawable[0].stop()
animationDrawable[1].setOneShot(true);
animationDrawable[1].start();
}
});

}

@Override
public void onWindowFocusChanged(boolean hasFocus) 
{
super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
 }

</Code>

我还为两个动画使用了两个不同的imageview 另一方面,当我删除类似以下代码的动画时,效果很好,但是突然我只能使用打孔按钮,我希望它可以同时使用两个按钮

public class MainActivity extends AppCompatActivity {

private Button btnPunch;
private ImageView imageView1 ;

private AnimationDrawable[] animationDrawable = new 
AnimationDrawable[2];

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

btnPunch = (Button) findViewById(R.id.btnPunch);


imageView1 = (ImageView) 
findViewById(R.id.imageView1);


imageView1.setBackground
Resource(R.drawable.animatio 
       npunch);
animationDrawable[0] = (AnimationDrawable) 
imageView1.getBackground();


 btnPunch.setOnClickListener(new 
View.OnClickListener() {
@Override
public void onClick(View v) {
animationDrawable[1].stop();
animationDrawable[0].setOneShot(true);
animationDrawable[0].start();
} 

}}


}

@Override
public void onWindowFocusChanged(boolean hasFocus) 
{

super.onWindowFocusChanged(hasFocus);
//animationDrawable.start();
}
} 

public class MainActivity extends AppCompatActivity { private Button btnPunch; private ImageView imageView1 ; private AnimationDrawable[] animationDrawable = new AnimationDrawable[2]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnPunch = (Button) findViewById(R.id.btnPunch); imageView1 = (ImageView) findViewById(R.id.imageView1); imageView1.setBackground Resource(R.drawable.animatio npunch); animationDrawable[0] = (AnimationDrawable) imageView1.getBackground(); btnPunch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animationDrawable[1].stop(); animationDrawable[0].setOneShot(true); animationDrawable[0].start(); } }} } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); //animationDrawable.start(); } }

单击“打孔”按钮,可以动画显示我的动画 我点击踢按钮,它使我的动画踢动画 帮助吗?

0 个答案:

没有答案