文章目录

  • 1、功能描述
  • 2、代码架构
  • 3、activity_main.xml 文件
  • 4、alpha_out.xml 透明度属性文件
  • 5、MainActivity.java 逻辑功能文件


1、功能描述

实现图片透明度的改变

Android Alpha 更改图片透明度_属性文件


点击之后

Android Alpha 更改图片透明度_android_02

两个图片透明度的改变实现方式不一样
1)一个是加载anmi xml 文件 里定义好的 透明度改变动画
2)一个是再代码里 实例化 animationSet 和 alphaAnimation 对象,来进行 动态设定

2、代码架构

Android Alpha 更改图片透明度_属性文件_03

1)activity_main.xml 定义了 按钮 和 两个 imageView
2)anim 文件 下的 alpha_out.xml 文件 是,定义好的 透明度改变的属性文件
3)MainActivity 文件 是实现逻辑功能

3、activity_main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lum.myalpha.MainActivity">



    <ImageView
        android:id="@+id/image_view_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/test"/>

    <ImageView
        android:id="@+id/image_two_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/two"/>
    >
    <Button
        android:id="@+id/button_alp_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="透明度" />

</LinearLayout>
4、alpha_out.xml 透明度属性文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!--其中alpha 定义了三个标签
    fromAlpha : 表示起始动画的透明度,1.0表示完全不透明
    0.0 表示完全透明,值0.0 ~ 1.0之间
    toAlpha : 表示动画结束时的透明度,
    duration: 动画持续事件,单位毫秒
    -->
<alpha android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration = "1000"/>

    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration = "2000"/>

    <alpha android:fromAlpha="1.0"
        android:toAlpha="0.5"
        android:duration = "3000"/>
</set>
5、MainActivity.java 逻辑功能文件
package com.example.lum.myalpha;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String  TAG  = "MainActivity: ";
    private Button button;
    private ImageView imageViewTest,imageViewTwo;  //定义两个image view对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageViewTwo = (ImageView) findViewById(R.id.image_two_id);
        imageViewTest = (ImageView) findViewById(R.id.image_view_id);
        button = (Button) findViewById(R.id.button_alp_id);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.button_alp_id:
                Log.i(TAG,"透明度改变");
                changeAlphaFormXml();//通过加载 xml  动画属性,来改变透明度
                changeAlphaFromCode(); //通过代码设置来更改透明度
                break;
                default:
                    break;
        }
    }

    //通过 代码  设置来更改透明度
    private void changeAlphaFromCode() {
        AnimationSet animationSet = new AnimationSet(true);
        //创建 AlphaAnimation 对象
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.1f);
        //设置动画持续时间
        alphaAnimation.setDuration(2000);
        //添加到AnimationSet
        animationSet.addAnimation(alphaAnimation);

        //开始动画
        imageViewTwo.startAnimation(animationSet);
    }

    //通过x'm'l改变透明度
    private void changeAlphaFormXml() {
        Log.i(TAG,"更改透明度");
        //定义Animation 对象
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha_out);
        //开始动画
        imageViewTest.startAnimation(animation);
    }
}

文章参考:
《Android 典型技术模块开发详解》

本人郑重声明,本博客所编文章、图片版权归权利人持有,本博只做学习交流分享所用,不做任何商业用途。访问者可將本博提供的內容或服务用于个人学习、研究或欣赏,不得用于商业使用。同時,访问者应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人的合法权利;如果用于商业用途,须征得相关权利人的书面授权。若文章、图片的原作者不愿意在此展示內容,请及时通知在下,將及时予以刪除。