zoukankan      html  css  js  c++  java
  • 自定义视图

    1. 自定义一个带有颜色属性的view视图

    /**
     * Created by ZhouXiaoHai on 2016/9/20.
     *
     * 自定义视图
     * ①定义MyRect视图
     */
    public class MyRect extends View {  // 继承视图
    
        public MyRect(Context context) {
            super(context);
        }
    
    
        public MyRect(Context context, AttributeSet attrs) {
            super(context, attrs);
            // 通过TypedArray来获取我们定义styleable中的属性值
            // R.styleable.MyRect  是我们创建的attr.xml中的下面代码中的MyRect
            /*
                <declare-styleable name="MyRect" >
                    <attr name="rect_color" format="color"></attr>
                </declare-styleable>
            * */
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyRect);
            // 通过属性来定义颜色 MyRect_rect_color 是指的  declare-styleable标签name和attr的name组合
            int color = ta.getColor(R.styleable.MyRect_rect_color, 0xffff0000);
    
            // 改变这个视图的颜色
            this.setBackgroundColor(color);
    
            // 回收资源
            ta.recycle();
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <!-- 在values中创建 attrs.xml
         这里的MyRect可以任意设置,不需要和MyRect类名字一样
     -->
    <resources>
        <declare-styleable name="MyRect" >
            <!-- 定义属性 format有很选择,要根据实际情况-->
            <attr name="rect_color" format="color"></attr>
        </declare-styleable>
    </resources>

    使用我们定义的MyRect视图

    <?xml version="1.0" encoding="utf-8"?>
    
    <!-- xmlns:chengzhier 可以自己设置了,才会有下面的chengzhier:rect_color属性
     -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:chengzhier="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
        
        <!-- chengzhier:rect_color就是我们自己定义的 -->
        <com.aaa.chengzhier.MyRect
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="0.15"
            chengzhier:rect_color="#ff0000ff"
            />
    </LinearLayout>

    效果:

    2. 自定义一个点击换色的按钮控件

    ① 在drawable目录下面添加两张按钮的北京2图片, ba1.jpg,ba2.jpg

    ② 在drawable目录下面创建button_skin.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- 当按钮未点击时用ba1.jpg -->
        <item android:state_pressed="false" android:drawable="@drawable/ba1"></item>
        <!-- 当按钮点击时用ba2.jpg -->
        <item android:state_pressed="true" android:drawable="@drawable/ba2"></item>
    
    
        <!-- 按钮还有很多的状态 可以自己看看 -->
    </selector>

    ③定义的按钮,使用背景

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
    
        <!-- 使用按钮的时候,背景直接用定义的 button_skin -->
        <Button
            android:background="@drawable/button_skin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:id="@+id/button"
            />
    </LinearLayout>

    ④ 效果

    没有点击的按钮

    点击中的按钮

    3. 制作一个旋转的正方形视图

    RotatingRect.java

    public class RotatingRect extends View {  //继承视图
        public RotatingRect(Context context) {
            super(context);
            this.initProperties();
        }
    
        public RotatingRect(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.initProperties();
        }
    
        public RotatingRect(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            this.initProperties();
        }
    
        private void initProperties() {
            this.p = new Paint();   // new一个画笔
            p.setColor(Color.RED);  // 设置为红色
        }
    
    
        // 重写draw方法,View会自动的调用这个方法
        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
    
            canvas.translate(200, 200);   // 距离左边200 上面200
            canvas.rotate(this.degrees, 50, 50);      // 转动角度 和  在哪里转动
            canvas.drawRect(0, 0, 100, 100, this.p);  // 绘制一个正方形
    
            this.degrees ++;
            // 使这个 view 无效, 应用程序会在次查看这个view是否绘制,如果没有就在绘制一次(执行draw方法)
            this.invalidate();
        }
    
        private Paint p;
        private int degrees = 0;
    }

    在xml文件视图中使用

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
    
        <com.aaa.chengzhier.RotatingRect
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    效果:

    红色的方形一直在转动.

  • 相关阅读:
    横冲直撞vue(第六篇):vue之过滤器、es6中填充字符串、es6新增的padStart()方法和padEnd()方法、vue自定义键盘修饰符、vue自定义全局指令
    leetcode的奇妙冒险(python3)系列:leetcode 283. Move Zeroes
    横冲直撞vue(第五篇):事件修饰符、指令系统综合案例
    横冲直撞vue(第四篇):v-model、指令系统总结、指令系统示例轮播图实现、指令系统示例跑马灯效果实现、在vue中使用样式的方式
    横冲直撞vue(第三篇):vue中template的三种写法、v-bind、v-on、更新元素的指令v-text与v-html、条件渲染指令v-if 与v-show、v-for
    横冲直撞vue(第二篇):什么是vue?框架和库的区别、vue的优点、vue的使用、使用vue实例化对象
    横冲直撞vue(第一篇):常用的ES6语法
    nodejs(第五篇):npm常用命令、包说明文件package.json、packjson-lock.json文件、使用nodemon插件、nrm的安装与使用
    最详细的个人博客教程搭建教程,最快5分钟快速搭建简约风格博客
    面试问了解Linux内存管理吗?10张图给你安排的明明白白!
  • 原文地址:https://www.cnblogs.com/shaoshao/p/5891007.html
Copyright © 2011-2022 走看看