Android 收缩展开动画

Android 收缩展开动画_第1张图片

activity_main.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"
    tools:context="${packageName}.${activityClass}" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:listSelector="@android:color/transparent" >
    ListView>

RelativeLayout>

lv_item.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <LinearLayout
        android:id="@+id/timeline_item_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="12dp"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/timeline_item_top"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:background="@drawable/timeline_cont"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tv_projectcontent_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="工作内容:"
                android:textColor="@color/gray1"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/tv_projectcontent_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="洽谈"
                android:textColor="@color/gray1"
                android:textSize="16sp" />
        LinearLayout>

        <RelativeLayout
            android:id="@+id/timeline_item_bottom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="-3dp"
            android:background="@drawable/spinner_bottom"
            android:padding="10dp"
            android:visibility="gone" >

            <TextView
                android:id="@+id/tv_projectname_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="项目名称:"
                android:textColor="@color/gray1" />

            <TextView
                android:id="@+id/tv_projectname_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/tv_projectname_title"
                android:gravity="center"
                android:text="大数据研发项目"
                android:textColor="@color/black" />

            <View
                android:id="@+id/line1"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_below="@id/tv_projectname_title"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/dotted_line_h_timeline" />

            <TextView
                android:id="@+id/tv_projectstage_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/line1"
                android:gravity="center"
                android:text="项目阶段:"
                android:textColor="@color/gray1" />

            <TextView
                android:id="@+id/tv_projectstage_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/tv_projectstage_title"
                android:layout_toRightOf="@id/tv_projectstage_title"
                android:gravity="center"
                android:text="并发测试"
                android:textColor="@color/black" />
        RelativeLayout>
    LinearLayout>

RelativeLayout>

LogAdapter.java

package com.example.expanddemo;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LogAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List logModels;
    private Context context;

    public LogAdapter(Context context, List itemModels) {
        inflater = LayoutInflater.from(context);
        this.logModels = itemModels;
        this.context = context;
    }

    @Override
    public int getCount() {
        return logModels.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LogModel itemModel = logModels.get(position);
        ViewHolder holder = null;
        View relativeView = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.lv_item, null);
            holder.tVprojectTitle = (TextView) convertView.findViewById(R.id.tv_projectcontent_title);
            holder.tVprojectContent = (TextView) convertView.findViewById(R.id.tv_projectcontent_content);
            holder.tVprojectName = (TextView) convertView.findViewById(R.id.tv_projectname_content);
            holder.tVprojectStage = (TextView) convertView.findViewById(R.id.tv_projectstage_content);
            holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.timeline_item_top);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        relativeView = (RelativeLayout) convertView.findViewById(R.id.timeline_item_bottom);
        relativeView.measure(0, 0);
        if (itemModel.isVisiable()) {
            holder.linearLayout.setBackgroundResource(R.drawable.timeline_cont_p);
            holder.tVprojectTitle.setTextColor(context.getResources().getColor(R.color.white));
            holder.tVprojectContent.setTextColor(context.getResources().getColor(R.color.white));
            ((LinearLayout.LayoutParams) relativeView.getLayoutParams()).bottomMargin = 0;
            relativeView.setVisibility(View.VISIBLE);
        } else {
            holder.linearLayout.setBackgroundResource(R.drawable.timeline_cont);
            holder.tVprojectContent.setTextColor(context.getResources().getColor(R.color.gray1));
            ((LinearLayout.LayoutParams) relativeView.getLayoutParams()).bottomMargin = (-1) * relativeView.getMeasuredHeight();
            relativeView.setVisibility(View.GONE);
        }
        holder.tVprojectContent.setText(itemModel.getProjectContent());
        holder.tVprojectName.setText(itemModel.getProjectName());
        holder.tVprojectStage.setText(itemModel.getProjectStage());
        return convertView;
    }

    public void refuseAdapter(List datas) {
        this.logModels = datas;
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView tVprojectContent, tVprojectName, tVprojectStage, tVprojectTitle;
        LinearLayout linearLayout;
    }

}

LogModel.java

package com.example.expanddemo;

public class LogModel {

    private String projectContent;
    private String projectName;
    private String projectStage;
    private boolean isVisiable = true;

