绘制位图按钮android

时间:2016-02-28 16:52:36

标签: android button bitmap

所以我正在制作游戏的开始画面。目前它只有背景图像和按钮图像。我发布下面的代码,然后解释问题...

public class TitleView extends View {

    private Bitmap titleGraphic;
    private Bitmap scaledTitleGraphic;

    private Bitmap playButtonUp;
    private Bitmap scaledPlayButton;

    private Bitmap playButtonDown;
    private Bitmap scaledPlayButtonDown;

    private Boolean playButtonPressed = false;
    private int screenW;//storing width...
    private int screenH;//storing height...


    public TitleView(Context context) {
        super(context);
        titleGraphic = BitmapFactory.decodeResource(getResources(), R.drawable.title_graphic);
        //scaledTitleGraphic = Bitmap.createScaledBitmap(titleGraphic,screenH,screenH,true);
        playButtonUp = BitmapFactory.decodeResource(getResources(), R.drawable.play_button_up);
        scaledPlayButton = Bitmap.createScaledBitmap(playButtonUp, 400, 125, true);
        playButtonDown = BitmapFactory.decodeResource(getResources(),R.drawable.play_button_down);
        scaledPlayButtonDown = Bitmap.createScaledBitmap(playButtonDown, 400, 125,true);
    }

    @Override //gets the width and height...
    public void onSizeChanged (int w, int h, int oldw, int oldh){
        super.onSizeChanged(w, h, oldw, oldh);
        screenW = w;
        screenH = h;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        //canvas.drawBitmap(titleGraphic, (screenW - titleGraphic.getWidth())/2, 0, null);
        //canvas.drawBitmap(scaledPlayButton, (screenW - playButtonUp.getWidth())/2, buttonPosition , null);
        scaledTitleGraphic = Bitmap.createScaledBitmap(titleGraphic,screenW,screenH,true);
        canvas.drawBitmap(scaledTitleGraphic, 0, 0, null);

        if(playButtonPressed){
            canvas.drawBitmap(scaledPlayButtonDown, (screenW - scaledPlayButtonDown.getWidth())/2, (int) (screenH*0.7) , null);
        }else{
            canvas.drawBitmap(scaledPlayButton, (screenW - scaledPlayButton.getWidth())/2, (int) (screenH*0.7) , null);
        }

    }
    public boolean onTouchEvent(MotionEvent event) {

        int eventaction = event.getAction();
        int X = (int)event.getX();
        int Y = (int)event.getY();

        switch (eventaction ) {
            case MotionEvent.ACTION_DOWN:
                if(((X > ((screenW-playButtonUp.getWidth())/2)) && (X < (((screenW-playButtonUp.getWidth())/2) +
                        playButtonUp.getWidth()))) && ((Y > (int)(screenH*0.7)) && (Y < (int)(screenH*0.7) +
                        playButtonUp.getHeight()))){
                    playButtonPressed = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                playButtonPressed = false;
                break;
        }
        invalidate();
        return true;

    }
}

这是最好的方法吗?首先,当我放置背景时它不合适所以我使用了createscaledbitmap方法。这是普遍使用还是我应该制作完美的分辨率图像?我觉得有点奇怪,为原始图像定义两个位图,然后为缩放图像定义另一个位图。

其次我遇到了类似按钮的问题。最初,当我把我的按钮放在它上面时,我的大部分屏幕覆盖了很大,所以我想我应该把它做得更小,然后我去了照相馆并将它制作成100x40,但那还是太大了?所以我还必须缩放位图。我只是感到困惑,我的意思是......我知道将所有分辨率的图像放到具有不同分辨率的目标设备上的好习惯,但我觉得我的方法不正确,因为在这种情况下,我应该只放一个巨大的图像,然后缩放所有时间?

我想说的是......我是从一本书中读到的,但是这本书有点陈旧了几年之后,我应该知道一个更好的方法吗?我的意思是,例如我想使用on click listener等,但在这种方法中使用onTouchEvent?

0 个答案:

没有答案