android 小球碰撞动画,android练习:碰撞的小球

这篇博客介绍了一个使用Android SurfaceView实现的小球碰撞动画练习。通过创建GameView类,利用SurfaceHolder和线程来更新小球的位置,并处理边界及横杆的碰撞效果。文章包含关键代码示例,适合初学者学习。
摘要由CSDN通过智能技术生成

先看图:

4738ef60031663d30dcffbcc6dbcda54.png

小球碰到边上反弹,下面的横杆接住小球让其再反弹上去。很简单,大家都见过的啦。

这里主要练习SurfaceView,Callback以及在SurfaceView中使用线程,看主要的View类:

package com.wt.app;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.Paint.FontMetrics;

import android.util.Log;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.SurfaceHolder.Callback;

public class GameView extends SurfaceView implements Callback,Runnable{

private static final String TAG="GameView";

SurfaceHolder holder=null;

int x,y=0;

int speedX=0,speedY=0;

int ball_x,ball_y=0;

int ball_speedX=5,ball_speedY=5;

int r=5;

public GameView(Context context) {

super(context);

// TODO Auto-generated constructor stub

holder=this.getHolder();

holder.addCallback(this);

this.setFocusable(true);

}

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated meth

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值