自定义控件

自定义控件有三种
组合已有的控件实现
-优酷菜单
1.在XNL布局里摆放好
2.给指定控件添加点击事件
3.执行动画(旋转动画)
4.菜单按钮的截获

-轮播图广告
-下拉选择框

继承已有的控件实现(扩展已有的功能)
-包含下拉刷新功能的ListView

完成自定义控件(继承View,ViewGroup)
-自定义开关
-侧滑面板

-优酷菜单
    1.在XNL布局里摆放好
    2.给指定控件添加点击事件
    3.执行动画(旋转动画)
    4.菜单按钮的截获

第一步:在XNL布局里摆放好
效果图:这里写图片描述

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


    <RelativeLayout
        android:id="@+id/rl_level1"
        android:layout_width="100dp"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@mipmap/level1">

        <ImageButton
            android:id="@+id/ib_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:background="@null"
            android:src="@mipmap/icon_home"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_level2"

        android:layout_width="190dp"
        android:layout_height="85dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@mipmap/level2">

        <ImageButton
            android:id="@+id/ib_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="78dp"
            android:src="@mipmap/icon_menu"
            />
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="50dp"
            android:layout_marginLeft="10dp"
            android:src="@mipmap/icon_search"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="50dp"
            android:layout_marginLeft="150dp"
            android:src="@mipmap/icon_myyouku"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_level3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@mipmap/level3">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:src="@mipmap/channel1"
            android:layout_marginTop="90dp"
            android:layout_marginLeft="18dp"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="50dp"
            android:layout_marginLeft="40dp"
            android:src="@mipmap/channel2"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="80dp"
            android:src="@mipmap/channel3"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="130dp"
            android:src="@mipmap/channel4"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="180dp"
            android:src="@mipmap/channel5"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="50dp"
            android:layout_marginLeft="220dp"
            android:src="@mipmap/channel6"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_marginTop="90dp"
            android:layout_marginLeft="250dp"
            android:src="@mipmap/channel7"/>
    </RelativeLayout>


</RelativeLayout>

第二步:给指定控件添加点击事件

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements View.OnClickListener{

    private RelativeLayout rl_level1;
    private RelativeLayout rl_level2;
    private RelativeLayout rl_level3;

    boolean isLevel3Display = true;
    boolean isLevel2Display = true;
    boolean isLevel1Display = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控件

        initView();
    }

    private void initView() {
        findViewById(R.id.ib_home).setOnClickListener(this);
        findViewById(R.id.ib_menu).setOnClickListener(this);

        rl_level1 = (RelativeLayout) findViewById(R.id.rl_level1);
        rl_level2 = (RelativeLayout) findViewById(R.id.rl_level2);
        rl_level3 = (RelativeLayout) findViewById(R.id.rl_level3);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_home:
                if(isLevel2Display) {
                    //如果当前三级菜单已经显示,先转出去
                    if(isLevel3Display) {
                        //延时
                        AnimationUtils.rotateOutAnim(rl_level3,200);
                        isLevel3Display = false;
                    }

                    //如果当前二级菜单已经显示,转出去
                    AnimationUtils.rotateOutAnim(rl_level2,0);
                    isLevel2Display = false;
                } else  {
                    //如果当前二级菜单没有显示,转出来
                    AnimationUtils.rotateInAnim(rl_level2);
                    isLevel2Display = true;
                }
                break;
            case R.id.ib_menu:
                if(isLevel3Display) {
                    //如果当前三级菜单已经显示,转出去
                    AnimationUtils.rotateOutAnim(rl_level3,0);
                    isLevel3Display = false;
                } else  {
                    //如果当前三级菜单没有显示,转出来
                    AnimationUtils.rotateInAnim(rl_level3);
                    isLevel3Display = true;
                }
                break;
            default:
                break;
        }
    }
}

动画效果AnimationUtils.java:

import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;

/**
 * Created by huang on 2016/8/22.
 */
public class AnimationUtils {

    //旋转出去的动画
    //要执行动画需要传入布局
    public static void rotateOutAnim(RelativeLayout layout, long delay) {
        RotateAnimation ra = new RotateAnimation(
                0f, -180f, //开始, 结束的角度
                Animation.RELATIVE_TO_SELF, 0.5f, //相对的坐标点(指定旋转中心X值)
                Animation.RELATIVE_TO_SELF, 1.0f //相对的坐标点(指定选择中心Y值)
        );

        ra.setDuration(500);    //时间
        //动画终止时停留在最后一帧,不会回到没有执行之前的状态
        ra.setFillAfter(true);

        //延时
        ra.setStartOffset(delay); //设置动画开始延时

        //执行
        layout.startAnimation(ra);
    }

    public static void rotateInAnim(RelativeLayout layout) {
        RotateAnimation ra = new RotateAnimation(
                -180f, 0f, //开始, 结束的角度, 逆时针
                Animation.RELATIVE_TO_SELF, 0.5f, //相对的坐标点(指定旋转中心X值)
                Animation.RELATIVE_TO_SELF, 1.0f //相对的坐标点(指定选择中心Y值)
        );

        ra.setDuration(500);    //时间
        //动画终止时停留在最后一帧,不会回到没有执行之前的状态
        ra.setFillAfter(true);


        //执行
        layout.startAnimation(ra);

    }
}
Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值