Android自定義View實(shí)現(xiàn)選座功能
我們?cè)诎沧块_(kāi)發(fā)中安卓自帶的控件滿(mǎn)足不了我們的需求,因此我們就需要用到自定義View來(lái)滿(mǎn)足我們的需求,在這里我要講解的是自定義View實(shí)現(xiàn)選座功能,在安卓中一個(gè)會(huì)使用自定義View的人一定會(huì)開(kāi)發(fā)出與眾不同以及美觀的項(xiàng)目
首先,我展示一下效果
以上主要就是我們需要?jiǎng)?chuàng)建一個(gè)我們自己的View繼承自Viewgroup控件并實(shí)現(xiàn)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是我們根據(jù)選座接口中的返回值判斷座位是否已經(jīng)被選,大家可以參考一下我的Bean類(lèi),但具體的做法還要以自己的接口文件為主
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; } } } }
最后,我們需要在布局文件里進(jìn)行調(diào)用,我的布局文件是這樣的
<?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="請(qǐng)開(kāi)始選座購(gòu)票" 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>
以上就是我的選座功能實(shí)現(xiàn)的全過(guò)程,歡迎大家提問(wèn)
總結(jié)
到此這篇關(guān)于Android自定義View實(shí)現(xiàn)選座功能的文章就介紹到這了,更多相關(guān)android自定義view選座內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android基于OkHttpUtils網(wǎng)絡(luò)請(qǐng)求的二次封裝
這篇文章主要介紹了Android基于OkHttpUtils網(wǎng)絡(luò)請(qǐng)求的二次封裝,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03AndroidStudio3.6.1打包jar及AndroidStudio4.0打包jar的一系列問(wèn)題及用法
這篇文章主要介紹了AndroidStudio3.6.1打包jar,AndroidStudio4.0打包jar的問(wèn)題及用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android實(shí)現(xiàn)彈窗進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)彈窗進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android 使用Zbar實(shí)現(xiàn)掃一掃功能
這篇文章主要介紹了Android 使用Zbar實(shí)現(xiàn)掃一掃功能,本文用的是Zbar實(shí)現(xiàn)掃一掃,因?yàn)楦鶕?jù)本人對(duì)兩個(gè)庫(kù)的使用比較,發(fā)現(xiàn)Zbar解碼比Zxing速度要快,實(shí)現(xiàn)方式也簡(jiǎn)單,需要的朋友可以參考下2023-03-03Android實(shí)現(xiàn)微信側(cè)滑刪除當(dāng)前頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信側(cè)滑刪除當(dāng)前頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android Studio中Run按鈕是灰色的快速解決方法
這篇文章主要介紹了Android Studio中Run按鈕是灰色的快速解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03Android開(kāi)發(fā)獲取傳感器數(shù)據(jù)的方法示例【加速度傳感器,磁場(chǎng)傳感器,光線傳感器,方向傳感器】
這篇文章主要介紹了Android開(kāi)發(fā)獲取傳感器數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Android獲取加速度傳感器、磁場(chǎng)傳感器、光線傳感器及方向傳感器數(shù)據(jù)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11