Android--自定义控件(组合系统控件)
通过对系统提供的控件进行组合,不用写新的类不用继承系统的控件也能实现自定义控件的效果。下面是一个简单的例子:
效果图:
主函数:
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
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 String tag = MainActivity.class.getSimpleName();
private ImageView iv_home,iv_menu;
private RelativeLayout level1,level2,level3;
private boolean isShowLevel2 = true;//是否显示2级菜单
private boolean isShowLevel3 = true;//是否显示3级菜单
private boolean isShowMenu = true;//是否显示整个菜单,包括1级,2级,3级的菜单
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initListener();
}
private void initView() {
setContentView(R.layout.activity_main);
iv_home = (ImageView) findViewById(R.id.iv_home);
iv_menu = (ImageView) findViewById(R.id.iv_menu);
level1 = (RelativeLayout) findViewById(R.id.level1);
level2 = (RelativeLayout) findViewById(R.id.level2);
level3 = (RelativeLayout) findViewById(R.id.level3);
}
private void initListener() {
iv_home.setOnClickListener(this);
iv_menu.setOnClickListener(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_MENU){
if(isShowMenu){
//需要关闭所有菜单
int startOffset = 0;
if(isShowLevel3){
AnimUtil.closeMenu(level3, startOffset);
isShowLevel3 = false;
startOffset += 200;
}
if(isShowLevel2){
AnimUtil.closeMenu(level2, startOffset);
isShowLevel2 = false;
startOffset += 200;
}
AnimUtil.closeMenu(level1, startOffset);
}else {
//需要显示所有菜单
AnimUtil.showMenu(level1,0);
AnimUtil.showMenu(level2,200);
isShowLevel2 = true;
AnimUtil.showMenu(level3,400);
isShowLevel3 = true;
}
isShowMenu = !isShowMenu;
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_home:
if(AnimUtil.animCount!=0){
//说明有动画在执行
return;
}
if(isShowLevel2){
//需要隐藏
int startOffset = 0;
if(isShowLevel3){
AnimUtil.closeMenu(level3,startOffset);
startOffset += 200;
isShowLevel3 = false;
}
AnimUtil.closeMenu(level2,startOffset);
}else{
//需要显示
// Log.e(tag, "执行显示操作");
AnimUtil.showMenu(level2,0);
}
isShowLevel2 = !isShowLevel2;
break;
case R.id.iv_menu:
if(AnimUtil.animCount!=0){
//说明有动画在执行
return;
}
if(isShowLevel3){
//隐藏3级菜单
AnimUtil.closeMenu(level3,0);
}else {
//显示3级菜单
AnimUtil.showMenu(level3,0);
}
isShowLevel3 = !isShowLevel3;
break;
default:
break;
}
}
}
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 rl,int startOffset){
for (int i = 0; i < rl.getChildCount(); i++) {
rl.getChildAt(i).setEnabled(false);
}
//pivotXValue: 0-1
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());
rl.startAnimation(animation);
}
public static void showMenu(RelativeLayout rl,int startOffset){
for (int i = 0; i < rl.getChildCount(); i++) {
rl.getChildAt(i).setEnabled(true);
}
RotateAnimation animation = new RotateAnimation(-180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 1);
animation.setDuration(500);
animation.setFillAfter(true);//动画结束后保持当时的状态
animation.setStartOffset(startOffset);
animation.setAnimationListener(new MyAnimationListener());
rl.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) {
}
}
}
布局文件:
<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:layout_width="100dp"
android:background="@drawable/level1"
android:layout_alignParentBottom="true"
android:id="@+id/level1"
android:layout_centerHorizontal="true"
android:layout_height="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/iv_home"
android:background="@drawable/icon_home"/>
</RelativeLayout>
<!-- 二级菜单 -->
<RelativeLayout android:layout_width="180dp"
android:layout_height="90dp"
android:id="@+id/level2"
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_marginTop="5dp"
android:id="@+id/iv_menu"
android:background="@drawable/icon_menu"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<RelativeLayout android:layout_width="280dp"
android:layout_height="142dp"
android:id="@+id/level3"
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_marginBottom="15dp"
android:layout_alignParentRight="true"
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="32dp"
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="32dp"
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="62dp"
android:background="@drawable/channel3"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="85dp"
android:background="@drawable/channel7"
android:layout_marginRight="62dp"
/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:background="@drawable/channel4"/>
</RelativeLayout>
</RelativeLayout>

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Android开发艺术探索——第八章:理解Window和WindowManager
理解Window和WindowManager Window表示的是一个窗口的概念,在日常生活中使用的并不是很多,但是某些特殊的需求还是需要的,比如悬浮窗之类的,他的具体实现是PhoneWindow,创建一个Window很简单,只需要WindowManager去实现,WindowManager是外界访问Window的入口,Window的具体实现是在WindowManagerService中,他们两个的交互是一个IPC的过程,Android中的所有视图都是通过Windowl来实现的,无论是Activity,Dialog还是Toast,他们的视图都是直接附加在Window上的,因此Window是View的直接管理者,在之前的事件分发中我们说到,View的事件是通过WIndow传递给DecorView,然后DecorView传递给我们的View,就连Activity的setContentView,都是由Window传递的。 一.Window和WindowManager 为了了解Window的工作机制,我们首先来看下如何通过WindowManager来创建一个Window Button btn ...
-
下一篇
Android设置ScrollView回到顶部的三种方式 (转)
一、ScrollView.scrollTo(0,0) 直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置; 二、ScrollView.fullScroll(View.FOCUS_UP) 类似于手动拖回顶部,有滚动过程; 三、ScrollView.smoothScrollTo(0, 0) 类似于手动拖回顶部,有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置。 转自:http://blog.csdn.net/xuanhg221/article/details/52931707 本文转载自SharkBin博客园博客,原文链接:http://www.cnblogs.com/SharkBin/p/7520345.html如需转载自行联系原作者
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Docker容器配置,解决镜像无法拉取问题
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- CentOS7设置SWAP分区,小内存服务器的救世主
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作