Android 基于TranslateAnimation 的动画动态菜单(非系统menu菜单)

先请注意,这里的菜单并不是按机器上的 MENU出现在那种菜单,而是基于Android SDK 提供的 android.view.animation.TranslateAnimation(extends android.view.animation.Animation)类实例后附加到一个 Layout 上使之产生的有动画出现和隐藏效果的菜单。

原理:Layout(菜单)从屏幕内(挨着屏幕边沿,其实并非一定,视需要的初态和末态而定)动态的移动到屏幕外(在外面可以挨着边沿,也可以离远点,这个无所谓了),这样就可以达到动态菜单的效果了。但是由于Animation的一些奇怪特性(setFill**() 函数的作用效果,这个在我使用的某几个Animation 当中出现了没有想明白的效果),就暂不理会这个东西了,所以使得我们还需要用上 XML属性android:visibility。当Layout(菜单)显示的时候,设置android:visibility="visible",当Layout(菜单)隐藏的时候,设置android:visibility="gone",这里 android:visibility 可以有3个值,"visible"为可见,"invisible"为不可见但占空间,"gone"为不可见且不占空间(所谓的占不占空间,这个可以自己写个 XML来试试就明白了)。

Class TranslateAnimation 的使用:Animation有两种定义方法,一种是用Java code,一种是用XML,这里只介绍用 code来定义(因为用XML来定义的那种我没用过。。嘿嘿。。)。多的不说,看代码。

这里是TranslateAnimationMenu.java(我在里面还另加入了 ScaleAnimation产生的动画,各位朋友可以照着 SDK以及程序效果来理解):

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

public class TranslateAnimationMenu extends Activity {
    /** Called when the activity is first created. */
    // TranslateAnimation showAction, hideAction;
    Animation showAction, hideAction;
    LinearLayout menu;
    Button button;
    boolean menuShowed;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 

        menu = (LinearLayout) findViewById(R.id.menu);
        button = (Button) findViewById(R.id.button);
        // 这里是 TranslateAnimation动画
        showAction = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,
        0.0f,  Animation.RELATIVE_TO_SELF, -1.0f,
        Animation.RELATIVE_TO_SELF, 0.0f);
        //这里是ScaleAnimation 动画
        //showAction = new ScaleAnimation(
        //     1.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
        //     Animation.RELATIVE_TO_SELF, 0.0f);
        showAction.setDuration(500); 

        // 这里是TranslateAnimation 动画
        hideAction = new TranslateAnimation(
           Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, -1.0f);
        // 这里是ScaleAnimation 动画
        //hideAction = new ScaleAnimation(
        //                   1.0f, 1.0f, 1.0f, 0.0f,
        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
        0.0f);
         hideAction.setDuration(500);
        menuShowed = false;
        menu.setVisibility(View.GONE);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub
               if (menuShowed) {
                   menuShowed = false;
                   menu.startAnimation(hideAction);
                   menu.setVisibility(View.GONE);
            }
            else {
                menuShowed = true;
                menu.startAnimation(showAction);
                menu.setVisibility(View.VISIBLE);
           }
         } 

        });
    }
}

这里是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="fill_parent"
        android:layout_height="100px"
        android:layout_alignParentTop="true"
        android:background="#ffffff" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="I am a menu" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Click to show/hide menu" />

</RelativeLayout>
时间: 2024-08-01 08:00:16

Android 基于TranslateAnimation 的动画动态菜单(非系统menu菜单)的相关文章

Android 相册图片选取+自定义裁剪方式(非系统裁剪)

不多说,直接上代码(裁剪的代码摘自网络.)(项目可运行) 主要是系统自身的剪切方式在有些机型上会程序崩溃的问题. 1 package com.jichun.activity; 2 3 import java.io.FileNotFoundException; 4 5 import com.jichun.view.CropCanvas; 6 7 import android.app.Activity; 8 import android.content.ContentResolver; 9 impo

Android—基于GifView显示gif动态图片

android中显示gif动态图片用到了开源框架GifView 1.拷GifView.jar到自己的项目中. 2.将自己的gif图片拷贝到drawable文件夹 3.在xml文件中设置基本属性: <com.ant.liao.GifView android:id="@+id/gifView" android:layout_centerInParent="true" android:layout_width="wrap_content" and

