LinearLayout模仿Button点击动态缩放效果

之前看到一个App的布局仿win8很漂亮,对它每个色块点击有缩放效果很好奇,动手试了一下。

  

直接上代码。。。。。。。。



MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnTouchListener {

	private LinearLayout layout1, layout2;

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

		layout1 = (LinearLayout) findViewById(R.id.layout1);
		layout2 = (LinearLayout) findViewById(R.id.layout2);

		layout1.setOnTouchListener(this);
		layout2.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		
		//设置Animation
		Animation animDwon = AnimationUtils.loadAnimation(this, R.anim.show_down);
		Animation animUp = AnimationUtils.loadAnimation(this, R.anim.show_up);
		
		LinearLayout layout = (LinearLayout) v;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			layout.startAnimation(animDwon);
			animDwon.setFillAfter(true);
			break;

		case MotionEvent.ACTION_UP:
			layout.startAnimation(animUp);
			animUp.setFillAfter(true);
			break;
		}
		return true;	//这时必须返回true,不然 MotionEvent.ACTION_UP 没效果
	}

}

activity_main.xml

<LinearLayout 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"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="80dip"
        android:layout_weight="1"
        android:background="#ff335566" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="Layout1" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="fill_parent"
        android:layout_height="80dip"
        android:layout_weight="1"
        android:layout_marginLeft="5dip"
        android:background="#ff447788" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="Layout2" />
    </LinearLayout>

</LinearLayout>

show_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.9"
        android:toYScale="0.9" />

</set>

show_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="200"
        android:fromXScale="0.9"
        android:fromYScale="0.9"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>








  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值