一.值动画展开线性布局
点击内容显示隐藏内容
/**安全Holder设置动画效果*/
public class DetailSafeHolder extends BaseHolder<Appinfo> implements
OnClickListener {
@ViewInject(R.id.safe_layout)
private RelativeLayout safe_layout;
@ViewInject(R.id.safe_content)
private LinearLayout safe_content;
@ViewInject(R.id.safe_arrow)
private ImageView safe_arrow;
ImageView[] ivs;
ImageView[] iv_des;
TextView[] tv_des;
LinearLayout[] des_layout;
@Override
protected View initView() {
View view = View.inflate(BaseApplication.getApplication(),
R.layout.detail_safe, null);
ViewUtils.inject(this, view);
ivs = new ImageView[4]; // 初始化标题栏的图片
ivs[0] = (ImageView) view.findViewById(R.id.iv_1);
ivs[1] = (ImageView) view.findViewById(R.id.iv_2);
ivs[2] = (ImageView) view.findViewById(R.id.iv_3);
ivs[3] = (ImageView) view.findViewById(R.id.iv_4);
iv_des = new ImageView[4]; // 初始化每个条目描述的图片
iv_des[0] = (ImageView) view.findViewById(R.id.des_iv_1);
iv_des[1] = (ImageView) view.findViewById(R.id.des_iv_2);
iv_des[2] = (ImageView) view.findViewById(R.id.des_iv_3);
iv_des[3] = (ImageView) view.findViewById(R.id.des_iv_4);
tv_des = new TextView[4]; // 初始化每个条目描述的文本
tv_des[0] = (TextView) view.findViewById(R.id.des_tv_1);
tv_des[1] = (TextView) view.findViewById(R.id.des_tv_2);
tv_des[2] = (TextView) view.findViewById(R.id.des_tv_3);
tv_des[3] = (TextView) view.findViewById(R.id.des_tv_4);
des_layout = new LinearLayout[4]; // 初始化条目线性布局
des_layout[0] = (LinearLayout) view.findViewById(R.id.des_layout_1);
des_layout[1] = (LinearLayout) view.findViewById(R.id.des_layout_2);
des_layout[2] = (LinearLayout) view.findViewById(R.id.des_layout_3);
des_layout[3] = (LinearLayout) view.findViewById(R.id.des_layout_4);
safe_layout.setOnClickListener(this);
//默认初始化不显示
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) safe_content.getLayoutParams();
params.height = 0;
safe_content.setLayoutParams(params);
return view;
}
@Override
protected void refreshView(Appinfo data) {
List<String> safeUrl = data.getSafeUrl();//大图片url
List<String> safeDesUrl = data.getSafeDesUrl();//小图片url
List<String> safeDes = data.getSafeDes();//描述
List<Integer> safeDesColor = data.getSafeDesColor(); // 文字颜色0 1 2 3
// 如果个数item或者其中一个少于4个,就将多的那个隐藏
for (int i = 0; i < 4; i++) {
if (i < safeUrl.size() && i < safeDesUrl.size()
&& i < safeDes.size() && i < safeDesColor.size()) {
ivs[i].setVisibility(View.VISIBLE);
des_layout[i].setVisibility(View.VISIBLE);
//设置图片和描述
BitmapHelper.getBitmapUtils().display(ivs[i],
FileUtils.URL + "image?name=" + safeUrl.get(i));
BitmapHelper.getBitmapUtils().display(iv_des[i],
FileUtils.URL + "image?name=" + safeDesUrl.get(i));
tv_des[i].setText(safeDes.get(i));
// 根据返回的颜色类型设置不同的颜色
int color;
int colorType = safeDesColor.get(i);
if (colorType >= 1 && colorType <= 3) {
color = Color.rgb(255, 153, 0); // 00 00 00
} else if (colorType == 4) {
color = Color.rgb(0, 177, 62);
} else {
color = Color.rgb(122, 122, 122);
}
tv_des[i].setTextColor(color);
} else {
ivs[i].setVisibility(View.GONE);
des_layout[i].setVisibility(View.GONE);
}
}
}
boolean flag ;// 默认关闭
int startHeight;
int endHeight;
@Override
public void onClick(View v) {
if (!flag) {// 如果是关闭,点击就打开
flag = true;
// safe_content.setVisibility(View.GONE);
startHeight = 0;
endHeight = getMeasureHeight();
} else { // 否则就关闭
flag = false;
startHeight = getMeasureHeight();
endHeight = 0;
// safe_content.setVisibility(View.VISIBLE);
}
//设置值动画,变化
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) safe_content.getLayoutParams();
ValueAnimator animator = ValueAnimator.ofInt(startHeight,endHeight);
<span style="white-space:pre"> </span>//监听动画的变化 animation.getAnimatedValue在一定时间内容按照 startHeight —>endHeight 变化
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
params.height = (Integer) animation.getAnimatedValue();
safe_content.setLayoutParams(params);
}
});
animator.setDuration(400);
animator.start();
//动画的监听
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
//动画完之后改变箭头
@Override
public void onAnimationEnd(Animator animation) {
if(flag){
safe_arrow.setImageResource(R.drawable.arrow_up);
}else{
safe_arrow.setImageResource(R.drawable.arrow_down);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
}
//获取控件safe_layout的测量的高
public int getMeasureHeight() {
int width = safe_content.getMeasuredHeight();// 宽是和匹配父窗体的,不变
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width,MeasureSpec.EXACTLY);//宽度精确
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000,MeasureSpec.AT_MOST);//高度设置合适的值,1000
//自定义该线性布局的规则
safe_content.measure(widthMeasureSpec, heightMeasureSpec);
<span style="white-space:pre"> </span>//safe_content.measure(0,0);
return safe_content.getMeasuredHeight();
}
}
二.再来一次.动画
public class DetailDesHolder extends BaseHolder<Appinfo> implements
OnClickListener {
@ViewInject(R.id.des_content)
private TextView des_content;
@ViewInject(R.id.des_author)
private TextView des_author;
@ViewInject(R.id.des_arrow)
private ImageView des_arrow;
@ViewInject(R.id.des_layout)
private RelativeLayout des_layout;
@Override
protected View initView() {
View view = View.inflate(BaseApplication.getApplication(),
R.layout.detail_des, null);
ViewUtils.inject(this, view);
return view;
}
@Override
protected void refreshView(Appinfo data) {
scrollView = getScrollView(des_content);
des_content.setText(data.getDes());
des_author.setText("作者:" + data.getAuthor());
// 默认设置的高度显示7行
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) des_content
.getLayoutParams();
layoutParams.height = getShortHeight();
des_content.setLayoutParams(layoutParams);
// 默认设置的箭头
des_arrow.setImageResource(R.drawable.arrow_down);
des_layout.setOnClickListener(this);
}
//获取ScrollView
private ScrollView getScrollView(View view) {
ViewParent parent = view.getParent();
if(parent instanceof ViewGroup){
ViewGroup group = (ViewGroup)parent;
if(group instanceof ScrollView){
return (ScrollView)group;
}else{
return getScrollView(group);
}
}else{
return null;
}
}
boolean flag = false;
private ScrollView scrollView;
@Override
public void onClick(View v) {
int startHeight;
int endHeight;
if(!flag){
flag = true;
startHeight = getShortHeight();
endHeight = getLongHeight();
}else{
flag = false;
startHeight = getLongHeight();
endHeight = getShortHeight();
}
/**值动画从startHeight--> endHeight*/
final ValueAnimator animator = ValueAnimator.ofInt(startHeight,endHeight);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator arg0) {
// TODO Auto-generated method stub
int animatedValue = (Integer) animator.getAnimatedValue();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) des_content.getLayoutParams();
layoutParams.height = animatedValue;
des_content.setLayoutParams(layoutParams);
scrollView.scrollTo(0, scrollView.getMeasuredHeight());//滑动到低端
}
});
animator.setDuration(500);
animator.start();
//监听动画完成后的事件,实现箭头的变化
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator arg0) {
}
@Override
public void onAnimationRepeat(Animator arg0) {
}
@Override
public void onAnimationEnd(Animator arg0) {
if(flag){
des_arrow.setImageResource(R.drawable.arrow_up);
}else{
des_arrow.setImageResource(R.drawable.arrow_down);
}
}
@Override
public void onAnimationCancel(Animator arg0) {
}
});
}
// 获得显示7行的的高度
public int getShortHeight() {
TextView textView = new TextView(BaseApplication.getApplication());
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);// 设置字体大小14dp
textView.setMaxLines(7);
textView.setLines(7);// 管你有多少行,都必须显示7行
// int width = des_content.getMeasuredWidth();
textView.measure(0, 0);//通知测量
int measuredHeight = textView.getMeasuredHeight();
return measuredHeight;
}
// 获得整个高度
public int getLongHeight() {
int width = des_content.getMeasuredWidth();
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
MeasureSpec.EXACTLY);// 以实际测量为标准
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000,
MeasureSpec.AT_MOST);// 最大标准,为1000
des_content.measure(widthMeasureSpec, heightMeasureSpec);
int measuredHeight = des_content.getMeasuredHeight();
System.out.println("long" + measuredHeight);
return measuredHeight;
}
}