簡體   English   中英

Android XML ImageView更改圖像

[英]Android XML ImageView changing Image

我正在嘗試一些功能,並且基本上是在學習東西,但我不知道為什么這不起作用。 我的意思是,當我第一次觸摸圖像時,圖像更改為R.drawable.buttondown,但是隨后當我移動手指或釋放手指時,其他情況將不起作用,並且圖像也不再更改。 你能告訴我為什么嗎?

    icon = (ImageView)findViewById(R.id.imageView1);

    icon.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                icon.setImageResource(R.drawable.buttondown);
                break;
            case MotionEvent.ACTION_UP:
                icon.setImageResource(R.drawable.ic_launcher);
                break;
            case MotionEvent.ACTION_MOVE:
                icon.setImageResource(R.drawable.abc_ab_bottom_transparent_dark_holo);
                break;
            }
            return false;
    }

您需要返回true來通知android您已經消耗了該事件。

public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            icon.setImageResource(R.drawable.buttondown);
            break;
        case MotionEvent.ACTION_UP:
            icon.setImageResource(R.drawable.ic_launcher);
            break;
        case MotionEvent.ACTION_MOVE:
            icon.setImageResource(R.drawable.abc_ab_bottom_transparent_dark_holo);
            break;
        }
        return true;
}

從文檔:

如果偵聽器已消耗事件,則為true,否則為false。

如果返回true,則表示您剛剛使用了此事件,並且對其他事件感興趣。 如果返回false,則將保留ACTION_DOWN,因為未使用該事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM