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);
	}
在点击方法里面写了两个方法,分别是两种实现方式。有什么写的不到位的还望博友多多指教。

                                                      完毕!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值