Blackberry [Tutorial] Cách tạo Animated Image

Thảo luận trong 'Lập trình cho Di động' bắt đầu bởi Keeper, 13/9/10.

  1. Keeper Tiểu nhị

    Để display ảnh động trong app của BB cũng khá phức tạp vì nó ko tự display ảnh GIF 1 cách đúng đắng nhất.

    Ví dụ trong facebook ta có dạng loading như hình sau:

    [IMG]

    Để làm được giống vậy ta sẽ phải chuẩn bị 1 hình có nhiều frame(mỗi frame thể hiện trạng thái động khác nhau) Mỗi frame có kích thước chính xác bằng nhau và trải các frame ra thành 1 dãy như sau:

    [IMG]

    Đoạn code sau sẽ load hình trên và quét các frame với tần số và số frame cần quét như ta set trong code:

    Code:
    /*
     * ProgressAnimationField.java
     *
     * © tinhte.vn, 2010
     * Confidential and proprietary.
     * 
     * Developer: Keeper
     * Email: louis.keeper@[U][URL="http://www.tinhte.vn/member.php?u=362321"]Gmail.Com[/URL][/U]
     */
    
    package Keeper;
    
    import net.rim.device.api.system.*;
    import net.rim.device.api.ui.*;
    
    
    public class ProgressAnimationField extends Field implements Runnable 
    {
        private Bitmap _bitmap;
        private int _numFrames;
        private int _frameWidth;
        private int _frameHeight;
        
        private int _currentFrame;
        private int _timerID = -1;
        
        private Application _application;
        private boolean _visible;
              
        public ProgressAnimationField( Bitmap bitmap, int numFrames, long style ) 
        {
            super( style | Field.NON_FOCUSABLE );
            _bitmap = bitmap;
            _numFrames = numFrames;
            _frameWidth = _bitmap.getWidth() / _numFrames;
            _frameHeight = _bitmap.getHeight();
            
            _application = Application.getApplication();
        }
        
        public void run() 
        {
            if( _visible ) {
                invalidate();
            }
        }
        
        protected void layout( int width, int height ) 
        {
            setExtent( width, _frameHeight + 5 );
        }
        
        protected void paint( Graphics g ) 
        {
            g.drawBitmap( 10, [B][COLOR=Red]5[/COLOR][/B], _frameWidth, _frameHeight, _bitmap, _frameWidth * _currentFrame, 0 );  // Con số 5 thể hiện hình trên có [B][COLOR=Red]5[/COLOR][/B] frames.  
            _currentFrame++;
            if( _currentFrame >= _numFrames ) {
                _currentFrame = 0;
            }
        }
        
        protected void onDisplay() 
        {
            super.onDisplay();
            _visible = true;
            if( _timerID == -1 ) {
                _timerID = _application.invokeLater( this, 200, true ); 
            } 
        }
        
        protected void onUndisplay() 
        {
            super.onUndisplay();
            _visible = false;
            if( _timerID != -1 ) {
                _application.cancelInvokeLater( _timerID );
                _timerID = -1;
            }
        }
    }
    
    Đây là giải pháp hình động dựa theo nguyên tắc ghép ảnh liên tục theo tần suất cố định để cho ra 1 ảnh động.

    Bạn có thể thử các ảnh sau:

    [IMG]

    [IMG]
    bbtn, phanthangbkl_x_p thích nội dung này.
  2. phanthangbk Thành viên

    Chào các anh, em muốn tập tẹ lập trình java cho BB, k biết bắt đầu từ đâu. Em còn chưa thạo sài JDE. mong các pác hướng dẫn cụ thể hoặc cho em xin chút tài liệu hướng dẫn. Thanks ^^!
  3. bbtn Thành viên

    Anh Hải cho em hỏi khi em muốn tạo phím tắt cho ứng dụng thì cần Import những gì ạ. Ở đây là em gán phím tắt cho ứng dụng em viết ra nhé

Chia sẻ trang này