2016-07-25 35 views
1

我正在Android應用程序中,我有一個ImageView,它直接在XML中設置(默認圖像)。現在,當用戶點擊圖片時,我想只用另一張圖片進行更改,以便用戶知道它已被按下。當我擡頭時,我可以找到onClickListener,但是這改變了圖像,並且不會恢復到原來的。我只是想找一些方法來顯示按鈕被按下。Android:更改圖像,當用戶單擊並將其設置爲原始

XML代碼:

<ImageView 
      android:id="@+id/shirtImage" 
      android:layout_width="75dp" 
      android:layout_height="75dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_centerVertical="true" 

      android:layout_gravity="center" 
      android:layout_marginLeft="30dp" 
      android:layout_marginStart="30dp" 
      android:alpha="0.8" 
      android:src="@drawable/shirt" /> 

活動代碼:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 

     shirt = (ImageView)findViewById(R.id.shirtImage); 

shirt.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       shirt.setImageResource(R.drawable.shirtactive); 
      } 
     }); 

    } 

謝謝。

更新代碼

shirt.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction() == MotionEvent.ACTION_DOWN){ 
        shirt.startAnimation(animationFadeIn); 
        shirt.setImageResource(R.drawable.shirtactive); 
        return true; 
       } 
       else if(event.getAction() == MotionEvent.ACTION_UP){ 
        shirt.startAnimation(animationFadeOut); 
        shirt.setImageResource(R.drawable.shirt); 
        return true; 
       } 
       return false; 
      } 
     }); 

回答

0

創建可繪製文件夾的選擇器,並設置爲源。 創建image_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/pressed_image" android:state_pressed="true"/> 
    <item android:drawable="@drawable/default_image"/> 
</selector> 

然後在佈局,

<ImageView 
     android:id="@+id/shirtImage" 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_centerVertical="true" 

     android:layout_gravity="center" 
     android:layout_marginLeft="30dp" 
     android:layout_marginStart="30dp" 
     android:alpha="0.8" 
     android:src="@drawable/image_selector" /> 
2

您可以用下面的內容

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/shirtactive" android:state_pressed="true" /> 
    <item android:drawable="@drawable/shirtactive" android:state_selected="true" /> 
    <item android:drawable="@drawable/shirt"/> 

</selector> 
創建一個在您繪製文件夾中的XML文件(名稱selector_imageview爲例)

並設置在您的ImageView中

<ImageView 
     android:id="@+id/shirtImage" 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_centerVertical="true" 

     android:layout_gravity="center" 
     android:layout_marginLeft="30dp" 
     android:layout_marginStart="30dp" 
     android:alpha="0.8" 
     android:src="@drawable/selector_imageview" /> 
0

您會發現,onClickListener僅在您單擊View時纔會運行其回調。這意味着,你按下了它,然後快速釋放它。你的情況,你有兩個選擇:

變我

實現以編程方式,你需要調用setOnTouchListeneronTouch回調,你需要檢查MotionEvent行動。如果它是向下的,那麼你需要改變你的圖像並「按下」一個,如果是UP,那麼就改回你的圖像。

變II

您需要提供XML withi這個模板:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/pressed_image" android:state_pressed="true"/> 
    <item android:drawable="@drawable/default_image"/> 
</selector> 

這將會給你你想要的效果。另外,我建議你使用這種方法,因爲它是少的樣板,更靈活,因爲:

  • 你能爲他們提供另一種狀態和圖像,例如,「專注」狀態或「選中」狀態
  • 你可以用更具體的一個覆蓋這個xml(例如,對於版本大於等於21的版本,在按下時不提供簡單的變化圖像,但是連鎖效果,或者一個國家爲另一個按下的圖像創建一個按下的圖像)。

更新

如果您想使用的動畫時改變視圖狀態從「默認」爲「壓制」,反之亦然,你需要使用變一,下面是示例:

view.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     switch (motionEvent.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       //make fade-in animation here 
       return true; 
      case MotionEvent.ACTION_UP: 
       //make fade-out animation here 
       return true; 
      default: 
       return false; 
     } 
    } 
}); 
+0

如何使用此功能獲得印刷機的淡入效果? –

+0

@ WeareBorg如果您將使用第一個變體,那麼在動作下降和動作上可以做任何你想做的事情。我會在我的回答中添加示例 –

0

現在,當用戶點擊圖片時,我想只用 來改變它,所以用戶知道它被按下了。

然後你想在兩個圖像之間切換。您想要將CheckBox的行爲應用於ImageView。 Yon可以跟蹤當前的圖像集,例如使用View#setTagView#getTag或ImageView的子類,並讓它實現Checkable接口。這樣你可以有一個選擇器,狀態爲android:state_checked。我寧願爲後者,即使前者很容易實現

0

我希望這段代碼能幫助你。您需要將圖像重置爲默認圖像ACTION_UP

imageButton.setOnTouchListener(new OnTouchListener() { 
@Override 
public boolean onTouch(View v, MotionEvent event) { 
    if(event.getAction() == MotionEvent.ACTION_DOWN){ 

     // Press stat 
     return true; 
    } 
    else if(event.getAction() == MotionEvent.ACTION_UP){ 

     // Relese Stat 
     return true; 
    } 
    return false; 
} 
}); 
+0

檢查了這一點 –

+0

很酷,這很好。您可能知道任何用於淡入淡出的動畫?持續時間非常短,看起來並不那麼順利。謝謝。 –

+0

我想你需要看看這個[鏈接](http://android-er.blogspot.in/2012/02/animate-fade-infade-out-by-changing.html) – Elango

0

其他答案對您的目的沒問題。但我建議使用svg文件作爲drawable,並在imageview屬性中使用tint模式。這是主要用於材料設計的最佳方式。

+0

üü當然?實際上,AppComapt庫具有向後兼容性。所以認爲色調適合向後兼容 –

相關問題