自定义View的第一个学习案例

  ViewGroup是自动以View中比较常用也比较简单的一种方式,通过组合现有的UI控件,绘制出一个全新的View

效果如下:

主类实现如下:

package com.demo.youku;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
/**
* False: hide
* 是否显示圆环 默认显示
* true:显示
* false:隐藏
*/
private Boolean showLevel1 = true;
private Boolean showLevel2 = true;
private Boolean showLevel3 = true;
private ImageView iconHome;
private ImageView iconMenu;
private RelativeLayout level1;//第一层
private RelativeLayout level2;//第二层
private RelativeLayout level3;//第三层
private Toolbar toolbar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("优酷菜单");
//设置导航图标要在setSupportActionBar方法之后
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.mipmap.icon_menu); iconHome = (ImageView) findViewById(R.id.home);
iconMenu = (ImageView) findViewById(R.id.icon_menu);
level1 = (RelativeLayout) findViewById(R.id.level1);
level2 = (RelativeLayout) findViewById(R.id.level2);
level3 = (RelativeLayout) findViewById(R.id.level3); MyOnclickListener myOnclickListener = new MyOnclickListener(); iconHome.setOnClickListener(myOnclickListener);
iconMenu.setOnClickListener(myOnclickListener);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//如果一级二级三级菜单显示 则关闭
if (showLevel1 || showLevel2 || showLevel3) {
showLevel1 = false;
showLevel2 = false;
if (showLevel3) {
showLevel3 = false;
Tools.hideView(level3);
Tools.hideView(level2, 400);
Tools.hideView(level1, 800);
} else {
Tools.hideView(level2);
Tools.hideView(level1, 300);
}
} else {
//如果都未显示 则显示一级二级菜单
showLevel1 = true;
showLevel2 = true; Tools.showView(level1);
Tools.showView(level2);
}
}
}); } class MyOnclickListener implements View.OnClickListener { @Override
public void onClick(View view) {
//
switch (view.getId()) {
case R.id.home:
if (showLevel2) {
showLevel2 = false;
Tools.hideView(level2);
if (showLevel3) {
showLevel3 = false;
Tools.hideView(level3, 400);
}
} else {
showLevel2 = true;
Tools.showView(level2);
}
break;
case R.id.icon_menu:
if (showLevel3) {
showLevel3 = false;
Tools.hideView(level3); } else {
showLevel3 = true;
Tools.showView(level3);
}
break; }
}
} }

Tools类主要用于控制View的显示和隐藏动画,提供了属性动画,不补间动画两种实现方式

package com.demo.youku;

import android.animation.ObjectAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout; public class Tools { /**
* 顺时针旋转0-180度隐藏view
*
* @param view
*/
public static void hideView(ViewGroup view) {
hideView(view, 0);
} /**
* 顺时针旋转180-360度显示view
*
* @param view
*/
public static void showView(ViewGroup view) {
/* RotateAnimation ra = new RotateAnimation(180, 360, view.getWidth() / 2, view.getHeight());
ra.setDuration(500);
ra.setFillAfter(true);
view.startAnimation(ra); //启动ViewGroup中所有子元素的点击事件
for (int i = 0; i < view.getChildCount(); i++) {
View childView = view.getChildAt(i);
childView.setEnabled(true);
}*/ ObjectAnimator animator = ObjectAnimator.ofFloat(view, "Rotation", 180, 360);
animator.setDuration(500);
animator.start(); view.setPivotX(view.getWidth() / 2);
view.setPivotX(view.getHeight());
} /**
* 延迟旋转
*
* @param view 需要旋转的view
* @param setTimeOut 动画延迟时间
*/
public static void hideView(ViewGroup view, int setTimeOut) {
/*RotateAnimation ra = new RotateAnimation(0, 180, view.getWidth() / 2, view.getHeight());
ra.setDuration(500);//动画时间
ra.setFillAfter(true);//是否保留动画结束状态
ra.setStartOffset(setTimeOut);//设置延迟时间
view.startAnimation(ra); //禁用ViewGroup中错有元素的点击事件
for (int i = 0; i < view.getChildCount(); i++) {
View childView = view.getChildAt(i);
childView.setEnabled(false);
}*/
<?xml version="1.0" encoding="utf-8"?>
<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="com.demo.youku.MainActivity"> <android.support.v7.widget.Toolbar
android:background="?attr/colorPrimary"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"> </android.support.v7.widget.Toolbar> <RelativeLayout
android:id="@+id/level3"
android:layout_width="280dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@mipmap/level3"> <ImageView
android:id="@+id/chanel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:background="@mipmap/channel1" /> <ImageView
android:id="@+id/chanel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/chanel1"
android:layout_marginBottom="8dp"
android:layout_marginLeft="30dp"
android:background="@mipmap/channel2" /> <ImageView
android:id="@+id/chanel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/chanel2"
android:layout_marginBottom="8dp"
android:layout_marginLeft="60dp"
android:background="@mipmap/channel3" /> <ImageView
android:id="@+id/chanel4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:background="@mipmap/channel4" /> <ImageView
android:id="@+id/chanel5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:background="@mipmap/channel5" /> <ImageView
android:id="@+id/chanel6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/chanel5"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="30dp"
android:background="@mipmap/channel6" /> <ImageView
android:id="@+id/chanel7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/chanel6"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="60dp"
android:background="@mipmap/channel7" /> </RelativeLayout> <RelativeLayout
android:id="@+id/level2"
android:layout_width="180dp"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@mipmap/level2"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:background="@mipmap/icon_search" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:background="@mipmap/icon_myyouku" /> <ImageView
android:id="@+id/icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:background="@mipmap/icon_menu" />
</RelativeLayout> <RelativeLayout
android:id="@+id/level1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@mipmap/level1"> <ImageView
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/icon_home" /> </RelativeLayout> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="ViewGrou之优酷菜单"
android:textSize="24sp" /> </RelativeLayout>

        animator.setStartDelay(setTimeOut);
animator.start(); view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight());
}
}

