Android_UI_美化_图片渐变动画效果

首先、准备好资源、得有两张图片



1、将两张图片内放入drawable-hdpi文件夹下;

2、在res/drawable文件夹下创建my_transition.xml文件

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/lamp_off"></item>
	<item android:drawable="@drawable/lamp_on"></item>
</transition>

3、在main_activity.xml文件中编写:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/main_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/my_transition" />

    <Switch
        android:id="@+id/main_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/main_iv"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp"
        android:textOff="关"
        android:textOn="开" />
    
</RelativeLayout>

4、在MainActivity.java中编写:


public class MainActivity extends Activity {

	private ImageView iv;

	private Switch sw;

	private TransitionDrawable td;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iv=(ImageView) findViewById(R.id.main_iv);

		sw=(Switch) findViewById(R.id.main_switch);
		td=(TransitionDrawable) iv.getDrawable();
		sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub

				if(isChecked){
					td.startTransition(500);
				}else{
					td.reverseTransition(5000);
				}

			}
		});
	}
}

5、运行就ok了,在下简单学习后,挺有意思的,大家可以互相学习。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值