Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stirante.mangareader;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.os.Handler;
- import android.os.Looper;
- import android.view.View;
- /**
- * Created by stirante
- */
- public class BallView extends View {
- Handler mHandler = new Handler(Looper.getMainLooper());
- Runnable mTick = new Runnable() {
- public void run() {
- invalidate();
- mHandler.postDelayed(this, 20); // 20ms == 60fps
- }
- };
- private Bitmap ballImage;
- private float x = 0;
- private float y = 0;
- private int dirX = 1;
- private int dirY = 1;
- private int speed = 8;
- private int width = -1;
- private int height = -1;
- public BallView(Context context) {
- super(context);
- ballImage = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_round);//tutaj zamien na jakies swoje obrazki
- startAnimation();
- }
- void startAnimation() {
- mHandler.removeCallbacks(mTick);
- mHandler.post(mTick);
- }
- void stopAnimation() {
- mHandler.removeCallbacks(mTick);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- if (width <= 0 && height <= 0) return;
- x += dirX * speed;
- y += dirY * speed;
- if (x >= width - ballImage.getWidth()) dirX = -1;
- if (x < 0) dirX = 1;
- if (y >= height - ballImage.getHeight()) dirY = -1;
- if (y < 0) dirY = 1;
- canvas.drawBitmap(ballImage, x, y, null);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- width = getMeasuredWidth();
- height = getMeasuredHeight();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement