I'm trying to get two animations to show at the same time. The application builds successfully but crashes in the emulator.
I have created an Anim class in a thread that handles the Animations in a while loop. I think there is a problem with my draw method but I cant seem to figure it out.
I also want the animations to be clickable and get different sounds when the different animations are clicked.
I have successfully implemented SoundPool to an OnClickListener that I attached to the view because I can't seem to get an OnClickListener to each animation, but this obviously sets an OnClickListener to the entire screen so I get the sound effect playing no matter where the user taps the screen.
I appreciate any help on this, thank you
CODE
public class PlayActivity extends AppCompatActivity { private SoundPool soundPool; private int sound1; surfaceView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); view = new surfaceView(this); setContentView(view); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build(); soundPool = new SoundPool.Builder() .setMaxStreams(5) .setAudioAttributes(audioAttributes) .build(); sound1 = soundPool.load(this, R.raw.eagle, 1); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { soundPool.play(sound1, 1, 1, 0, 0, 1); } }); } public class surfaceView extends SurfaceView { public surfaceView(Context context) { super(context); new Anim().start(); } private class Anim extends Thread { int counter = 0; int counter2 = 0; @Override public void run() { long last_updated_time = 0; long delay = 250; int[] purple_bird = { R.drawable.bird1, R.drawable.bird2 }; int[] red_bird = { R.drawable.red1, R.drawable.red2, R.drawable.red3, R.drawable.red4 }; while (true) { boolean playing = true; if (playing) { long current_time = System.currentTimeMillis(); if (current_time > last_updated_time + delay) { if ((counter >= 2) && (counter2 >= 4)) { counter = 0; counter2 = 0; } draw(purple_bird[counter], red_bird[counter2]); last_updated_time = current_time; counter++; counter2++; } } } } private void draw(int red_bird, int purple_bird) { SurfaceHolder holder = getHolder(); Canvas canvas = holder.lockCanvas(); if (canvas != null) { canvas.drawColor(Color.WHITE); Paint paint = new Paint(); Bitmap purpleBird = BitmapFactory.decodeResource(getContext().getResources(), purple_bird); Bitmap redBird = BitmapFactory.decodeResource(getContext().getResources(), red_bird); Bitmap resizedRedBird = Bitmap.createScaledBitmap(redBird, (int) (redBird.getWidth() * 0.4), (int) (redBird.getHeight() * 0.4), true); Bitmap resizedPurpleBird = Bitmap.createScaledBitmap(purpleBird, (int) (purpleBird.getWidth() * 0.2), (int) (purpleBird.getHeight() * 0.2), true); canvas.drawBitmap(resizedRedBird, 500, 500, paint); canvas.drawBitmap(resizedPurpleBird, 100, 100, paint); holder.unlockCanvasAndPost(canvas); } } } } }
https://stackoverflow.com/questions/66702172/simultaneous-display-of-two-animations-in-surfaceview March 19, 2021 at 11:33AM
没有评论:
发表评论