Advertisement
Guest User

How to add a Drawable onClick / onTouch Listener?

a guest
Nov 1st, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Random;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.graphics.Canvas;
  8. import android.graphics.drawable.Drawable;
  9. import android.os.Bundle;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.view.animation.Animation;
  13. import android.view.animation.Interpolator;
  14. import android.view.animation.LinearInterpolator;
  15. import android.view.animation.TranslateAnimation;
  16. import android.widget.Toast;
  17.  
  18. public class SnowFall extends Activity {
  19.  
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         SnowFallView snowFallView = new SnowFallView(this);
  24.         setContentView(snowFallView);
  25.         snowFallView.setBackgroundDrawable(getResources().getDrawable(R.drawable.winter));
  26.     }
  27.  
  28.     private class SnowFallView extends View {
  29.         private int snow_flake_count = 10;
  30.         private final List<Drawable> drawables = new ArrayList<Drawable>();
  31.         private int[][] coords;
  32.         private final Drawable snow_flake;
  33.  
  34.         public SnowFallView(Context context) {
  35.             super(context);
  36.             setFocusable(true);
  37.             setFocusableInTouchMode(true);
  38.  
  39.             snow_flake = context.getResources().getDrawable(R.drawable.snow_flake);
  40.             snow_flake.setBounds(0, 0, snow_flake.getIntrinsicWidth(), snow_flake
  41.                     .getIntrinsicHeight());
  42.         }
  43.  
  44.         @Override
  45.         protected void onSizeChanged(int width, int height, int oldw, int oldh) {
  46.             super.onSizeChanged(width, height, oldw, oldh);
  47.             Random random = new Random();
  48.             Interpolator interpolator = new LinearInterpolator();
  49.  
  50.             snow_flake_count = Math.max(width, height) /100;
  51.             coords = new int[snow_flake_count][];
  52.             drawables.clear();
  53.             for (int i = 0; i < snow_flake_count; i++) {
  54.                 Animation animation = new TranslateAnimation(20, height / 10
  55.                         - random.nextInt(height / 5), -65, height + 30);
  56.                 animation.setDuration(10 * height + random.nextInt(5 * height));
  57.                 animation.setRepeatCount(-1);
  58.                 animation.initialize(10, 10, 10, 10);
  59.                 animation.setInterpolator(interpolator);
  60.  
  61.                 coords[i] = new int[] { random.nextInt(width - 30), -30 };
  62.                 drawables.add(new AnimateDrawable(snow_flake, animation));
  63.  
  64.                 animation.setStartOffset(random.nextInt(20 * height));
  65.                 animation.startNow();
  66.             }
  67.         }
  68.  
  69.  
  70.         @Override
  71.         protected void onDraw(Canvas canvas) {
  72.             for (int i = 0; i < snow_flake_count; i++) {
  73.                 Drawable drawable = drawables.get(i);
  74.                 canvas.save();
  75.                 canvas.translate(coords[i][0], coords[i][1]);
  76.                 drawable.draw(canvas);
  77.                 canvas.restore();
  78.             }
  79.             invalidate();
  80.         }
  81.  
  82.  
  83.         @Override
  84.         public boolean onTouchEvent(MotionEvent event) {
  85.             //  --->  ????
  86.             return super.onTouchEvent(event);
  87.         }
  88.  
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement