有时候需要点击一个view可以动画展开和收缩折叠一个View这样的效果,这样就可以直接自定义View来实现。

本例中,采用继承FrameLayout来实现自定义的ExpandView。下面将详细介绍各个部分来实现该类以及如何使用该自定义视图。

效果图如下:

未展开效果:

正在向上折叠收缩中的效果:

已经展开效果:

自定义展开类:ExpandView的实现:

package com.czm.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.LinearLayout; public class ExpandView extends FrameLayout{ private Animation mExpandAnimation;
private Animation mCollapseAnimation;
private boolean mIsExpand; public ExpandView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
public ExpandView(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public ExpandView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initExpandView();
}
private void initExpandView() {
LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, this, true); mExpandAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.expand);
mExpandAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
} @Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
} @Override
public void onAnimationEnd(Animation animation) {
setVisibility(View.VISIBLE);
}
}); mCollapseAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.collapse);
mCollapseAnimation.setAnimationListener(new Animation.AnimationListener() { @Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
} @Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
} @Override
public void onAnimationEnd(Animation animation) {
setVisibility(View.INVISIBLE);
}
}); }
public void collapse() {
if (mIsExpand) {
mIsExpand = false;
clearAnimation();
startAnimation(mCollapseAnimation);
}
} public void expand() {
if (!mIsExpand) {
mIsExpand = true;
clearAnimation();
startAnimation(mExpandAnimation);
}
} public boolean isExpand() {
return mIsExpand;
} public void setContentView(){
View view = null;
view = LayoutInflater.from(getContext()).inflate(R.layout.layout_expand, null);
removeAllViews();
addView(view);
} }

对应的ui配置文件:layout_expand.xml的实现:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#63A90A"
android:gravity="center_horizontal"> <TextView
android:id="@+id/enterlesson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="项目列表1"
android:textSize="14sp"
android:singleLine="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/enterlesson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="项目列表2"
android:textSize="14sp"
android:singleLine="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/enterlesson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="项目列表3"
android:textSize="14sp"
android:singleLine="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/enterlesson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="项目列表4"
android:textSize="14sp"
android:singleLine="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:drawablePadding="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"/> </LinearLayout>

展开动画代码:

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

<scale
android:duration="200"
android:fromXScale="1."
android:fromYScale=".0"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1."
android:toYScale="1." />

</set>

收缩叠起代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <scale
android:duration="120"
android:fromXScale="1."
android:fromYScale="1."
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1."
android:toYScale="0." /> </set>

如何使用上面自定义的ExpandView类呢?分为两步:

(1)在UI配置文件里引用定义 该View:

<LinearLayout
android:id="@+id/layout_title"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#63A90A"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:text="点击向下展开"
/>
<ImageView
android:id="@+id/imageview_state"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="2dp"
android:src="@drawable/expand"
/>
</LinearLayout>

<com.czm.customview.ExpandView
android:id="@+id/expandView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:visibility="invisible"
android:layout_below="@+id/layout_title"
android:layout_marginBottom="150dp"
android:clickable="true"/>

(1)在java类中引用ExpandView类:

private ExpandView mExpandView;
private LinearLayout mLinearLayout;
private TextView mTextView;
private ImageView mImageView; public void initExpandView(){
   mLinearLayout = (LinearLayout)findViewById(R.id.layout_title);
mTextView = (TextView)findViewById(R.id.textview_title);
mImageView = (ImageView)findViewById(R.id.imageview_state);
mExpandView = (ExpandView) findViewById(R.id.expandView);
mExpandView.setContentView();
mLinearLayout.setClickable(true);
mLinearLayout.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mExpandView.isExpand()){
mExpandView.collapse();
mTextView.setText("点击向下展开");
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.expand));
}else{
mExpandView.expand();
mTextView.setText("点击向上收叠");
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.collapse));
}
}
});
}

Android 自定义View修炼-自定义可动画展开收缩View的实现的更多相关文章

  1. Android 自定义View修炼-自定义弹幕效果View

    一.概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂 ...

  2. Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)

    很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ...

  3. Android 自定义View修炼-自定义加载进度动画XCLoadingImageView

    一.概述 本自定义View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果. 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进度颜色. ...

  4. Android 自定义View修炼-自定义HorizontalScrollView视图实现仿ViewPager效果

    开发过程中,需要达到 HorizontalScrollView和ViewPager的效果,于是直接重写了HorizontalScrollView来达到实现ViewPager的效果. 实际效果图如下: ...

  5. Android 自定义View修炼-Android 实现自定义的卫星式菜单(弧形菜单)View

    一.总述 Android 实现卫星式菜单也叫弧形菜单的主要要做的工作如下:1.动画的处理2.自定义ViewGroup来实现卫星式菜单View (1)自定义属性       a. 在attrs.xml中 ...

  6. [转]Android自定义控件三部曲系列完全解析(动画, 绘图, 自定义View)

    来源:http://blog.csdn.net/harvic880925/article/details/50995268 一.自定义控件三部曲之动画篇 1.<自定义控件三部曲之动画篇(一)—— ...

  7. Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件

    一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: ...

  8. Android 自定义View修炼-仿QQ5.0 的侧滑菜单效果的实现

    有一段时间没有写博客了,最近比较忙,没什么时间写,刚好今天有点时间, 我就分享下,侧滑菜单的实现原理,一般android侧滑的实现原理和步骤如下:(源码下载在下面最后给出哈) 1.使用ViewGrou ...

  9. Android中自定义View和自定义动画

    Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...

随机推荐

  1. 【POJ 2987】Firing (最小割-最大权闭合子图)

    裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...

  2. [wikioi]最优布线问题

    http://wikioi.com/problem/1231/ Kruskal+并查集.comp函数里面如果用const引用的话,可以减少copy.并查集find的时候是递归找父亲的根.无他. #in ...

  3. VC自动与Internet时间服务器同步更新

    在VCKBASE.CSDN里挖了许久的坟,才找到一些有点用的资料,最后自己整理出这样的个函数,方面VC实现时间同步,多的不说,自己看源码,根据自己的需要可以适当修改源码: #include <W ...

  4. 《鸟哥的Linux私房菜》读书笔记二

    1.Unix的前身是由贝尔实验室(Bell lab.)的Ken Thompson利用汇编语言写成的, 后来在1971-1973年间由Dennis Ritchie以C程序语言进行改写,才称为Unix. ...

  5. Android用户界面 UI组件--自动提示输入框 AutoCompleteTextView和MultiAutoCompleteTextView

    AutoCompleteTextView: 就是一个带自动提示的EditText,当输入字符时,会出现提示. android:completionThreshold  输入几个字符时提示 androi ...

  6. 深入详解SQL中的Null

    深入详解SQL中的Null NULL 在计算机和编程世界中表示的是未知,不确定.虽然中文翻译为 “空”, 但此空(null)非彼空(empty). Null表示的是一种未知状态,未来状态,比如小明兜里 ...

  7. Rocky(dfs)

    题目描述 Sylvester Stallion is an old horse who likes nothing better than to wander around in the fields ...

  8. sql server 2005+ 行转列

    )) , 'aa') , 'bb') , 'aaa') , 'bbb') , 'ccc') go , , '') from tb group by id

  9. Delta-wave

    Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. jquery mobile (一)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...