android学习---自定义View
来源:互联网 发布:dj java decompiler 编辑:程序博客网 时间:2024/05/24 03:13
自定义View的定义 : 通过直接继承View或者重写View子类实现逻辑的View
如何使用自定义View
自绘控件:直接继承View类的方式
自绘控件的步骤:1.继承View
2.实现Listener接口(不是必须的)3.重写构造方法(两个参数)
public class CountNumView extends View { public CountNumView(Context context, AttributeSet attrs) { super(context, attrs); }
4.重写onDraw方法
5.Paint画笔
Paint常用方法解释Paint常用方法解释setAntiAlias()设置画笔的锯齿效果setStyle()设置画笔的风格(空心或实心)setColor() 设置画笔的颜色 setStrokeWidth()设置空心边框的宽度setARGB()设置画笔的A、R、G、B值 getColor()获取画笔的颜色setAlpha()设置画笔的Alpha值(透明度) setTextSize()设置字体的尺寸
6.Canvas绘制
Canvas的常用方法解释Canvas的常用方法解释drawLine()绘制直线 drawRect() 绘制矩形drawCircle()绘制圆形drawText()绘制字符drawBitmap() 绘制图形
7.invalidate方法刷新 ( 1.主要作用是请求View树进行重绘; 2.调用invalidate方法后,将会再次 执行onDraw方法)
8.在布局文件中引用(包名.类名)
<com.view.CountNumView 包名.类名 android:layout_width="wrap_content" android:layout_height="wrap_content" />
重写控件:间接继承View子类的方式
步骤:1.继承系统提供的View
2.重写对应的构造方法(两个参数)
3.实现逻辑并定义事件监听
4.在布局文件中引用
重写ListView实现下拉刷新
步骤:1.在layout下编写下拉刷新头部布局
<TextView android:layout_width="match_parent" android:layout_height="60dp" android:text="正在刷新" android:gravity="center" />
2.新建类继承ListView
3.实现构造方法
4.将头部布局通过addHeaderView添加到ListView头部
5.将头部布局的Padding值设为负数,隐藏头部
6.重写onTouchEvent事件
7.在MotionEventACTION_MOVE中判断是否下拉
8.不断修改头部布局Padding为正数,显示到屏幕上
public class RefreshListView extends ListView {private int headerViewHeight;private int downY;private int moveY;private View v;public RefreshListView(Context context, AttributeSet attrs) {super(context, attrs);v = inflate(context, R.layout.listview_header, null);v.measure(0, 0);// 获取顶部视图的高度headerViewHeight = v.getMeasuredHeight();v.setPadding(0, -headerViewHeight, 0, 0);// 添加顶部视图addHeaderView(v);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:// 获取手指按下时Y轴坐标downY = (int) ev.getY();break;case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动的距离moveY = (int) ((ev.getY() - downY) * 0.3);// 动态设置paddingv.setPadding(0, moveY - headerViewHeight, 0, 0);v.invalidate();invalidate();break;case MotionEvent.ACTION_UP:// 手指抬起时重新隐藏HeaderViewv.setPadding(0, -headerViewHeight, 0, 0);break;default:break;}return super.onTouchEvent(ev);}}然后再在布局文件中运用包名和类名引用
组合控件:间接继承View子类,将需要用到的原生控件组合到一起
组合控件的使用,通过代码来回忆1.先定义你要把它当做组合控件的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textviewspicejiage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥41.90" android:textColor="#bb5555" android:textSize="20sp" /> <Button android:id="@+id/button_spiceitemgoumai" android:layout_width="80dp" android:layout_height="30dp" android:layout_marginLeft="80dp" android:background="@drawable/rgister_btn_bk" android:text="购买" android:textColor="#ffffff" /></LinearLayout>
2.然后再建一个Java文件,构造布局选择器
public class FoodListviewzuhe extends LinearLayout{public FoodListviewzuhe(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater inflate = LayoutInflater.from(context);View view=inflate.inflate(R.layout.fooditemzuhe, null);this.addView(view);}}
3.然后在布局文件中引用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.view.FoodListviewzuhe android:layout_width="match_parent" android:layout_height="wrap_content" > </com.view.FoodListviewzuhe></LinearLayout>
- Android学习 自定义View
- android学习---自定义View
- Android 自定义View学习
- Apidemo学习 android 自定义View
- 【Android学习笔记】自定义View
- 【Android开发学习】自定义View
- android学习_自定义View
- Android 自定义View学习(1)
- Android 自定义View学习(2)
- 【android学习】自定义view属性
- Android 学习之自定义View
- Android学习:简单自定义View
- Android自定义view学习笔记
- Android 自定义View学习2
- Android学习笔记-自定义view
- Android学习之自定义View
- Android自定义View基础学习
- 学习android必经之路自定义View
- Java虚拟机 (JVM)运行机制_动力节点Java学院整理
- 221. Maximal Square (最大正方形)
- 递归总结
- 正则表达式语言
- JVM方法调用的那些事
- android学习---自定义View
- GPS坐标转百度地图坐标的方法
- Hybrid App (二)
- nova service的heatbeat机制
- Docker 学习系列--安装几种开发软件
- 解题报告:Codeforces Round #433 (Div. 1) D. Michael and Charging Stations (DP)
- 动态SQL中的绑定变量——参数模式、占位符以及NULL值的处理
- ios 项目 .gitignore 文件配置
- mysql启动问题修复