天天看點

Android中ImageButton的三種點選效果—點選變化,點一次換一張,逐幀動畫的實作

一、點選變化

點選變化是指滑鼠點選時,圖檔發生改變,松開滑鼠後,圖檔還原。

main.xml中的代碼:

<ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text1"
        android:contentDescription="@null"
        android:background="@drawable/image"/>
           

image.xml中的代碼:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/a"/>
    <item android:state_pressed="false" android:drawable="@drawable/b"/>
</selector>
           

二、點一次換一張

main.xml中的代碼:

<ImageButton
        android:id="@+id/ib1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="140dp"
        android:background="@drawable/b"
        android:contentDescription="@null" />

    <ImageButton
        android:id="@+id/ib2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="147dp"
        android:background="@drawable/a"
        android:contentDescription="@null" />
           

MainActivity.java中的代碼:

public class MainActivity extends Activity implements OnClickListener  {
	ImageButton button1;
	ImageButton button2;

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 獲得ib1的id
		button1 = (ImageButton) findViewById(R.id.ib1);
		//顯示VISIBLE
		button1.setVisibility(View.VISIBLE);

		button2=(ImageButton)findViewById(R.id.ib2);
		//隐藏GONE
		button2.setVisibility(View.GONE);

		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.ib1:
				button1.setVisibility(View.GONE);
			    button2.setVisibility(View.VISIBLE);
			    break;
			case R.id.ib2:
				button2.setVisibility(View.GONE);
			    button1.setVisibility(View.VISIBLE);
			    break;
		}
	}
           

三、逐幀動畫的實作

main.xml中的代碼:

<ImageButton
        android:id="@+id/ib3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="260dp"
        android:contentDescription="@null" />
           

many.xml中的代碼:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >
    <item android:drawable="@drawable/ic_19" android:duration="50"/>
    <item android:drawable="@drawable/ic_18" android:duration="50"/>
    <item android:drawable="@drawable/ic_17" android:duration="50"/>
    <item android:drawable="@drawable/ic_16" android:duration="50"/>
    <item android:drawable="@drawable/ic_15" android:duration="50"/>
    <item android:drawable="@drawable/ic_14" android:duration="50"/>
    <item android:drawable="@drawable/ic_13" android:duration="50"/>
    <item android:drawable="@drawable/ic_12" android:duration="50"/>
    <item android:drawable="@drawable/ic_11" android:duration="50"/>
    <item android:drawable="@drawable/ic_10" android:duration="50"/>
    <item android:drawable="@drawable/ic_9" android:duration="50"/>
    <item android:drawable="@drawable/ic_8" android:duration="90"/>
    <item android:drawable="@drawable/ic_7" android:duration="90"/>
    <item android:drawable="@drawable/ic_6" android:duration="90"/>
    <item android:drawable="@drawable/ic_5" android:duration="90"/>
    <item android:drawable="@drawable/ic_4" android:duration="80"/>
    <item android:drawable="@drawable/ic_3" android:duration="80"/>
    <item android:drawable="@drawable/ic_2" android:duration="80"/>
    <item android:drawable="@drawable/ic_1" android:duration="80"/>

</animation-list>
           

MainActivity.java中的代碼:

public class MainActivity extends Activity {
	ImageButton button3;
	AnimationDrawable animationDrawable;
	int flage=0;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//動畫顯示
		button3 = (ImageButton) findViewById(R.id.ib3);
		button3.setBackgroundResource(R.drawable.many);
		button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				animationDrawable=(AnimationDrawable) button3.getBackground();
                //這個判斷有問題,還沒有想到用一個imagebuttonn怎麼才能做到
				if(flage%2==0){
					animationDrawable.start();
					flage++;
				}
				else{
					animationDrawable.stop();
					flage++;
				}
			}
		});
	}