Android的UI和交互是很重要的一部分,直接影響到用戶對(duì)軟件的體驗(yàn)。隨著項(xiàng)目經(jīng)驗(yàn)的積累,發(fā)現(xiàn)Android中動(dòng)畫(huà)的運(yùn)用越來(lái)越重要。本篇文章抽出了項(xiàng)目登錄界面中實(shí)現(xiàn)的第三方登錄,用戶可以上拉展開(kāi),下拉隱藏第三方登錄這么一個(gè)效果,提高用戶和軟件的交互性。
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、太白網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為太白等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
實(shí)現(xiàn)效果:
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.propertyanimation.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical"> <RelativeLayout android:id="@+id/re_ControlshowhideView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:orientation="vertical"> <RelativeLayout android:id="@+id/re_showarrowhead" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="match_parent" android:layout_height="0.1dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_marginTop="17dip" android:background="#dadada" /> <ImageView android:id="@+id/arrowhead" android:layout_width="30dip" android:layout_height="30dip" android:layout_centerInParent="true" android:src="@drawable/jiantoubelow" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/re_showarrowhead" android:layout_marginTop="10dp" android:gravity="center" android:text="-其他登錄方式-" /> </RelativeLayout> <RelativeLayout android:id="@+id/showhideView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/re_ControlshowhideView" android:gravity="center" android:visibility="gone" android:orientation="vertical"> <Button android:id="@+id/btn_qq" android:layout_width="40dp" android:layout_height="57.5dp" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:background="@drawable/qqlogin" android:clickable="true" android:gravity="center" android:paddingLeft="10dp" android:textSize="16sp" /> <Button android:id="@+id/btn_weixin" android:layout_width="40dp" android:layout_height="57.5dp" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/btn_qq" android:background="@drawable/weixinlogin" android:clickable="true" android:gravity="center" android:paddingLeft="10dp" android:textSize="16sp" /> </RelativeLayout> </RelativeLayout> </RelativeLayout>
(2)PropertyAnimation.java 這個(gè)文件主要是把實(shí)現(xiàn)的屬性動(dòng)畫(huà)封裝到一個(gè)類里,這樣一個(gè)功能就成為一個(gè)模塊。其它調(diào)用者也可以很方便的使用。
package com.example.propertyanimation; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ValueAnimator; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.RotateAnimation; public class PropertyAnimation { private float mDensity; private int mHiddenViewMeasuredHeight; //點(diǎn)擊箭頭的時(shí)候,需要隱藏的控件最終到達(dá)一個(gè)高度, // 這個(gè)就是我們要控件到達(dá)的目標(biāo)值。 public PropertyAnimation(Context context){ //點(diǎn)擊箭頭的時(shí)候,需要隱藏的控件最終到達(dá)一個(gè)高度,這個(gè)就是我們的目標(biāo)值,只需要通過(guò)布局中的dp轉(zhuǎn)換成像素就行了。 mDensity = context.getResources().getDisplayMetrics().density; mHiddenViewMeasuredHeight = (int) (mDensity * 57.5 + 0.5); } public void animateOpen(View v) { v.setVisibility(View.VISIBLE); //createDropAnimator()自定義的一個(gè)動(dòng)畫(huà)效果函數(shù) ValueAnimator animator = createDropAnimator(v, 0, mHiddenViewMeasuredHeight); animator.start(); } /** * 給控制動(dòng)畫(huà)的箭頭設(shè)置動(dòng)畫(huà). * 給箭頭設(shè)置向上的動(dòng)畫(huà) * @param view 控件 */ public void animationIvOpen(View view) { //旋轉(zhuǎn)動(dòng)畫(huà),參數(shù)說(shuō)明:new RotateAnimation(旋轉(zhuǎn)的開(kāi)始角度,旋轉(zhuǎn)的結(jié)束角度,X軸的伸縮模式:可以取值為ABSOLUTE、 // RELATIVE_TO_SELF、RELATIVE_TO_PARENT,X坐標(biāo)的伸縮值,Y軸的伸縮模式:可以取值為ABSOLUTE、RELATIVE_TO_SELF、 // RELATIVE_TO_PARENT,Y坐標(biāo)的伸縮值); RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //動(dòng)畫(huà)執(zhí)行完后是否停留在執(zhí)行完的狀態(tài) animation.setFillAfter(true); //持續(xù)時(shí)間 animation.setDuration(100); //為箭頭圖片綁定動(dòng)畫(huà) view.startAnimation(animation); } /** * 給控制動(dòng)畫(huà)的箭頭設(shè)置動(dòng)畫(huà). * 給箭頭設(shè)置向下的動(dòng)畫(huà) * @param view */ public void animationIvClose(View view) { RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setFillAfter(true); animation.setDuration(100); view.startAnimation(animation); } /** * 設(shè)置隱藏動(dòng)畫(huà) * * @param view //動(dòng)畫(huà)作用的控件 */ public void animateClose(final View view) { //獲得控件的高度 int origHeight = view.getHeight(); //createDropAnimator()自定義的一個(gè)動(dòng)畫(huà)效果函數(shù) ValueAnimator animator = createDropAnimator(view, origHeight, 0); //如果你不想實(shí)現(xiàn)Animator.AnimatorListener中的所有接口,你可以通過(guò)繼承AnimatorListenerAdapter。 //AnimatorListenerAdapter類為所有的方法提供了一個(gè)空實(shí)現(xiàn),所以你可以根據(jù)需要實(shí)現(xiàn)你需要的,覆蓋AnimatorListenerAdapter原來(lái)的方法 animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { //動(dòng)畫(huà)結(jié)束時(shí)調(diào)用 view.setVisibility(View.GONE); } }); animator.start(); } /** * 自定義的動(dòng)畫(huà)效果 * * @param v //動(dòng)畫(huà)作用的控件 * @param start //動(dòng)畫(huà)的開(kāi)始值 * @param end //動(dòng)畫(huà)的結(jié)束值 * @return */ private ValueAnimator createDropAnimator(final View v, int start, int end) { //這里我們利用ValueAnimator.ofInt創(chuàng)建了一個(gè)值從start到end的動(dòng)畫(huà) ValueAnimator animator = ValueAnimator.ofInt(start, end); //為ValueAnimator注冊(cè)AnimatorUpdateListener監(jiān)聽(tīng)器,在該監(jiān)聽(tīng)器中可以 // 監(jiān)聽(tīng)ValueAnimator計(jì)算出來(lái)的值的改變,并將這些值應(yīng)用到指定對(duì)象 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator arg0) { //獲取動(dòng)畫(huà)當(dāng)前值 int value = (int) arg0.getAnimatedValue(); //得到控件的屬性集合 ViewGroup.LayoutParams layoutParams = v.getLayoutParams(); //設(shè)置控件的高屬性 layoutParams.height = value; //把屬性綁定到需要?jiǎng)赢?huà)的控件上 v.setLayoutParams(layoutParams); } }); return animator; } }
(3)MainActivity.java 這個(gè)文件開(kāi)始使用封裝好的屬性動(dòng)畫(huà)了。
package com.example.propertyanimation; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ private ImageView mIv_arrowhead; private RelativeLayout mHiddenLayout; private PropertyAnimation propertyAnimation; private Button btn_qq; //QQ登錄按鈕 private Button btn_weixin; //微信登錄按鈕 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //自己定義的屬性動(dòng)畫(huà)類 propertyAnimation=new PropertyAnimation(this); //隱藏/顯示第三方登錄的箭頭圖標(biāo) mIv_arrowhead = (ImageView) this.findViewById(R.id.arrowhead); mIv_arrowhead.setOnClickListener(this); //隱藏/顯示的布局 mHiddenLayout = (RelativeLayout) this.findViewById(R.id.showhideView); //QQ登錄 btn_qq = (Button) this.findViewById(R.id.btn_qq); btn_qq.setOnClickListener(this); //微信登錄 btn_weixin=(Button)this.findViewById(R.id.btn_weixin); btn_weixin.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.arrowhead: if (mHiddenLayout.getVisibility() == View.GONE) { propertyAnimation.animateOpen(mHiddenLayout); propertyAnimation.animationIvOpen(mIv_arrowhead); } else { propertyAnimation.animateClose(mHiddenLayout); propertyAnimation.animationIvClose(mIv_arrowhead); } break; case R.id.btn_qq: //QQ授權(quán)登錄 Toast.makeText(this,"QQ登錄",Toast.LENGTH_SHORT).show(); break; case R.id.btn_weixin: //微信授權(quán)登錄 Toast.makeText(this,"微信登錄",Toast.LENGTH_SHORT).show(); break; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站名稱:Android實(shí)現(xiàn)第三方登錄的上拉展開(kāi),下拉隱藏,下拉隱藏示例
轉(zhuǎn)載源于:http://muchs.cn/article34/pgoese.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、云服務(wù)器、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)