亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實現(xiàn)帶動畫效果的可點擊展開TextView

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

本文為大家分享了Android實現(xiàn)帶動畫效果的可點擊展開TextView 制作代碼,效果圖:

收起(默認(rè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>

功能實現(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 = "中華人民共和國,簡稱中國,位于亞洲東部,太平洋西岸, 是工人階級領(lǐng)導(dǎo)的、以工農(nóng)聯(lián)盟為基礎(chǔ)的人民民主專政的社會主義國家。\n" +
    "\n" +
    "1949年(己丑年)10月1日成立, 以五星紅旗為國旗, 《義勇軍進(jìn)行曲》為國歌, 國徽內(nèi)容包括國旗、天安門、齒輪和麥稻穗, 首都北京, 省級行政區(qū)劃為23個省、5個自治區(qū)、4個直轄市、2個特別行政區(qū), 是一個以漢族為主體民族,由56個民族構(gòu)成的統(tǒng)一多民族國家,漢族占總?cè)丝诘?1.51%。\n" +
    "\n" +
    "新中國成立后隨即開展經(jīng)濟(jì)恢復(fù)與建設(shè),1953年開始三大改造, 到1956年確立了社會主義制度,進(jìn)入社會主義探索階段。 文化大革命之后開始改革開放,逐步確立了中國特色社會主義制度。中國陸地面積約960萬平方公里,大陸海岸線1.8萬多千米,島嶼岸線1.4萬多千米,內(nèi)海和邊海的水域面積約470多萬平方千米。海域分布有大小島嶼7600多個,其中臺灣島最大,面積35798平方千米。同14國接壤,與8國海上相鄰。中國是四大文明古國之一, 有著悠久的歷史文化。是世界國土面積第三大的國家,世界第一大人口國家,與英、法、美、俄并為聯(lián)合國安理會五大常任理事國。\n" +
    "\n" +
    "中國是世界第二大經(jīng)濟(jì)體,世界第一貿(mào)易大國,世界第一大外匯儲備國, 世界第一大鋼鐵生產(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行時候的高度

    tv_des.setMaxLines(Integer.MAX_VALUE);//會全部顯示內(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();

      //只有展開動畫的時候才需要內(nèi)容向上滾動,收縮動畫的時候是不需要滾動的
      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) {
  }
 }

}


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論