动态改变actionbar的menu菜单MenuItem的显示

onCreateOptionsMenu(Menu menu)方法中我们能够通过getMenuInflater().inflate(R.menu.list_option, menu);来构建一个菜单,并通过 1 menu.findItem(R.id.menu) 获得具体的一个菜单项.但这个onCreateOptionsMenu在activity的整个周期中只被调用一次,之后都不会变化,于是在android的较新版本里有了onPrepareOptionsMenu方法. onCreateOptions

基于Android平台的i-jetty网站智能农业监控系统

基于android平台i-jetty网站的智能农业监控系统 摘要:传统的监控系统,一般是基于PC的有线通信传输,其有很多不足之处,如功耗较高.布线成本高.难度大,适应性差,可扩展性不强,增加新的通信线路需要再次布线施工,而且维护起来也比较麻烦,一旦线路出问题,需要繁琐的检查.而嵌入式Web监控系统是基于物联网技术,其无线通信技术具有成本低廉.适应性强.扩展性强.信息安全.使用维护简单等优点. 智能农业中,种植大棚是通过大棚内安装温湿度以及光照传感器,来对农作物的环境参数进行实时采集,由Web监控

创建Material Design风格的Android应用--使用自定义动画

动画在Material Design设计中给用户反馈放用户点击时,并且在程序用户界面中提供连贯的视觉.Material主题为按钮(Button)和activity的转换提供了一些默认的动画,在android5.0(api 21)和更高的版本,你可以自定义这些动画和创建一个新动画: Touch feedback(触摸反馈) Circular Reveal(循环揭露效果) Activity transitions(Activity转换效果) Curved motion(曲线运动) View stat

基于定时器的动画和性能调优

摘自:http://www.cocoachina.com/ios/20150106/10839.html 基于定时器的动画 我可以指导你,但是你必须按照我说的做. -- 骇客帝国 在第10章“缓冲”中,我们研究了CAMediaTimingFunction,它是一个通过控制动画缓冲来模拟物理效果例如加速或者减速来增强现实感的东西,那么如果想更加真实地模拟 物理交互或者实时根据用户输入修改动画改怎么办呢?在这一章中,我们将继续探索一种能够允许我们精确地控制一帧一帧展示的基于定时器的动画. 定时帧 动

Android组件——使用DrawerLayout仿网易新闻v4.4侧滑菜单

转载请注明出处:http://blog.csdn.net/allen315410/article/details/42914501 概述 今天这篇博客将记录一些关于DrawerLayout的基本用法,我想关于DrawerLayout的用法也许有不少不够了解,这也是比较正常的事情,因为DrawerLayout作为Android组件是Google后来在android中添加的,在android.support.v4包下.那么,DrawerLayout是一个怎么的组件呢?我们知道,当我们使用Androi

iOS Core Animation Advanced Techniques(六): 基于定时器的动画和性能调优

基于定时器的动画 我可以指导你,但是你必须按照我说的做. -- 骇客帝国 在第10章“缓冲”中,我们研究了CAMediaTimingFunction,它是一个通过控制动画缓冲来模拟物理效果例如加速或者减速来增强现实感的东西,那么如果想更加真实地模拟 物理交互或者实时根据用户输入修改动画改怎么办呢?在这一章中,我们将继续探索一种能够允许我们精确地控制一帧一帧展示的基于定时器的动画. 定时帧 动画看起来是用来显示一段连续的运动过程,但实际上当在固定位置上展示像素的时候并不能做到这一点.一般来说这种显

Android 学习笔记之AndBase框架学习(七) SlidingMenu滑动菜单的实现

PS:努力的往前飞..再累也无所谓.. 学习内容: 1.使用SlidingMenu实现滑动菜单..   SlidingMenu滑动菜单..滑动菜单在绝大多数app中也是存在的..非常的实用..Github有位牛人将这个东西书写成了一个简单的框架..我们只需要通过引用相关的资源就能够完成一个滑动菜单的实现..有了这一层的基础..那么滑动菜单就很容易实现了..就拿我们最为熟悉的QQ来说吧..当我们进行滑动操作的时候..会有一个新的View去覆盖原本的View..我们可以通过触发新的视图上的控件来执行