日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UI组件之ImageView及其子类(二)ImageButton ,ZoomButton

發布時間:2025/3/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UI组件之ImageView及其子类(二)ImageButton ,ZoomButton 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

從ImageButton這個字面意思上來看,它是一個圖片按鈕,那么我們就可以使用它做一個我們想要的圖片按鈕了,但是我們在實際使用的過程當中,就會發現該按鈕的使用并沒有想像中的那么簡單,需要再增加一些代碼或再配置XML才能實現圖片按鈕按下的效果

ImageButton 還直接派生了ZoomButton組件,只是Android默認提供了btn_minus,btn_plus兩個Drawable資源,只要為ZoomButton的android:src屬性分別指著兩個android提供的資源,即可實現“放大”,“縮小”按鈕,ZoomButton組件僅僅是android:src屬性默認使用的是android的資源,添加了一些方法可實現放大縮小。


android還提供了一個ZoomControls組件,該組件繼承了是LeanerLayout布局組件,把兩個放大縮小的按鈕水平線性的組合在一起,并允許分別為兩個按鈕綁定不同的事件監聽器


圖片按鈕正常狀態的效果:


第二個按鈕按下是的效果


實現圖片按鈕按下的效果有兩種方式可以實現:一是增加代碼,二配置XML。

一、在java中為圖片按鈕增加觸摸監聽的函數來實現圖片切換

ImageButton btn = (ImageButton)findViewById(R.id.imageButton1); //在java代碼中修改按下按鈕時不同的狀態btn.setOnTouchListener(new View.OnTouchListener(){ @Overridepublic boolean onTouch(View v, MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN){ //重新設置按下時的背景圖片 ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.red)); }else if(event.getAction() == MotionEvent.ACTION_UP){ //再修改為抬起時的正常圖片 ((ImageButton)v).setImageDrawable(getResources().getDrawable(R.drawable.purple)); } // TODO Auto-generated method stubreturn false;} }); 代碼比較簡單,就是當圖片按下時,修改按鈕的背景圖片,當抬起時再修改為正常的圖片顯示
二、通過給按鈕配置XML文件來實現圖片按鈕的背景切換效果

<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 指定按鈕按鈕下時的圖片 --><item android:state_pressed="true"android:drawable="@drawable/red"/><!-- 指定按鈕松開時的圖片 --> <item android:state_pressed="false"android:drawable="@drawable/purple"/> </selector>
main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><!-- 普通圖片按鈕 --><ImageButtonandroid:id="@+id/imageButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/blue" /> <!-- 按下時顯示不同圖片的按鈕 ,android:background--><ImageButtonandroid:id="@+id/imageButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_selector" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_margin="10dp"android:orientation="horizontal" ><!-- 分別定義兩個zoombutton,分別用了android提供的btn_minus和btn_plus圖片,當然自己也可以定義 --><ZoomButtonandroid:id="@+id/zoomButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@android:drawable/btn_plus" /><ZoomButtonandroid:id="@+id/zoomButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@android:drawable/btn_minus" /></LinearLayout><!-- 定義zoomcontrols組件 --><ZoomControlsandroid:id="@+id/zoomControls1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:orientation="horizontal" /></LinearLayout>
需要特別注意的是:在ImageButton中,如果使用XML配置文件來設置圖片的效果的話,就不要再指定它的android:src=""屬性值了,否則圖片的按下效果就出不來了。
這兩種方法各有各的好處,在實際運用過種當種可以根據自己的需要進行選擇。

推薦ImageButton用的好的文章:

http://my.oschina.net/amigos/blog/59751

http://blog.csdn.net/ztp800201/article/details/7312687



總結

以上是生活随笔為你收集整理的UI组件之ImageView及其子类(二)ImageButton ,ZoomButton的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。