1

我想在黑莓 java 中创建一个播放/暂停按钮,如 youtube,我们如何使用单个按钮处理两个操作,即当第一次单击该按钮时,它应该播放音频,如果再次单击同一个按钮音频应该停止,我们如何用一个按钮处理两个动作,我们如何改变播放/暂停等图像?

4

1 回答 1

2
import java.util.*;

import net.rim.device.api.system.*;

import net.rim.device.api.ui.*;

import net.rim.device.api.ui.component.*;

import net.rim.device.api.ui.container.*;

import net.rim.device.api.system.Bitmap;

public class CustomImageButtonApp extends UiApplication
{
static Bitmap image;

public static void main(String[] args)
{
    CustomImageButtonApp app = new CustomImageButtonApp ();
    app.enterEventDispatcher();
}
public CustomImageButtonApp ()
{
 final imagebutton img_btn = new imagebutton("",Field.FOCUSABLE, "play.png",
 "pause.png", 0x9cbe95);
    MainScreen screen=new MainScreen();
     pushScreen(screen);
     screen.setTitle("title");
     screen.add(img_btn);
 }




public class CustomImageButton extends Field  {

    private String _label;
    private int _labelHeight;
    private int _labelWidth;
    private Font _font;   
    private Bitmap _currentPicture;
    private Bitmap _playPicture;
    private Bitmap _pausePicture;
    int color;


    public  CustomImageButton (String text, long style ,String playimg, String pauseimg, int color){
           super(style);

        _playPicture= Bitmap.getBitmapResource( playimg );
        _pausePicture=Bitmap.getBitmapResource( pauseimg );

        _font = getFont();
        _label = text;
        _labelHeight = _playPicture.getHeight(); 
        _labelWidth = _pausePicture.getWidth();

        this.color = color;

        _currentPicture = _playPicture;
    }


    /**
     * @return The text on the button
     */
    String getText(){
        return _label;
    }


   /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#getPreferredHeight()
     */
    public int getPreferredHeight(){
        return _labelHeight;
    }

     /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#getPreferredWidth()
     */
    public int getPreferredWidth(){
        return _labelWidth;
    }


    /**
     * Field implementation. 
     * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
     */
    protected void drawFocus(Graphics graphics, boolean on) {
     // Do nothing
    }

    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#layout(int, int)
     */
    protected void layout(int width, int height) {
        setExtent(Math.min( width, getPreferredWidth()),
        Math.min( height, getPreferredHeight()));
    }
    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#paint(Graphics)
     */
    protected void paint(Graphics graphics){     
        // First draw the background colour and picture
        graphics.setColor(this.color);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);

        // Then draw the text
        graphics.setColor(Color.BLACK);
        graphics.setFont(_font);
        graphics.drawText(_label, 4, 2,
            (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
            getWidth() - 6 );
    }

    /**
     * Overridden so that the Event Dispatch thread can catch this event
     * instead of having it be caught here..
     * @see net.rim.device.api.ui.Field#navigationClick(int, int)
     */
    protected boolean navigationClick(int status, int time){

      if (_currentPicture ==  _playPicture) {
             _currentPicture =  _pausePicture;
            // invalidate(); 

         } else {
             _currentPicture = _playPicture;
             //invalidate(); 

         }

         //fieldChangeNotify(1);
         return true;

}
}
}
于 2013-02-27T07:52:30.930 回答