国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)帶動(dòng)畫效果的可點(diǎn)擊展開(kāi)TextView

2019-12-12 02:30:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

收起(默認(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)稱中國(guó),位于亞洲東部,太平洋西岸, 是工人階級(jí)領(lǐng)導(dǎo)的、以工農(nóng)聯(lián)盟為基礎(chǔ)的人民民主專政的社會(huì)主義國(guó)家。/n" +    "/n" +    "1949年(己丑年)10月1日成立, 以五星紅旗為國(guó)旗, 《義勇軍進(jìn)行曲》為國(guó)歌, 國(guó)徽內(nèi)容包括國(guó)旗、天安門、齒輪和麥稻穗, 首都北京, 省級(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)畫的時(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)聽(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) {  } }}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凭祥市| 罗江县| 上蔡县| 五华县| 吉木萨尔县| 监利县| 舒兰市| 长顺县| 栾城县| 全南县| 肇州县| 阳城县| 板桥市| 丹巴县| 榆中县| 静宁县| 亚东县| 会同县| 沿河| 弋阳县| 上虞市| 正阳县| 汉沽区| 称多县| 河曲县| 阿克陶县| 德阳市| 旅游| 特克斯县| 丰县| 格尔木市| 平塘县| 修武县| 新乡县| 金川县| 临江市| 井冈山市| 开平市| 蓬安县| 华亭县| 华宁县|