安卓学习笔记(一)自定义控件1
来源:互联网 发布:linux shell执行exe 编辑:程序博客网 时间:2024/05/18 23:56
在安卓开发中,自定义控件可以实现一些系统中没有的炫酷的效果从而给人眼前一亮的感觉。
1、根据安卓原生控件组合,再加上动画效果实现特殊的效果。
2、完全的自定义控件。
例1、布局文件
<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=".MainActivity" > <RelativeLayout android:layout_width="100dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/level1" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/icon_home" /> </RelativeLayout> <RelativeLayout android:layout_width="180dp" android:layout_height="90dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/level2" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:background="@drawable/icon_search" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="10dp" android:layout_marginRight="10dp" android:background="@drawable/icon_myyouku" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:background="@drawable/icon_menu" /> </RelativeLayout> <RelativeLayout android:layout_width="280dp" android:layout_height="142dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/level3" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="15dp" android:layout_marginLeft="12dp" android:background="@drawable/channel1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="15dp" android:layout_marginRight="12dp" android:background="@drawable/channel5" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="55dp" android:layout_marginLeft="35dp" android:background="@drawable/channel2" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="55dp" android:layout_marginRight="35dp" android:background="@drawable/channel6" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="85dp" android:layout_marginLeft="66dp" android:background="@drawable/channel3" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="85dp" android:layout_marginRight="66dp" android:background="@drawable/channel7" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:background="@drawable/channel4" /> </RelativeLayout></RelativeLayout>`
接下来就要通过动画来实现旋转效果
动画:
package com.example.text1;import android.view.View;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.RotateAnimation;import android.widget.RelativeLayout;public class AnimUtil { public static int animCount = 0; public static void closeMenu(RelativeLayout view, int startOffset) { for (int i = 0; i < view.getChildCount(); i++) { // 将RelativeLayout中的子view设置禁用 view.getChildAt(i).setEnabled(false); } /** * fromDegrees 起点 toDegrees 旋转度数 pivotXType X轴上基于什么旋转(在这里基于自身) * pivotXValue X轴上的基于的点坐标 pivotYType Y轴上基于什么旋转(在这里基于自身) pivotYValue * Y轴上的基于的点坐标 */ RotateAnimation animation = new RotateAnimation(0, 180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1); // 旋转时间 animation.setDuration(500); // 保持结束时的状态 animation.setFillAfter(true); // 延时执行 animation.setStartOffset(startOffset); animation.setAnimationListener(new MyAnimationListener()); view.startAnimation(animation); } public static void showMenu(RelativeLayout view, int startOffset) { for (int i = 0; i < view.getChildCount(); i++) { // 将RelativeLayout中的子view设置禁用 view.getChildAt(i).setEnabled(true); } RotateAnimation animation = new RotateAnimation(180, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1); // 旋转时间 animation.setDuration(500); // 保持结束时的状态 animation.setFillAfter(true); // 延时执行 animation.setStartOffset(startOffset); animation.setAnimationListener(new MyAnimationListener()); view.startAnimation(animation); } static class MyAnimationListener implements AnimationListener { @Override public void onAnimationStart(Animation animation) { animCount++; } @Override public void onAnimationEnd(Animation animation) { animCount--; } @Override public void onAnimationRepeat(Animation animation) { } }}
MainActivity
package com.example.text1;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.RelativeLayout;public class MainActivity extends Activity implements OnClickListener { private boolean isShowLeve2 = true;// 是否显示2级菜单 private boolean isShowLeve3 = true;// 是否显示3级菜单 private boolean isShowmenu = true;// 是否显示所有菜单 private ImageView home; private RelativeLayout level1, level2, level3; private ImageView menu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); home = (ImageView) findViewById(R.id.home); menu = (ImageView) findViewById(R.id.menu); level1 = (RelativeLayout) findViewById(R.id.level1); level2 = (RelativeLayout) findViewById(R.id.level2); level3 = (RelativeLayout) findViewById(R.id.level3); home.setOnClickListener(this); menu.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.home: if(AnimUtil.animCount != 0){//说明有动画在执行 return; } if (isShowLeve2) {// 将菜单隐藏 int startOffset = 0; if (isShowLeve3) { AnimUtil.closeMenu(level3, startOffset); startOffset += 200; isShowLeve3 = false; } AnimUtil.closeMenu(level2, startOffset); isShowLeve2 = false; } else { AnimUtil.showMenu(level2,0); isShowLeve2 = true; } break; case R.id.menu: if(AnimUtil.animCount != 0){//说明有动画在执行 return; } if (isShowLeve3) {// 将菜单隐藏 AnimUtil.closeMenu(level3, 0); isShowLeve3 = false; } else { AnimUtil.showMenu(level3,0); isShowLeve3 = true; } break; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_MENU){ if(isShowmenu){ int startOffset = 0; //关闭所有菜单 if(isShowLeve3){ AnimUtil.closeMenu(level3,startOffset); isShowLeve3 = false; startOffset +=200; } if(isShowLeve2){ AnimUtil.closeMenu(level2,startOffset); isShowLeve2 = false; startOffset +=200; } AnimUtil.closeMenu(level1, startOffset); isShowmenu = false; }else { //显示所有菜单 AnimUtil.showMenu(level1,0); AnimUtil.showMenu(level2,200); isShowLeve2 = true; AnimUtil.showMenu(level3,400); isShowLeve3 = true; isShowmenu = true; } return true; } return super.onKeyDown(keyCode, event); }}
0 0
- 安卓学习笔记(一)自定义控件1
- 安卓学习笔记(一)自定义控件2
- 自定义控件学习笔记(一)
- 安卓自定义控件(一)——组合控件
- 安卓学习笔记之自定义组合控件
- 安卓自定义控件(一)Canvas、Paint、Shader、Xfermode
- 安卓学习笔记(一)Android ImageButton、ImageView控件属性设置 图片显示问题
- 安卓学习笔记(一)
- 安卓学习笔记整理(一)
- 安卓开发学习笔记(一)
- 安卓学习笔记(一)
- 安卓学习笔记一
- 安卓学习笔记一
- 安卓学习之控件学习笔记
- 自定义控件学习笔记(2-1)
- 安卓开发笔记(一)------------自定义按钮
- 自定义控件学习(一)
- 自定义安卓控件
- MFC自制菜单(既绘制菜单的外观)
- Leetcode Best Time to Buy and Sell Stock II OJ 刷题 算法
- Linux学习笔记----rpm包and rpm命令管理
- 简单密码破译
- H--(LightOJ -- 1008
- 安卓学习笔记(一)自定义控件1
- C++ Primer : 第九章 : vector变长、string的其他操作以及容器适配器
- 10.1-5 双端队列deque
- Zookeeper监控的作用
- ActiveMQ(2) 安装
- 带哨兵的双向链表
- FZU 2087 统计树边
- 如何使用ubuntu14.04中的分区工具
- GDB十分钟教程