android實現alpha漸變動畫效果

這裏我來教大家實現安卓漸變動畫的兩種方式:

首先佈局裏面寫到:

<Button
        android:id="@+id/btn_alpha"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="漸變動畫" />
    
    <ImageView 
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/day_6"/>
這裏麪包括一個按鈕,用來點擊的,一個圖片,用來觀察效果的。

第一種:定義anim文件

在res文件夾下面定義一個anim文加件,裏面創建xml文件。代碼如下:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true"
    android:duration="3000"
    android:startOffset="2000"
    android:repeatCount="2">
然後就是在avtivity裏面調用了。

第二種:在activity裏面動態寫入動畫。代碼如下:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findViewById(R.id.btn_alpha).setOnClickListener(this);
        mImageView = (ImageView) findViewById(R.id.imageview);
    }

	public void onClick(View v) {
		//startAlphaAnimationJavaCode();
		staryAlphaAnimationXml();
	}

	private void staryAlphaAnimationXml() {
//		android:interpolator="@android:anim/linear_interpolator"    動畫加速   減速    加速再減速
//	android:fillAfter="true"		動畫執行後的狀態	
//	android:duration="3000"			動畫執行3秒
//	android:startOffset="2000"		動畫2秒後執行
//	android:repeatCount="2"			動畫重複執行2遍
		Animation alphaAnim = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
		mImageView.startAnimation(alphaAnim);
	}

	private void startAlphaAnimationJavaCode() {
		//漸變動畫    從顯示(1.0)到隱藏(0.0)
		AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.0f);
		//執行三秒
		alphaAnim.setDuration(3000);
		mImageView.startAnimation(alphaAnim);
	}
在點擊方法裏面寫了兩個方法,分別是兩種實現方式。有什麼寫的不到位的還望博友多多指教。

                                                      完畢!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章