页面布局如下,布局中使用Toolbar代替ActionBar:主要需要更换默认主题:Theme.AppCompat.Light.NoActionBar

自定义View(一)-ViewGroup实现优酷菜单的更多相关文章

  1. android自定义控件之模仿优酷菜单

    去年的优酷HD版有过这样一种菜单,如下图: 应用打开之后,先是三个弧形的三级菜单,点击实体键menu之后,这三个菜单依次旋转退出,再点击实体键menu之后,一级菜单会旋转进入,点击一级菜单,二级菜单旋 ...

  2. 自定义View和ViewGroup

    为了扫除学习中的盲点,尽可能多的覆盖Android知识的边边角角,决定对自定义View做一个稍微全面一点的使用方法总结,在内容上面并没有什么独特的地方,其他大神们的博客上面基本上都有讲这方面的内容,如 ...

  3. android 自定义 view 和 ViewGroup

    ---恢复内容开始--- ViewGroup的职能为:给childView计算出建议的宽和高和测量模式 :决定childView的位置:为什么只是建议的宽和高,而不是直接确定呢,别忘了childVie ...

  4. 【Android 应用开发】自定义View 和 ViewGroup

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...

  5. 自定义View和ViewGroup(有这一篇就够了)

    为了扫除学习中的盲点,尽可能多的覆盖Android知识的边边角角,决定对自定义View做一个稍微全面一点的使用方法总结,在内容上面并没有什么独特的地方,其他大神们的博客上面基本上都有讲这方面的内容,如 ...

  6. 自定义View 和 ViewGroup

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...

  7. Android自定义View(RollWeekView-炫酷的星期日期选择控件)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53420889 本文出自:[openXu的博客] 目录: 1分析 2定义控件布局 3定义Cus ...

  8. Android自定义组件系列【1】——自定义View及ViewGroup

    View类是ViewGroup的父类,ViewGroup具有View的所有特性,ViewGroup主要用来充当View的容器,将其中的View作为自己孩子,并对其进行管理,当然孩子也可以是ViewGr ...

  9. 自定义View总结2

    自定义控件: 1.组合控件:将系统原生控件组合起来,加上动画效果,形成一种特殊的UI效果 2.纯粹自定义控件:继承自系统的View,自己去实现view效果 优酷菜单: 1.系统原生的旋转和位置动画并没 ...

随机推荐

  1. 【转】Google推荐的命名规则——Android图片资源

    http://blog.csdn.net/yy1300326388/article/details/45443477 1.译 资产类型 前缀 例子 图标 ic_ ic_star.png 启动图标 ic ...

  2. udt nat traverse

    https://github.com/bakwc/udt-nat-traverse Example of nat traversal using udt library. UDT is a udp b ...

  3. windows7下实现局域网内文件共享

    1.右击桌面网络----属性----更改高级共享设置 (注释:查看当前网络 比如:家庭网络.公共网络 等!) "我这里为公共网络" 2.选择 公共网络---选择以下选项:启动网络发 ...

  4. Orchard官方文档

    开始使用 安装Orchard 通过Orchard zip文件安装 使用WebMatrix开发Orchard Dashboard总览 创建你的第一个Orchard站点 导航和菜单 添加博客 新增管理媒体 ...

  5. wp———图片切换效果

    此篇文章主要是记录一下使用XamlReader加载动画时遇到的一些问题. 首先呢,把源码附上 <phone:PhoneApplicationPage x:Class="PicChang ...

  6. 由实现JavaScript中的Map想到的

    项目中要用到JavaScript中的Map数据类型,它不像JDK那样有自带的,怎么办?搜了找到一个不错的(http://darkmasky.iteye.com/blog/454749).用这个可以满足 ...

  7. hdu 5265 pog loves szh II STL

    pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  8. 几种更新(Update语句)查询的方法

    正 文: 数据库更新就一种方法Update,其标准格式:Update 表名 set 字段=值 where 条件只是依据数据的来源不同,还是有所差别的:  1.从外部输入这样的比較简单例:update ...

  9. MySQL数据库最大连接数

    MYSQL数据库安装完毕后,默认最大连接数是100. 命令: show processlist; 假设是root帐号,你能看到全部用户的当前连接.假设是其他普通帐号,仅仅能看到自己占用的连接. sho ...

  10. 内存管理和@property的属性

    内存管理和@property的属性 目录 对内存管理的理解 Objective C内存管理方式 内存的管理 对象的所有权和内存管理原则 合理解决内存管理带来的问题 自动释放池 @property的属性 ...