本文為大家分享了Android實(shí)現(xiàn)帶動(dòng)畫效果的可點(diǎn)擊展開TextView 制作代碼,效果圖:
收起(默認(rèn))效果:

點(diǎn)擊展開后的效果:

源碼:
布局:
<?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>
功能實(shí)現(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;//是否展開整個(gè)描述 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 = "中華人民共和國,簡稱中國,位于亞洲東部,太平洋西岸, 是工人階級(jí)領(lǐng)導(dǎo)的、以工農(nóng)聯(lián)盟為基礎(chǔ)的人民民主專政的社會(huì)主義國家。/n" + "/n" + "1949年(己丑年)10月1日成立, 以五星紅旗為國旗, 《義勇軍進(jìn)行曲》為國歌, 國徽內(nèi)容包括國旗、天安門、齒輪和麥稻穗, 首都北京, 省級(jí)行政區(qū)劃為23個(gè)省、5個(gè)自治區(qū)、4個(gè)直轄市、2個(gè)特別行政區(qū), 是一個(gè)以漢族為主體民族,由56個(gè)民族構(gòu)成的統(tǒng)一多民族國家,漢族占總?cè)丝诘?1.51%。/n" + "/n" + "新中國成立后隨即開展經(jīng)濟(jì)恢復(fù)與建設(shè),1953年開始三大改造, 到1956年確立了社會(huì)主義制度,進(jìn)入社會(huì)主義探索階段。 文化大革命之后開始改革開放,逐步確立了中國特色社會(huì)主義制度。中國陸地面積約960萬平方公里,大陸海岸線1.8萬多千米,島嶼岸線1.4萬多千米,內(nèi)海和邊海的水域面積約470多萬平方千米。海域分布有大小島嶼7600多個(gè),其中臺(tái)灣島最大,面積35798平方千米。同14國接壤,與8國海上相鄰。中國是四大文明古國之一, 有著悠久的歷史文化。是世界國土面積第三大的國家,世界第一大人口國家,與英、法、美、俄并為聯(lián)合國安理會(huì)五大常任理事國。/n" + "/n" + "中國是世界第二大經(jīng)濟(jì)體,世界第一貿(mào)易大國,世界第一大外匯儲(chǔ)備國, 世界第一大鋼鐵生產(chǎn)國和世界第一大農(nóng)業(yè)國,世界第一大糧食總產(chǎn)量國以及世界上經(jīng)濟(jì)成長最快的國家之一。"; 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行時(shí)候的高度 tv_des.setMaxLines(Integer.MAX_VALUE);//會(huì)全部顯示內(nèi)容 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è)置的默認(rèn)顯示行數(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(); //只有展開動(dòng)畫的時(shí)候才需要內(nèi)容向上滾動(dòng),收縮動(dòng)畫的時(shí)候是不需要滾動(dòng)的 if (!isExpandDes) { int scrollY = currentHeight - minHeight; scrollView.scrollBy(0, scrollY); } } }); desAnimator.setDuration(300); desAnimator.addListener(new DesAnimListener()); desAnimator.start(); break; } } /** * 描述區(qū)域動(dòng)畫的監(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) { } }}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注