Android編程之界面跳動(dòng)提示動(dòng)畫(huà)效果實(shí)現(xiàn)方法
本文實(shí)例講述了Android編程之界面跳動(dòng)提示動(dòng)畫(huà)效果實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
上一個(gè)效果圖:
先上布局:
<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" > <RelativeLayout android:id="@+id/red" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch2blue" android:layout_centerHorizontal="true" android:text="首頁(yè)" /> <Button android:id="@+id/switch2blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="置換位藍(lán)色" /> </RelativeLayout> <RelativeLayout android:id="@+id/blue" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch2red" android:layout_centerHorizontal="true" android:text="第二頁(yè)" /> <Button android:id="@+id/switch2red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="置換位紅色" /> </RelativeLayout> </RelativeLayout>
代碼如下:
import java.lang.reflect.Field; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.DisplayMetrics; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.GestureDetector.OnGestureListener; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.view.animation.Animation.AnimationListener; import android.widget.Button; import android.widget.RelativeLayout; public class MainActivity extends Activity implements OnClickListener, OnTouchListener, OnGestureListener { private RelativeLayout red, blue; private Button switch2blue, switch2red; private float thisDelta = 0.05f; private static boolean hasEverPulled = false; private boolean pulled = false; private static int height; private GestureDetector gestureDetector; private static int statusHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayMetrics metrics = getResources().getDisplayMetrics(); height = metrics.heightPixels; statusHeight = getStatusHeight(); initView(); // 跳動(dòng)提示可以上拉 final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { jump(thisDelta); // handler.postDelayed(this, 3000); } }, 3000); } private void initView() { red = (RelativeLayout) findViewById(R.id.red); blue = (RelativeLayout) findViewById(R.id.blue); switch2blue = (Button) findViewById(R.id.switch2blue); switch2red = (Button) findViewById(R.id.switch2red); switch2blue.setOnClickListener(this); switch2red.setOnClickListener(this); blue.setOnTouchListener(this); blue.setLongClickable(true); gestureDetector = new GestureDetector(this, this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.switch2blue: red2BlueUI(); break; case R.id.switch2red: blue2RedUI(); break; } } // 從紅頁(yè)面轉(zhuǎn)到藍(lán)頁(yè)面 private void red2BlueUI() { blue.bringToFront(); blue.requestLayout(); blue.invalidate(); } // 從藍(lán)頁(yè)面轉(zhuǎn)到紅頁(yè)面 private void blue2RedUI() { red.bringToFront(); red.requestLayout(); red.invalidate(); } // 獲取狀態(tài)欄的高度 private int getStatusHeight() { Class<?> c = null; Object obj = null; Field field = null; int x = 0; int height = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); height = getResources().getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } return height; } // 主頁(yè)面跳動(dòng) private void jump(float delta) { if (thisDelta - 0.03f < 0.001f) { thisDelta = 0.05f; return; } thisDelta = delta; if (hasEverPulled) { return; } playJumpAnimation(thisDelta); } // 上拉/下拉主頁(yè)面 private void pull(boolean upward) { if (upward && pulled) { return; } if (!upward && !pulled) { return; } float originalY; float finalY; if (!pulled) { originalY = 0; finalY = (float) (0 - height + 0.4 * height); } else { originalY = (float) (0 - height + 0.4 * height); finalY = 0; } pulled = !pulled; AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY)); animationSet.setDuration(300); animationSet.setInterpolator(new AccelerateDecelerateInterpolator()); animationSet.setFillAfter(true); animationSet.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { if (!pulled) { red2BlueUI(); } } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { // if (pulled) { // blue2RedUI(); // } } }); blue.startAnimation(animationSet); hasEverPulled = true; } // 跳起動(dòng)畫(huà) private void playJumpAnimation(final float delta) { float originalY = 0; float finalY = 0 - height * delta; AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY)); animationSet.setDuration(300); animationSet.setInterpolator(new AccelerateDecelerateInterpolator()); animationSet.setFillAfter(true); animationSet.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { playLandAnimation(delta); } }); blue.startAnimation(animationSet); } // 落下動(dòng)畫(huà) private void playLandAnimation(final float delta) { float originalY = 0 - height * delta; float finalY = 0; AnimationSet animationSet = new AnimationSet(true); animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY)); animationSet.setDuration(200); animationSet.setInterpolator(new AccelerateInterpolator()); animationSet.setFillAfter(true); animationSet.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { jump(0.03f); } }); blue.startAnimation(animationSet); } @Override public boolean onDown(MotionEvent e) { pull(false); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 手勢(shì)滑動(dòng)達(dá)到100才觸發(fā) if (e1.getY() - e2.getY() > 100) { pull(true); } else if (e2.getY() >= e1.getY()) { pull(false); } return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onTouch(View v, MotionEvent event) { if (pulled) { // 首張頁(yè)可觸控點(diǎn) if (event.getY() > height * 0.4 - statusHeight) { return false; } } return gestureDetector.onTouchEvent(event); } }
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程實(shí)現(xiàn)仿心跳動(dòng)畫(huà)效果的方法
- 三款A(yù)ndroid炫酷Loading動(dòng)畫(huà)組件推薦
- Android 使用XML做動(dòng)畫(huà)UI的深入解析
- Android Tween動(dòng)畫(huà)之RotateAnimation實(shí)現(xiàn)圖片不停旋轉(zhuǎn)效果實(shí)例介紹
- Android 吸入動(dòng)畫(huà)效果實(shí)現(xiàn)分解
- Android動(dòng)畫(huà)之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
- Android圖片翻轉(zhuǎn)動(dòng)畫(huà)簡(jiǎn)易實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動(dòng)下落功能實(shí)例
- Android游戲開(kāi)發(fā)學(xué)習(xí)①?gòu)椞∏驅(qū)崿F(xiàn)方法
- Android實(shí)現(xiàn)跳動(dòng)的小球加載動(dòng)畫(huà)效果
相關(guān)文章
Android ApplicationContext接口深入分析
ApplicationContext是Spring應(yīng)用程序中的中央接口,由于繼承了多個(gè)組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊(cè)監(jiān)聽(tīng)事件,加載資源文件等2022-11-11Android編程Widget創(chuàng)建與使用方法簡(jiǎn)明教程
這篇文章主要介紹了Android編程Widget創(chuàng)建與使用方法,結(jié)合實(shí)例形式分析了Widget的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-10-10Android自定義HorizontalScrollView打造超強(qiáng)Gallery效果
這篇文章主要介紹了Android自定義HorizontalScrollView打造圖片橫向滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android訪問(wèn)assets本地json文件的方法
這篇文章主要介紹了Android訪問(wèn)assets本地json文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
這篇文章主要介紹了Android開(kāi)發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能,結(jié)合實(shí)例形式分析了Android DatePicker和TimePicker組件的功能、常用函數(shù)、布局及日期時(shí)間選擇相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android學(xué)習(xí)教程之下拉刷新實(shí)現(xiàn)代碼(11)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之下拉刷新實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填充
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)獲取短信驗(yàn)證碼并自動(dòng)填充的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04Android實(shí)現(xiàn)按鈕點(diǎn)擊事件的三種方法總結(jié)
Button是程序用于和用戶進(jìn)行交互的一個(gè)重要控件。既然有Button,那肯定有onClick方法,下面就教大家三種實(shí)現(xiàn)點(diǎn)擊事件的方法,感興趣的可以了解一下2022-04-04Kotlin類(lèi)對(duì)象class初始化與使用
Kotlin 是一種追求簡(jiǎn)潔的語(yǔ)言,在類(lèi)上也下了不少功夫,放棄了很多c++ 中類(lèi)非常復(fù)雜的概念,其實(shí)對(duì)于類(lèi)可以這樣來(lái)理解,為了復(fù)用的方便性和完整性,我們把變量和函數(shù)組合在一起,形成了類(lèi)的概念2022-12-12Android 簡(jiǎn)單的圖片查看器源碼實(shí)現(xiàn)
本篇文章主要介紹了Android 簡(jiǎn)單的圖片查看器源碼實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09