Android自定义View实现选座功能
作者:代码馨 发布时间:2022-12-23 00:05:45
标签:android,自定义view,选座
我们在安卓开发中安卓自带的控件满足不了我们的需求,因此我们就需要用到自定义View来满足我们的需求,在这里我要讲解的是自定义View实现选座功能,在安卓中一个会使用自定义View的人一定会开发出与众不同以及美观的项目
首先,我展示一下效果
以上主要就是我们需要创建一个我们自己的View继承自Viewgroup控件并实现onMeasure以及onDraw方法
具体的代码是这样的
public class SearView extends ViewGroup {
private Context context;
public SearView(@NonNull Context context) {
super(context);
}
public SearView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context=getContext();
}
public SearView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private ArrayList<SeatinfoBean.ResultBean> mlist;
public void setData(ArrayList<SeatinfoBean.ResultBean> list){
this.mlist = list;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mlist != null && mlist.size() > 0) {
for (int i = 0; i < mlist.size(); i++) {
SeatinfoBean.ResultBean resultBean = mlist.get(i);
resultBean.draw(canvas,context);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
float x = event.getX();
float y = event.getY();
completeByXY(x,y);
break;
}
return true;
}
public void completeByXY(float x,float y){
for (int i=0;i<mlist.size();i++){
SeatinfoBean.ResultBean resultBean1 = mlist.get(i);
int left = resultBean1.getLeft();
int right = resultBean1.getRight();
int bottom = resultBean1.getBottom();
int top = resultBean1.getTop();
if (x>=left&&x<right&&y>=top&&y<=bottom){
clickedSeat.clickedSeat(resultBean1);
int status = resultBean1.getStatus();
if (status==1){
status=3;
resultBean1.setStatus(status);
}else if (status==3){
status=1;
resultBean1.setStatus(status);
}
break;
}
}
postInvalidate();
}
public interface ClickedSeat{
void clickedSeat(SeatinfoBean.ResultBean resultBean);
}
private ClickedSeat clickedSeat;
public void setClickedSeat(ClickedSeat clickedSeat) {
this.clickedSeat = clickedSeat;
}
}
以上的resultBean是我们根据选座接口中的返回值判断座位是否已经被选,大家可以参考一下我的Bean类,但具体的做法还要以自己的接口文件为主
public static class ResultBean {
/**
* row : 1
* seat : 1
* status : 2
*/
private String row;
private String seat;
private int status;
private int left;
private int top;
private int right;
private int bottom;
private Context context;
private boolean ist = false;
public String getRow() {
return row;
}
public void setRow(String row) {
this.row = row;
}
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getLeft() {
return left;
}
public void setLeft(int left) {
this.left = left;
}
public int getTop() {
return top;
}
public void setTop(int top) {
this.top = top;
}
public int getRight() {
return right;
}
public void setRight(int right) {
this.right = right;
}
public int getBottom() {
return bottom;
}
public void setBottom(int bottom) {
this.bottom = bottom;
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
public boolean isIst() {
return ist;
}
public void setIst(boolean ist) {
this.ist = ist;
}
public void draw(Canvas canvas,Context context){
if (status==2){
BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.xuan);
Bitmap bitmap = drawable.getBitmap();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int row = Integer.parseInt(getRow());
int seat = Integer.parseInt(getSeat());
row = row*50;
seat = seat*50;
canvas.drawBitmap(bitmap,seat,row,new Paint());
left = seat;
top = row;
right = seat+width;
bottom = row+height;
}
if (status==1){
BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.xuan1);
Bitmap bitmap = drawable.getBitmap();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int row = Integer.parseInt(getRow());
int seat = Integer.parseInt(getSeat());
row = row*50;
seat = seat*50;
canvas.drawBitmap(bitmap,seat,row,new Paint());
left = seat;
top = row;
right = seat+width;
bottom = row+height;
}
if (status==3){
BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.xuan3);
Bitmap bitmap = drawable.getBitmap();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int row = Integer.parseInt(getRow());
int seat = Integer.parseInt(getSeat());
row = row*50;
seat = seat*50;
canvas.drawBitmap(bitmap,seat,row,new Paint());
left = seat;
top = row;
right = seat+width;
bottom = row+height;
}
}
}
}
最后,我们需要在布局文件里进行调用,我的布局文件是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.XuanZuoActivity"
android:orientation="vertical">
<TextView
android:id="@+id/xuanzuotext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请开始选座购票"
android:background="#140404"
android:gravity="center"
android:textSize="@dimen/permission_dp_30"
android:textColor="#fff"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#140404">
<TextView
android:id="@+id/weixuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="未选座"
android:textColor="#fff"
android:layout_marginLeft="@dimen/permission_dp_20"
android:layout_marginTop="@dimen/dp_3"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xuan1"/>
<TextView
android:id="@+id/weixuan2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已选座"
android:textColor="#fff"
android:layout_marginLeft="@dimen/dp_210"
android:layout_marginTop="@dimen/dp_3"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xuan"/>
</LinearLayout>
<com.bw.movie.SearView
android:id="@+id/xuanzuo"
android:background="#140404"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_weight="6"
/>
<Button
android:id="@+id/button_xuanzuo"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0"
android:layout_weight="0.5"
android:text="立即支付"
android:background="#E91E63"/>
</LinearLayout>
来源:https://blog.csdn.net/m0_48436034/article/details/108335756