    public LogModel(String projectContent, String projectName, String projectStage) {
        this.projectContent = projectContent;
        this.projectName = projectName;
        this.projectStage = projectStage;
    }

    public String getProjectContent() {
        return projectContent;
    }

    public void setProjectContent(String projectContent) {
        this.projectContent = projectContent;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectStage() {
        return projectStage;
    }

    public void setProjectStage(String projectStage) {
    }

    public boolean isVisiable() {
        return isVisiable;
    }

    public void setVisiable(boolean isVisiable) {
        this.isVisiable = isVisiable;
    }
}

ExpandAnimation.java

package com.example.expanddemo;

import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class ExpandAnimation extends Animation {

    private View view;
    private LinearLayout.LayoutParams mViewLayoutParams;
    private int mMarginStart, mMarginEnd;
    private boolean mIsVisibleAfter = false;
    private boolean mWasEndedAlready = false;

    public ExpandAnimation(View view, long duration) {
        setDuration(duration);
        this.view = view;
        mViewLayoutParams = (LayoutParams) view.getLayoutParams();
        mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);
        mMarginStart = mViewLayoutParams.bottomMargin;
        view.measure(0, 0);
        mMarginEnd = (mMarginStart == 0) ? (0 - view.getMeasuredHeight()) : 0;
        // mMarginEnd = mMarginStart - view.getMeasuredHeight();
        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        Log.i("info", "mMarginStart-->>" + mMarginStart + ", mMarginEnd-->>" + mMarginEnd + ", interpolatedTime-->>" + interpolatedTime);
        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1f) {
            mViewLayoutParams.bottomMargin = mMarginStart + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
            view.requestLayout();
        } else if (!mWasEndedAlready) {
            mViewLayoutParams.bottomMargin = mMarginEnd;
            view.requestLayout();
            if (mIsVisibleAfter)
                view.setVisibility(View.GONE);
            mWasEndedAlready = true;
        }
    }
}

MainActivity.java

package com.example.expanddemo;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listView;
    private LogAdapter adapter;
    /** 控制重复点击 */
    private boolean animationFinished = true;
    private List logModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView1);
        getDatas();
        adapter = new LogAdapter(this, logModels);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                if (!animationFinished)
                    return;
                View animationView = view.findViewById(R.id.timeline_item_bottom);
                ExpandAnimation animation = new ExpandAnimation(animationView, 300);
                animation.setAnimationListener(new MyAnimationListener(position));
                animationView.startAnimation(animation);
            }
        });
    }

    private void getDatas() {
        logModels = new ArrayList();
        for (int i = 0; i < 5; i++) {
            LogModel logModel = new LogModel("测试" + i, "大数据项目" + i, "第 " + i + " 阶段");
            logModels.add(logModel);
        }
    }

    class MyAnimationListener implements AnimationListener {

        private int position;
        private LinearLayout linearLayout;
        private TextView tVprojectTitle, tVprojectContent;

        public MyAnimationListener(int position) {
            this.position = position;
            init();
        }

        private void init() {
            View view = listView.getChildAt(position);
            linearLayout = (LinearLayout) view.findViewById(R.id.timeline_item_top);
            tVprojectTitle = (TextView) view.findViewById(R.id.tv_projectcontent_title);
            tVprojectContent = (TextView) view.findViewById(R.id.tv_projectcontent_content);
        }

        @Override
        public void onAnimationStart(Animation arg0) {
            animationFinished = false;
            LogModel item = logModels.get(position);
            if (!item.isVisiable()) {
                linearLayout.setBackgroundResource(R.drawable.timeline_cont_p);
                tVprojectTitle.setTextColor(getResources().getColor(R.color.white));
                tVprojectContent.setTextColor(getResources().getColor(R.color.white));
            }
        }

        @Override
        public void onAnimationRepeat(Animation arg0) {

        }

        @Override
        public void onAnimationEnd(Animation arg0) {
            LogModel item = logModels.get(position);
            item.setVisiable(!item.isVisiable());
            adapter.notifyDataSetChanged();
            animationFinished = true;
        }
    }
}

源码下载

你可能感兴趣的:(Android)