您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)帶動(dòng)畫(huà)效果的可點(diǎn)擊展開(kāi)TextView,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
效果圖:
收起(默認(rèn))效果:
點(diǎn)擊展開(kāi)后的效果:
源碼:
布局:
<?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="簡(jiǎn)介" 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;//是否展開(kāi)整個(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 = "中華人民共和國(guó),簡(jiǎn)稱(chēng)中國(guó),位于亞洲東部,太平洋西岸, 是工人階級(jí)領(lǐng)導(dǎo)的、以工農(nóng)聯(lián)盟為基礎(chǔ)的人民民主專(zhuān)政的社會(huì)主義國(guó)家。\n" + "\n" + "1949年(己丑年)10月1日成立, 以五星紅旗為國(guó)旗, 《義勇軍進(jìn)行曲》為國(guó)歌, 國(guó)徽內(nèi)容包括國(guó)旗、天安門(mén)、齒輪和麥稻穗, 首都北京, 省級(jí)行政區(qū)劃為23個(gè)省、5個(gè)自治區(qū)、4個(gè)直轄市、2個(gè)特別行政區(qū), 是一個(gè)以漢族為主體民族,由56個(gè)民族構(gòu)成的統(tǒng)一多民族國(guó)家,漢族占總?cè)丝诘?1.51%。\n" + "\n" + "新中國(guó)成立后隨即開(kāi)展經(jīng)濟(jì)恢復(fù)與建設(shè),1953年開(kāi)始三大改造, 到1956年確立了社會(huì)主義制度,進(jìn)入社會(huì)主義探索階段。 文化大革命之后開(kāi)始改革開(kāi)放,逐步確立了中國(guó)特色社會(huì)主義制度。中國(guó)陸地面積約960萬(wàn)平方公里,大陸海岸線1.8萬(wàn)多千米,島嶼岸線1.4萬(wàn)多千米,內(nèi)海和邊海的水域面積約470多萬(wàn)平方千米。海域分布有大小島嶼7600多個(gè),其中臺(tái)灣島最大,面積35798平方千米。同14國(guó)接壤,與8國(guó)海上相鄰。中國(guó)是四大文明古國(guó)之一, 有著悠久的歷史文化。是世界國(guó)土面積第三大的國(guó)家,世界第一大人口國(guó)家,與英、法、美、俄并為聯(lián)合國(guó)安理會(huì)五大常任理事國(guó)。\n" + "\n" + "中國(guó)是世界第二大經(jīng)濟(jì)體,世界第一貿(mào)易大國(guó),世界第一大外匯儲(chǔ)備國(guó), 世界第一大鋼鐵生產(chǎn)國(guó)和世界第一大農(nóng)業(yè)國(guó),世界第一大糧食總產(chǎn)量國(guó)以及世界上經(jīng)濟(jì)成長(zhǎng)最快的國(guó)家之一。"; tv_des.setText(s); tv_des.setMaxLines(3); tv_des.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //一般用完之后,立即移除該監(jiān)聽(tīng) 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)聽(tīng) tv_des.getViewTreeObserver().removeGlobalOnLayoutListener(this); maxHeight = tv_des.getMeasuredHeight();//獲取總高度 if (minHeight == maxHeight) { //最大高度和最小高度一樣。說(shuō)明設(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(); //只有展開(kāi)動(dòng)畫(huà)的時(shí)候才需要內(nèi)容向上滾動(dòng),收縮動(dòng)畫(huà)的時(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)畫(huà)的監(jiān)聽(tīng) * * @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) { } } }
關(guān)于“Android如何實(shí)現(xiàn)帶動(dòng)畫(huà)效果的可點(diǎn)擊展開(kāi)TextView”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。