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>








展开阅读全文

没有更多推荐了,返回首页