Android编程之界面跳动提示动画效果实现方法

   2015-11-25 0
核心提示:这篇文章主要介绍了Android编程之界面跳动提示动画效果实现方法,实例分析了Android动画效果的布局及功能相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android编程之界面跳动提示动画效果实现方法。分享给大家供大家参考,具体如下:

上一个效果图:

Android编程之界面跳动提示动画效果实现方法

Android编程之界面跳动提示动画效果实现方法

先上布局:

<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="首页" />
  <Button
   android:id="@+id/switch2blue"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:text="置换位蓝色" />
 </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="第二页" />
  <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();
  // 跳动提示可以上拉
  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;
  }
 }
 // 从红页面转到蓝页面
 private void red2BlueUI() {
  blue.bringToFront();
  blue.requestLayout();
  blue.invalidate();
 }
 // 从蓝页面转到红页面
 private void blue2RedUI() {
  red.bringToFront();
  red.requestLayout();
  red.invalidate();
 }
 // 获取状态栏的高度
 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;
 }
 // 主页面跳动
 private void jump(float delta) {
  if (thisDelta - 0.03f < 0.001f) {
   thisDelta = 0.05f;
   return;
  }
  thisDelta = delta;
  if (hasEverPulled) {
   return;
  }
  playJumpAnimation(thisDelta);
 }
 // 上拉/下拉主页面
 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 onAnimation
Start(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;
 }
 // 跳起动画
 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 onAnimation
Start(Animation animation) {
   }
   @Override
   public void onAnimationRepeat(Animation animation) {
   }
   @Override
   public void onAnimationEnd(Animation animation) {
    playLandAnimation(delta);
   }
  });
  blue.startAnimation(animationSet);
 }
 // 落下动画
 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 onAnimation
Start(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) {
  // 手势滑动达到100才触发
  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) {
   // 首张页可触控点
   if (event.getY() > height * 0.4 - statusHeight) {
    return false;
   }
  }
  return gestureDetector.onTouchEvent(event);
 }
}

希望本文所述对大家Android程序设计有所帮助。

 
标签: Android 提示 动画
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 说一说Android Studio和IDEA中一个很有用的内存调试插件
    说一说Android Studio和IDEA中一个很有用的内存
    JetBrains JVM Debugger Memory View plugin 在我最近的研发活动期间寻找新的工具,以提高我的开发经验,使Android Studio的生活更轻松,我发现一个有用的插件,我从来没有听说过。 这就是为什么,我决定写这个强大的工具,它如何帮助我与内存调试我的应用程
  • 安卓中通知功能的具体实现
    安卓中通知功能的具体实现
    通知[Notification]是Android中比较有特色的功能,当某个应用程序希望给用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知实现。使用通知的步骤1、需要一个NotificationManager来获得NotificationManager manager = (NotificationManager
    02-05 安卓开发
  • Android view系统分析-setContentView
    Android view系统分析-setContentView
    第一天上班,列了一下今年要学习的东西。主要就是深入学习Android相关的系统源代码,夯实基础。对于学习Android系统源代码,也没什么大概,就从我们平常使用最基础的东西学起,也就是从view这个切入点开始学习Android的源码,在没分析源码之前,我们有的时候
    02-05 安卓开发
  • 如何进行网络视频截图/获取视频的缩略图
    如何进行网络视频截图/获取视频的缩略图
    小编导读:获取视频的缩略图,截图正在播放的视频某一帧,是在音视频开发中,常遇到的问题。本文是主要用于点播中截图视频,同时还可以获取点播视频的缩略图进行显示,留下一个问题,如下图所示, 如果要获取直播中节目视频缩略图,该怎么做呢?(ps:直播是直
  • Android NDK 层发起 HTTP 请求的问题及解决
    Android NDK 层发起 HTTP 请求的问题及解决
    前言新的一年,大家新年快乐~~鸡年大吉!本次给大家带来何老师的最新文章~虽然何老师还在过节,但依然放心不下广大开发者,在此佳节还未结束之际,给大家带来最新的技术分享~ 事件的起因不说了,总之是需要实现一个 NDK 层的网络请求。为了多端适用,还是选择
  • SDK热更之如何在SDK代码中自动插桩及如何生成补
    写在前面本文是SDKHotfix相关的SDK热更系列文章中的一篇,以下为项目及系列文章相关链接:SDKHotfix整体介绍:http://blog.bihe0832.com/sdk_hotfix_project.htmlSDKHotfix对应github地址:https://github.com/bihe0832/SDKHoxFix这篇文章主要介绍一下SDK热更
  • 安装量破千万的第一个产品,我总结了3句话
    安装量破千万的第一个产品,我总结了3句话
    在今天的文章中,作者回顾了自己的第一个产品,他说“我做的第一款产品,是我的一块里程碑。”一起来看看~背景老牌大型互联网公司,部门内部创业的一个项目。我作为产品经理,也是第一次做产品经理,主导产品项目。实际上,项目初期包括我和安卓开发2个人。开
  • 移动周刊第 176 期:Android 知识梳理
    移动周刊第 176 期:Android 知识梳理
    写在前面 本期移动周刊第 176 期如约而至,聚焦 Android、iOS、VR/AR/MR、直播等前沿移动开发技术,收录一周最热点,解读开发技巧,每周三移动周刊抢先看,我们希望从中能够让你有一些收获,如果你有好的文章以及优化建议,请发送邮件至mobilehub@csdn.net,
  • Android插件化(六): OpenAtlasの改写aapt以防止资源ID冲突
    Android插件化(六): OpenAtlasの改写aapt以防
    引言Android应用程序的编译中,负责资源打包的是aapt,如果不对打包后的资源ID进行控制,就会导致插件中的资源ID冲突。所以,我们需要改写aapt的源码,以达到通过某种方式传递资源ID的Package ID,通过aapt打包时获取到这个Package ID并且应用才插件资源的命名
    02-05 安卓开发
  • Android架构(一)MVP架构在Android中的实践
    Android架构(一)MVP架构在Android中的实践
    为什么要重视程序的架构设计 对程序进行架构设计的原因,归根结底是为了 提高生产力 。通过设计是程序模块化,做到模块内部的 高聚合 和模块之间的 低耦合 (如依赖注入就是低耦合的集中体现)。 这样做的好处是使得程序开发过程中,开发人员主需要专注于一点,
    02-05 安卓开发
点击排行