Android實現(xiàn)帶動畫效果的可點擊展開TextView
更新時間:2017年07月10日 16:15:14 作者:u014620028
這篇文章主要為大家詳細介紹了Android實現(xiàn)帶動畫效果的可點擊展開TextView,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了Android實現(xiàn)帶動畫效果的可點擊展開TextView 制作代碼,效果圖:
收起(默認)效果:

點擊展開后的效果:

源碼:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6f6f6"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="簡介"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_des"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#666666"
android:textSize="18sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_des_arrow"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:background="@mipmap/arrow_down"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
功能實現(xiàn):
package com.cnfol.demo;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private TextView tv_des;
private ImageView iv_des_arrow;
private boolean isExpandDes = false;//是否展開整個描述
private int minHeight = 0;
private int maxHeight = 0;
private ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.sv);
tv_des = (TextView) findViewById(R.id.tv_des);
tv_des.setOnClickListener(this);
iv_des_arrow = (ImageView) findViewById(R.id.iv_des_arrow);
iv_des_arrow.setOnClickListener(this);
String s = "中華人民共和國,簡稱中國,位于亞洲東部,太平洋西岸, 是工人階級領導的、以工農聯(lián)盟為基礎的人民民主專政的社會主義國家。\n" +
"\n" +
"1949年(己丑年)10月1日成立, 以五星紅旗為國旗, 《義勇軍進行曲》為國歌, 國徽內容包括國旗、天安門、齒輪和麥稻穗, 首都北京, 省級行政區(qū)劃為23個省、5個自治區(qū)、4個直轄市、2個特別行政區(qū), 是一個以漢族為主體民族,由56個民族構成的統(tǒng)一多民族國家,漢族占總人口的91.51%。\n" +
"\n" +
"新中國成立后隨即開展經(jīng)濟恢復與建設,1953年開始三大改造, 到1956年確立了社會主義制度,進入社會主義探索階段。 文化大革命之后開始改革開放,逐步確立了中國特色社會主義制度。中國陸地面積約960萬平方公里,大陸海岸線1.8萬多千米,島嶼岸線1.4萬多千米,內海和邊海的水域面積約470多萬平方千米。海域分布有大小島嶼7600多個,其中臺灣島最大,面積35798平方千米。同14國接壤,與8國海上相鄰。中國是四大文明古國之一, 有著悠久的歷史文化。是世界國土面積第三大的國家,世界第一大人口國家,與英、法、美、俄并為聯(lián)合國安理會五大常任理事國。\n" +
"\n" +
"中國是世界第二大經(jīng)濟體,世界第一貿易大國,世界第一大外匯儲備國, 世界第一大鋼鐵生產(chǎn)國和世界第一大農業(yè)國,世界第一大糧食總產(chǎn)量國以及世界上經(jīng)濟成長最快的國家之一。";
tv_des.setText(s);
tv_des.setMaxLines(3);
tv_des.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//一般用完之后,立即移除該監(jiān)聽
tv_des.getViewTreeObserver().removeGlobalOnLayoutListener(this);
minHeight = tv_des.getMeasuredHeight();//獲取3行時候的高度
tv_des.setMaxLines(Integer.MAX_VALUE);//會全部顯示內容
tv_des.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//一般用完之后,立即移除該監(jiān)聽
tv_des.getViewTreeObserver().removeGlobalOnLayoutListener(this);
maxHeight = tv_des.getMeasuredHeight();//獲取總高度
if (minHeight == maxHeight) {
//最大高度和最小高度一樣。說明設置的默認顯示行數(shù),已經(jīng)可以把所有數(shù)據(jù)全部顯示
iv_des_arrow.setVisibility(View.GONE);
}
tv_des.getLayoutParams().height = minHeight;
tv_des.requestLayout();//讓tv_des顯示為3行的高度
}
});
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_des:
case R.id.iv_des_arrow:
ValueAnimator desAnimator = null;
if (isExpandDes) {
desAnimator = ValueAnimator.ofInt(maxHeight, minHeight);
} else {
desAnimator = ValueAnimator.ofInt(minHeight, maxHeight);
}
desAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
int currentHeight = (Integer) animator.getAnimatedValue();
tv_des.getLayoutParams().height = currentHeight;
tv_des.requestLayout();
//只有展開動畫的時候才需要內容向上滾動,收縮動畫的時候是不需要滾動的
if (!isExpandDes) {
int scrollY = currentHeight - minHeight;
scrollView.scrollBy(0, scrollY);
}
}
});
desAnimator.setDuration(300);
desAnimator.addListener(new DesAnimListener());
desAnimator.start();
break;
}
}
/**
* 描述區(qū)域動畫的監(jiān)聽
*
* @author Administrator
*/
class DesAnimListener implements Animator.AnimatorListener {
@Override
public void onAnimationCancel(Animator arg0) {
}
@Override
public void onAnimationEnd(Animator arg0) {
isExpandDes = !isExpandDes;
iv_des_arrow.setBackgroundResource(isExpandDes ? R.mipmap.arrow_up : R.mipmap.arrow_down);
}
@Override
public void onAnimationRepeat(Animator arg0) {
}
@Override
public void onAnimationStart(Animator arg0) {
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android UI設計與開發(fā)之實現(xiàn)應用程序只啟動一次引導界面
這篇文章主要為大家詳細介紹了Android UI設計與開發(fā)之實現(xiàn)應用程序只啟動一次引導界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android ListView下拉刷新上拉自動加載更多DEMO示例
這篇文章主要介紹了Android ListView下拉刷新上拉自動加載更多DEMO示例的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Android神兵利器之Image Asset Studio的實現(xiàn)
這篇文章主要介紹了Android神兵利器之Image Asset Studio的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Android音視頻開發(fā)Media FrameWork框架源碼解析
這篇文章主要為大家介紹了Android音視頻開發(fā)Media FrameWork框架源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android SQLite操作之大數(shù)據(jù)處理與同時讀寫方法
這篇文章主要介紹了Android SQLite操作之大數(shù)據(jù)處理與同時讀寫方法,實例分析了Android操作SQLite時基于事務的數(shù)據(jù)緩存與批量插入技巧,以及同時讀寫的相關實現(xiàn)方法與注意事項,需要的朋友可以參考下2016-07-07

