android控制項重繪

來源:互聯網
上載者:User

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;
        }
        
        //利用三角Function Compute出球的新位置值,當與邊界發生碰撞時,改變球的角度
        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;
        }
    }
}

結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.