猜你喜欢
- 文件创建:File.Create(Application.StartupPath + "\\AlarmSet.txt")
- 今天分享的是用系统自带的相机实现一键拍照功能。public class MainActivity extends AppCompatActi
- 当我们第一次下载QQ并且打开的时候,会有一个新手引导,引导是几张图片,再加上一些文字说明,向右滑动,直到结束,今天一大早起来研究了一下关于此
- JVM默认物理内存JVM初始分配的内存由-Xms指定,默认是物理内存的1/64;JVM最大分配的内存由-Xmx指定,默认是物理内存的1/4。
- 本文实例讲述了C#通过xpath查找xml指定元素的方法。分享给大家供大家参考。具体如下:orders.xml文档内容如下<?xml
- 本文实现了一个有趣的小东西:使用自定义View绘图,一边画线,画出的线条渐渐变淡,直到消失。效果如下图所示:用属性动画或者渐变填充(Shad
- 本文实例讲述了C#实现让ListBox适应最大Item宽度的方法。分享给大家供大家参考。具体实现方法如下:private void butt
- 项目需求最近项目中有一个需求就是让Java代码去代替人工操作,自动生成PPT,具体就是查询数据库数据,然后根据模板文件(PPT),将数据库数
- 接上篇:播放器-IOS(Swift)篇安卓端原生播放器的接入思路与ios基本一致,所以本篇就不废话了,直接上代码:创建插件VideoView
- 今天在码代码的时候突然想到这个问题,觉得有点困惑。在网上也翻阅不少帖子其中有一个帖子给了我一个思路,其实也是解释了基础概念。概念一:try
- 浅谈java 执行jar包中的main方法通过 OneJar 或 Maven 打包后 jar 文件,用命令:java -jar ****.j
- 本过程是使用Virtual Studio 2019实现的,利用老师给出的框架进行的修改。一、认识NetworkStream(网络流)1、Ne
- 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If
- 一、tomcat内存设置问题 收藏 在使用Java程序从数据库中查询大量的数据或是应用服务器(如tomcat、jboss,weblogic)
- 前言现代化大型项目通常使用独立的数据库来存储数据,其中以采用关系型数据库居多。用于开发项目的高级语言(C#、Java等)是面向对象的,而关系
- 参考资料《Java 编程思想》,关于含有基类的导出类,其成员的初始化过程是一个容易让人困惑的地方,下面通过具体的实例进行讲解,代码取自《Ja
- 背景最近好几个项目在运行过程中客户都提出文件上传大小的限制能否设置的大一些,用户经常需要上传好几个G的资料文件,如图纸,视频等,并且需要在上
- 我们springboot项目有自己默认的配置文件,一般地由application.yml和bootstrap.yml组成,前者是模块的配置,
- XListview是一个非常受欢迎的下拉刷新控件,但是已经停止维护了。之前写过一篇XListview的使用介绍,用起来非常简单,这两天放假无
- 什么是 Retrofit ?Retrofit是Square开发的一个Android和Java的REST客户端库。这个库非常简单并且具有很多特