安卓创建下拉功能
xml代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="MainActivity" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#458EFD"android:padding="10dip" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:gravity="center_vertical"android:text="下拉展開動(dòng)畫"android:textColor="#ffffff"android:textSize="20sp" /></RelativeLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#548AEA"android:gravity="center"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="20dip"android:text="這是下拉前"android:textColor="#ffffff"android:textSize="16sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:textColor="#ffffff"android:textSize="45sp" /></LinearLayout><LinearLayoutandroid:id="@+id/linear_hidden"android:layout_width="match_parent"android:layout_height="120dip"android:background="#548AEA"android:gravity="center"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="20dip"android:text="這是下拉后"android:textColor="#ffffff"android:textSize="16sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1"android:textColor="#ffffff"android:textSize="45sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#548AEA"android:gravity="center"android:onClick="onClick"android:orientation="vertical" ><ImageViewandroid:id="@+id/my_iv"android:layout_width="40dip"android:layout_height="40dip"android:layout_margin="20dip"android:src="@drawable/xiala" /></LinearLayout></LinearLayout>
java代碼:
package com.example.myapplication6;import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity {private LinearLayout mHiddenLayout;//隱藏的布局private float mDensity;//屏幕密度,通過屏幕密度計(jì)算高度,在布局的時(shí)候最好要用dipprivate int mHiddenViewMeasuredHeight;//需要的高度private ImageView mIv;//圖像視圖@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);mHiddenLayout = (LinearLayout) findViewById(R.id.linear_hidden);mIv = (ImageView) findViewById(R.id.my_iv);mDensity = getResources().getDisplayMetrics().density;mHiddenViewMeasuredHeight = (int) (mDensity * 120 + 0.5);}public void onClick(View v) {if (mHiddenLayout.getVisibility() == View.GONE) {//返回值有View.VISIBLE(可見)、View.INVISIBLE(不可見但占著原來的空間)和View.GONE( 不可見且不占原來的空間)//點(diǎn)擊后如果是不可見的狀態(tài)要讓它可見animateOpen(mHiddenLayout);animationIvOpen();} else {animateClose(mHiddenLayout);animationIvClose();}}private void animateOpen(View v) {v.setVisibility(View.VISIBLE);//設(shè)置為可見狀態(tài)ValueAnimator animator = createDropAnimator(v, 0,mHiddenViewMeasuredHeight);//這個(gè)類提供了一個(gè)用于計(jì)算動(dòng)畫值并將其設(shè)置為目標(biāo)對象的動(dòng)畫的簡單時(shí)序引擎。//默認(rèn)情況下,ValueAnimator采用非線性時(shí)域插值,通過AccelerateDecelerateInterpolator類,加速和減速的動(dòng)畫。//這種行為可以通過調(diào)用setinterpolator改變(timeinterpolator)animator.start();}private void animationIvOpen() {RotateAnimation animation = new RotateAnimation(0, 180,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);//旋轉(zhuǎn)動(dòng)畫,這個(gè)動(dòng)畫用于下拉符號的旋轉(zhuǎn)animation.setFillAfter(true);animation.setDuration(100);mIv.startAnimation(animation);}private void animationIvClose() {RotateAnimation animation = new RotateAnimation(180, 0,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);animation.setFillAfter(true);animation.setDuration(100);mIv.startAnimation(animation);}private void animateClose(final View view) {int origHeight = view.getHeight();//獲取目前的高度ValueAnimator animator = createDropAnimator(view, origHeight, 0);//高度變?yōu)?即重新隱藏animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {view.setVisibility(View.GONE);//重新變?yōu)椴豢梢暊顟B(tài)}});animator.start();}private ValueAnimator createDropAnimator(final View v, int start, int end) {ValueAnimator animator = ValueAnimator.ofInt(start, end);animator.addUpdateListener(new AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator arg0) {int value = (int) arg0.getAnimatedValue();ViewGroup.LayoutParams layoutParams = v.getLayoutParams();layoutParams.height = value;v.setLayoutParams(layoutParams);}});return animator;}}
總結(jié)
- 上一篇: mac android屏幕演示,如何在M
- 下一篇: 虚拟专题:知识图谱 | 频谱知识图谱:面