Android学习 画图 (一) 球

这篇博客介绍了如何在Android中画出一个球,并让这个球在碰到屏幕边界时能够反弹。作者提供了运行效果展示以及源代码下载链接,供读者实践和参考。相关资料包括API Demo和官方图形概述文档。
摘要由CSDN通过智能技术生成

1.画一个球,在遇到四周的墙壁会反弹。

package com.example.draw2d;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;

public class Draw2D extends Activity {
        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(new BallView(this));
    }



    /**
     * 定义一个View,用来显示球
     * @author siqi
     *
     */
    public class BallView extends View {
        private Ball ball = new Ball();
        private BallThread ballThread = new BallThread(this);
        
        public BallView(Context context) {
            super(context);
            
            /**
             * 启动线程
             */
            ballThread.start();
        }

        public Ball getBall() {
            return ball;
        }
        
        /**
         * onDraw在每次View被刷新的时候执行。
         */
        @Override
        protected void onDraw(Canvas canvas) {
            ball.draw(canvas);
            super.onDraw(canvas);
        }
    }
    

    /**
     * 定义一个线程来刷新View,处理球的运动
     * @author siqi
     *
     */
    public class BallThread extends Thread {
        /**
         * 当bAlive是false的时候退出线程
         */
        private boolean bAlive = true;
        
        private BallView ballView;
        
        /**
         * 球运动的速度
         */
        private int speed = 3;
        /**
         * x方向的运
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值