zoukankan      html  css  js  c++  java
  • 【Android】动态弹出组件(Layout)

     1 package com.example.translateanimationmenu;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.view.View.OnClickListener;
     7 import android.view.animation.Animation;
     8 import android.view.animation.ScaleAnimation;
     9 import android.view.animation.TranslateAnimation;
    10 import android.widget.Button;
    11 import android.widget.LinearLayout;
    12 import android.widget.RelativeLayout;
    13 import android.widget.Toast;
    14 
    15 public class TranslateAnimationMenu extends Activity {
    16     /** Called when the activity is first created. */
    17     //TranslateAnimation showAction, hideAction;
    18     Animation showAction, hideAction;
    19     RelativeLayout menu;
    20     Button button;
    21     Button menuButton;
    22     boolean menuShowed;
    23     
    24     @Override
    25     public void onCreate(Bundle savedInstanceState) {
    26         super.onCreate(savedInstanceState);
    27         setContentView(R.layout.activity_translate_animation_menu);
    28         menu = (RelativeLayout) findViewById(R.id.menu);
    29         button = (Button) findViewById(R.id.button);
    30         menuButton = (Button) findViewById(R.id.menuButton);
    31         menuButton.setOnClickListener(new OnClickListener() {
    32             @Override
    33             public void onClick(View arg0) {
    34                 // TODO Auto-generated method stub
    35                 Toast.makeText(TranslateAnimationMenu.this, "Success", Toast.LENGTH_SHORT).show();
    36             }
    37         });
    38     
    39 //        // 这里是TranslateAnimation动画
    40 //        showAction = new TranslateAnimation(
    41 //                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
    42 //                Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
    43 //         这里是ScaleAnimation动画
    44         showAction = new ScaleAnimation(
    45                 1.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
    46         showAction.setDuration(500);
    47         // 这里是TranslateAnimation动画
    48         hideAction = new TranslateAnimation(
    49                 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
    50                 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
    51         // 这里是ScaleAnimation动画
    52         //hideAction = new ScaleAnimation(
    53         // 1.0f, 1.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
    54         hideAction.setDuration(500);
    55         menuShowed = false;
    56         menu.setVisibility(View.GONE);
    57     
    58         button.setOnClickListener(new OnClickListener() {
    59             @Override
    60             public void onClick(View v) {
    61                 // TODO Auto-generated method stub
    62                 if (menuShowed) {
    63                     menuShowed = false;
    64                     menu.startAnimation(hideAction);
    65                     menu.setVisibility(View.GONE);
    66                 }
    67                 else {
    68                     menuShowed = true;
    69                     menu.startAnimation(showAction);
    70                     menu.setVisibility(View.VISIBLE);
    71                 }
    72             }
    73         });
    74     }
    75 }

    下面是XML:

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:orientation="vertical" 
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent">
     5 
     6 <TextView 
     7     android:layout_width="fill_parent"
     8     android:layout_height="wrap_content" 
     9     android:text="test" />
    10 
    11 <RelativeLayout 
    12     android:id="@+id/menu"
    13     android:layout_height="100px" 
    14     android:layout_width="fill_parent"
    15     android:layout_alignParentTop="true" 
    16     android:background="#22ff0000">
    17     
    18 <Button
    19     android:id="@+id/menuButton"
    20     android:layout_width="wrap_content"
    21     android:layout_height="wrap_content" 
    22     android:text="Click Me!"
    23     android:background="#2200ff00"
    24     android:layout_centerHorizontal="true"
    25     android:gravity="center" />
    26 </RelativeLayout>
    27 
    28 <Button 
    29     android:id="@+id/button" 
    30     android:layout_width="fill_parent"
    31     android:layout_height="wrap_content" 
    32     android:layout_alignParentBottom="true"
    33     android:text="Click to show/hide menu" />
    34 </RelativeLayout>

    上面就可以实现组件的动态出现和动态隐藏

  • 相关阅读:
    可重入函数
    epoll源码深度剖析
    数据结构学习之基本概念
    机器学习算法原理与编程实践之朴素贝叶斯分类
    机器学习算法与编程实践之中文文本分类
    ElasticSearch常用结构化搜索
    linux几种常见的文件内容查找和替换命令
    List遍历时删除遇到的问题
    初学ElasticSeacher
    从jar中读取资源文件的问题
  • 原文地址:https://www.cnblogs.com/lqminn/p/2804631.html
Copyright © 2011-2022 走看看