android控件重绘

android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用。

View的重绘——系统不会经常去调用View的OnDraw函数,为了能够在View上实现动画效果,比如说游戏(但好像很多游戏是用更高效的SurfaceView为实现的),在主线程是执行完程序的逻辑后,应该要调用postInvalidate(),通知系统去调用onDraw函数去重绘界面,才能将动画的效果给显示出来。

(如果在view类里面要调用onDraw函数,使用invalidate函数:this.invalidate();如果实在view类之外,ui线程之外使用:

private Runnable mRunnable = new Runnable() {
        //界面的主线程
        @Override
        public void run() {
            while( drawing )
            {
                try {
                    //更新球的位置信息
                    update();
                    //通知系统更新界面,相当于调用了onDraw函数
                    postInvalidate();
                    //界面更新的频率,这里是每30ms更新一次界面
                    Thread.sleep(30);
                    //Log.e(TAG, "drawing");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

网上找了个小例子:

http://blog.csdn.net/lxw1980/article/details/6031978

package cn.edu.wtu;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.View;
public class Main extends Activity {
    TheScreen mScreen;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //mScreen是自定义的View
        mScreen = new TheScreen(this);
        setContentView(mScreen);
    }
    
    //为避免在程序退出后线程仍在进行,造成不必要的系统资源浪费,在Activity退出是时候,主动将线程停止
    @Override
    public void onDestroy()
    {
        mScreen.stopDrawing();
        super.onDestroy();
    }
}
/**
 * 自定义的View类,为两个球的碰撞模拟
 * @author windy
 *
 */
class TheScreen extends View
{
    
    private static final String TAG = "Draw";
    //界面主线程的控制变量
    private boolean drawing = false;
    //储存当前已有的球的信息
    private ArrayList<Circle> circles;
    private Paint mPaint;
    //两个球的运动范围
    public static final int WIDTH = 300;
    public static final int HEIGHT = 400;
    public static final double PI = 3.14159265;
    Paint mPaint2 = new Paint();
    public TheScreen(Context context)
    {
        super(context);
        circles = new ArrayList<Circle>();
        //加入了两个球
        circles.add(new Circle());
        circles.add(new Circle(20, 30, 10));
        mPaint = new Paint();
        mPaint.setColor(Color.YELLOW);
        mPaint.setAntiAlias(true);
        mPaint2.setStyle(Style.STROKE);
        mPaint2.setColor(Color.RED);
        mPaint2.setAntiAlias(true);
        //启动界面线程,开始自动更新界面
        drawing = true;
        new Thread(mRunnable).start();
    }
    
    private Runnable mRunnable = new Runnable() {
        //界面的主线程
        @Override
        public void run() {
            while( drawing )
            {
                try {
                    //更新球的位置信息
                    update();
                    //通知系统更新界面,相当于调用了onDraw函数
                    postInvalidate();
                    //界面更新的频率,这里是每30ms更新一次界面
                    Thread.sleep(30);
                    //Log.e(TAG, "drawing");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    
    public void stopDrawing()
    {
        drawing = false;
    }
    
    
    @Override
    public void onDraw(Canvas canvas)
    {
        //在canvas上绘上边框
        canvas.drawRect(0, 0, WIDTH, HEIGHT, mPaint2);
        //在canvas上绘上球
        for( Circle circle : circles)
        {
            canvas.drawCircle(circle.x, circle.y, circle.radius, mPaint);
        }
    }
    
    //界面的逻辑函数,主要检查球是否发生碰撞,以及更新球的位置
    private void update()
    {
        if( circles.size()>1)
        {
            for( int i1=0; i1<circles.size()-1; i1++)
            {
                //当两个球发生碰撞,交换两个球的角度值
                for( int i2=i1+1; i2<circles.size(); i2++)
                    if( checkBumb(circles.get(i1),circles.get(i2)))
                    {
                        circles.get(i1).changeDerection(circles.get(i2));
                    }
            }
            
        }
        //更新球的位置
        for( Circle circle: circles)
            circle.updateLocate();
    }
    
    private boolean checkBumb(Circle c1, Circle c2)
    {
        return (c1.x-c2.x)*(c1.x-c2.x) + (c1.y-c2.y)*(c1.y-c2.y) <= (c1.radius+c2.radius)*(c1.radius+c2.radius);            
    }
    
    /**
     * 自定义的View的内部类,存储每一个球的信息
     * @author windy
     *
     */
    class Circle
    {
        float x=50;
        float y=70;
        double angle= (new Random().nextFloat())*2*PI;
        int speed=4;
        int radius=10;
        
        public Circle() {
        }
        
        public Circle( float x, float y, int r )
        {
            this.x = x;
            this.y = y;
            radius = r;
        }
        
        //利用三角函数计算出球的新位置值,当与边界发生碰撞时,改变球的角度
        public void updateLocate()
        {
            x = x+ (float)(speed *Math.cos(angle));
            //Log.v(TAG, Math.cos(angle)+"");
            y = y+ (float)(speed *Math.sin(angle));
            //Log.v(TAG, Math.cos(angle)+"");
            if( (x+radius)>=WIDTH )
            {
                if( angle >=0 && angle <= (PI/2))
                    angle = PI - angle;
                if( angle > 1.5 * PI && angle <= 2*PI)
                    angle = 3 * PI - angle;                
            }
            if( x-radius <=0 )
            {
                if( angle >= PI && angle <= 1.5*PI )
                    angle = 3*PI - angle;
                if( angle >= PI/2 && angle < PI)
                    angle = PI - angle;
            }
            if( y-radius<=0 || y+radius>=HEIGHT)
                angle = 2*PI - angle;
            
        }
        //两球交换角度
        public void changeDerection(Circle other)
        {
            double temp = this.angle;
            this.angle = other.angle;
            other.angle = temp;
        }
    }
}

结果:



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值