Android中画桌子椅子的几种方法(动态添加座子椅子)(一)
第一种:自定义View继承ViewGroup实现
自定义viewgroup,通过对xml中的每个控件获取他们的宽高,然后根据三角函数关系进行位置的放置
onMeasure是测量每个子控件的宽高
onLayout 是确定每个子控件要摆放到哪
首先是我们定义一个xml文件,然后通过自定义view获取里面的子空间,然后进行位置摆放
下面是xml文件代码:
<com.myview.MyWeiZhi
xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:zph="http://schemas.android.com/apk/res-auto"android:id="@+id/id_menu"android:layout_width="match_parent"android:layout_height="match_parent"zph:radius1="12dp" ><RelativeLayout
android:layout_width="15dp"android:id="@+id/sp_user_imager"android:background="@drawable/yuan_dan"android:layout_height="15dp"><TextView
android:id="@+id/sp_user_text"android:layout_width="15dp"android:layout_centerInParent="true"android:text="01"android:textSize="8sp"android:gravity="center"android:textColor="@color/colorbasse"android:layout_height="15dp" /></RelativeLayout><ImageView
android:id="@+id/image1"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image2"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image3"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image4"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image5"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image6"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image7"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image8"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image9"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/><ImageView
android:id="@+id/image10"android:layout_width="4dp"android:layout_height="4dp"android:background="@drawable/hui_yuan"/></com.myview.MyWeiZhi>
这里是10个小圆和一个大圆和大圆中的文字(序号)的布局文件.
下面是我们的自定义view代码
*** Created by adminZPH on 2016/12/21.* 这里是对桌位的每一个小的座位的划线,包含一个大的圆,并且大圆周边有12个小的圆* 默认的属性是:大圆的背景是#FAE4E7 小圆的背景是#D5D3D4* 选中的状态的属性是:大圆背景是红色 小圆也一样*** 总共的个数是不确定的**/
public class MyWeiZhi extends ViewGroup {/*10个椅子的时候private static final Double sin= Math.sin(36*Math.PI/180);private static final Double sin18= Math.sin(18*Math.PI/180);private static final Double cos= Math.cos(36*Math.PI/180);private static final Double cos18= Math.cos(18*Math.PI/180);*/private int mRadius;private int seatnumber=10;//默认是10个防止出错private int seatnumbernumber=0;//如果想安装坐标点进行摆放的话,可以将onLayout中的x和y换成xx,yy;private int xx;//坐标点private int yy;//坐标点private View mCButton;//中间大圆public MyWeiZhi(Context context,int seatnumber,int seatnumbernumber,int xx,int yy){this(context,null, 0,seatnumber,seatnumbernumber,xx,yy);}public MyWeiZhi(Context context, AttributeSet attrs){this(context, attrs, 0,0,0,0,0);}public MyWeiZhi(Context context, AttributeSet attrs, int defStyle,int seatnumber,int seatnumbernumber,int xx,int yy){super(context, attrs, defStyle);this.seatnumber=seatnumber;this.seatnumbernumber=seatnumbernumber;this.xx= (int) SizeUtils.pxTodp(context,xx);this.yy= (int) SizeUtils.pxTodp(context,yy);mRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50, getResources().getDisplayMetrics());TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.MyWeizhi, defStyle, 0);mRadius = (int) a.getDimension(R.styleable.MyWeizhi_radius1, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,getResources().getDisplayMetrics()));a.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){int count = getChildCount();for (int i = 0; i < count; i++){measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b){if (changed){/*** 当前控件的父控件的中心坐标* */int x=getMeasuredWidth()/2;int y=getMeasuredHeight()/2;/*** 中心点绘制* */mCButton = getChildAt(0);int cl1 = x;int ct1 = y;int cWidth1 = mCButton.getMeasuredWidth();int cHeight1 = mCButton.getMeasuredHeight();mCButton.layout(cl1-cWidth1/2, ct1-cHeight1/2, cl1 + cWidth1/2, ct1 +cHeight1/2);/*** 接着进行绘制小椅子,根据服务器获得的seatnumber个数进行* layout(Left,Top,Right,Bottom)* 绘制全部都根据每个控件的中心点坐标进行绘制,保证刚好平均分配并且在当前父控件的中心位置* 根据三角函数关系 sin和cos进行获得每个子控件的中心点位置** */for (int i = 0; i < seatnumber; i++){View child = getChildAt(i + 1);// L T R Bint cl = (int) (x+mRadius*Math.sin((360/seatnumber*i)*Math.PI/180));int ct = (int) (y-mRadius*Math.cos((360/seatnumber*i)*Math.PI/180));int cWidth = child.getMeasuredWidth();int cHeight = child.getMeasuredHeight();child.layout(cl-cWidth/2, ct-cHeight/2, cl + cWidth/2, ct + cHeight/2);}}}}
在activity中可以进行初始话,并addview到布局中去
MyWeiZhi mMyWeiZhi=new MyWeiZhi(this,1,12,100,100);
这是第一种方法,通过自定义View继承ViewGroup实现
Android中画桌子椅子的几种方法(动态添加座子椅子)(一)相关推荐
- android onclick方法吗,Android中button的onClick事件几种方法
Android中button的onClick事件几种方法 发布时间:2020-09-03 02:36:31 来源:脚本之家 阅读:100 作者:水桶前辈 Android中button的onClick事 ...
- android中图片圆角,Android中实现圆角图片的几种方法
Android中实现圆角图片有多种姿势,不知你解锁了几种? 方法一:setXfermode法 此种方式就是再new一个相同尺寸的bitmap,然后使用paint.setXfermode(new Por ...
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
这篇文章转自博客园 Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putPa ...
- Java/Android中汉字转拼音的两种方法,优劣比较
一.前言 在我们的开发中,有时会有这样的需求,就是联系人列表按照拼音顺序排列(如通讯录).于是,我也在网上搜到了许多这类的文章,就两种最常见的做法在此进行简单的比较和分析 二.汉字转拼音的方法 使用第 ...
- Android中设置文本颜色的五种方法
0.利用系统自带的颜色类 tx.setTextColor(android.graphics.Color.RED); 一.利用系统自带颜色XML文件 @android:color/holo_red_da ...
- Android中汉字转拼音的两种方法,优劣比较
一.前言 在我们的开发中,有时会有这样的需求,就是联系人列表按照拼音顺序排列(如通讯录).于是,我也在网上搜到了许多这类的文章,就两种最常见的做法在此进行简单的比较和分析 二.汉字转拼音的方法 使用第 ...
- android获取位图字节数,Android中获取图片尺寸大小两种方法
两种方法 建议用第二种 private void getPictureSize(String path) { /*第一种直接把bitmap加载到内存中,通过对bitmap的测量, 得出宽高,由于这个 ...
- [Android] Eclipse Android中设置模拟器屏幕大小几种方法
在Eclipse Android中设置模拟器屏幕大小是本文要介绍的内容,主要是来了解并学习Eclipse Android中模拟器的设置,具体关于Eclipse Android内容的详解来看本文 . ...
- Android中获取资源文件的几种方法
1.通过String获取其在R.drawable中的int try{ Field field=R.drawable.class.getField("icon"); int i= f ...
最新文章
- Promise的实例用法
- 人工智能浪潮褪去,冲刺IPO成AI企业生存关键?
- python读取excel表格-python读写excel文件
- 第十一周编程总结--助教
- 博本 微型 电脑 linux,博本电脑下载与安装Windows7iso镜像系统教程
- el-table-column中格式化判断数据为空则显示指定内容
- 利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)
- js事件之event.preventDefault()与event.stopPropagation()用法区别
- Android 沙箱开源,Android Sandbox(沙箱)开源工具介绍
- Oracle Internal Event:10200 Consistent Read诊断事件
- C语言数组相似度比对,某课程设计---文件相似度判断
- hadoop连接远程mysql_MySQL设置远程连接
- C++---布隆过滤器
- 要把人工智能提速50倍的ARM,却依然坚持做“通用的计算架构”
- python爬虫爬取慕课网中的图片
- 鸿蒙推送荣耀,华为鸿蒙首批推送机型8款,荣耀“避嫌”,不在首批名单
- Atitit word结构化提取考试试题读取 poi读取word nlp aiprj 目录 1.1. 结构化后数据	1 1.2. 文字读取	1 1.3. 图片读取	1 1.4. Doc的表格读取 /
- 已知相关系数求解联合分布律
- 支付宝支付时提示“长时间没操作,请重新发起请求”的错误处理
- 用python分析股票收益影响因素的方法_用Python分析股票的收益和风险