av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Android studio實現(xiàn)滑動開關

瀏覽:88日期:2022-09-27 08:38:25

大家好,今天剛學會使用Android Studio實現(xiàn)滑動開關的效果,自己感覺還可以,和大家分享一下,如果覺得可以的可以拿去,然后再給我點個贊,謝謝。本人也是學Android studiok開發(fā)不久,是個菜鳥,各位大佬覺得有不好的地方,可以討論一下,共同學習,一起進步。

實現(xiàn)效果

Android studio實現(xiàn)滑動開關

Android studio實現(xiàn)滑動開關

下面是代碼,代碼寫的比較粗糙,注釋沒有寫的很好,需要用的可以自己移植一下

package com.example.biansheng2;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.graphics.Typeface;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import androidx.annotation.Nullable;/** * 用于選擇手動模式還是路線規(guī)劃模式 */public class ModelSelectView extends View { Paint backPaint = new Paint();//背景畫筆 Paint facePaint = new Paint();//表面視圖畫筆 Paint colorPaint1 = new Paint();//繪制紅色和藍色 Paint colorPaint2 = new Paint();//繪制橙色 Paint colorPaint3 = new Paint();//繪制灰色 Paint handFont = new Paint();//手動控制模式字體 Paint routeFont = new Paint();//路線規(guī)劃模式字體 boolean modelFlag = false;//false表示為手動控制模式,true表示為路線規(guī)劃模式 RectF mRectF1 = new RectF(150, 840, 580, 960); int x = 150, y = 900;//得到手指再控件上移動的坐標 public ModelSelectView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paintInit(); canvas.drawRect(mRectF1, backPaint); canvas.drawCircle(150, 900, 60, backPaint); canvas.drawCircle(580, 900, 60, backPaint); if (!modelFlag){ canvas.drawCircle(150, 900, 60, facePaint); canvas.drawRect(150,840,x,960,facePaint); //設置手動控制字體可見 handFont.setAlpha(255); //設置路線規(guī)劃字體不可見 routeFont.setAlpha(0); }else { canvas.drawCircle(580, 900, 60, facePaint); canvas.drawRect(x,840,580,960,facePaint); //設置手動控制字體不可見 handFont.setAlpha(0); //設置路線規(guī)劃字體可見 routeFont.setAlpha(255); } canvas.drawText('手動控制',250,915,handFont); canvas.drawText('路線規(guī)劃',250,915,routeFont); canvas.drawCircle(x, y, 44, backPaint); canvas.drawCircle(x, y, 40, colorPaint2); canvas.drawCircle(x, y, 47, colorPaint3); } private void paintInit() { if (!modelFlag) { backPaint.setAntiAlias(true); backPaint.setColor(Color.parseColor('#ff00b5eb')); facePaint.setColor(Color.parseColor('#D81B60')); facePaint.setAntiAlias(true); }else { backPaint.setAntiAlias(true); backPaint.setColor(Color.parseColor('#D81B60')); facePaint.setColor(Color.parseColor('#ff00b5eb')); facePaint.setAntiAlias(true); } colorPaint2.setAntiAlias(true); colorPaint2.setColor(Color.parseColor('#FF6407')); colorPaint1.setAntiAlias(true); colorPaint1.setColor(Color.BLUE); colorPaint3.setColor(Color.WHITE); colorPaint3.setAntiAlias(true); colorPaint3.setStyle(Paint.Style.STROKE); colorPaint3.setStrokeWidth(5); Typeface font = Typeface.create(Typeface.DEFAULT_BOLD,Typeface.BOLD_ITALIC); handFont.setTypeface(font); handFont.setColor(Color.WHITE); handFont.setAntiAlias(true); handFont.setTextSize(50); routeFont.setTypeface(font); routeFont.setColor(Color.WHITE); routeFont.setAntiAlias(true); routeFont.setTextSize(50); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int getX = (int) event.getX(); int getY = (int) event.getY(); //判斷得到的坐標是否在視圖范圍之內(nèi) if (getX >= 150 && getX <= 580 && getY >= 840 && getY <= 960) { x = getX; y = 900; } break; case MotionEvent.ACTION_UP: int getX1 = (int) event.getX(); int getY1 = (int) event.getY(); //判斷是否在控件內(nèi) if (getY1 >= 810 && getY1 <= 990) { if (getX1 <= 280) { x = 150; modelFlag = false; } else if (getX1 >= 443) { x = 580; modelFlag = true; } else { if (!modelFlag) { x = 150; modelFlag = false; } else { x = 580; modelFlag = true; } } } break; } invalidate();//刷新界面 return true; }}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
相關文章:
主站蜘蛛池模板: 久久亚洲一区二区三区四区 | 中文字幕在线视频网站 | 亚洲成人免费网址 | 日韩在线精品视频 | 国产在线一区二区三区 | 国产a级毛毛片 | 91精品国产色综合久久不卡蜜臀 | 日韩精品激情 | 日日碰狠狠躁久久躁96avv | 久久久久国产一区二区三区 | 成年人网站免费 | 欧美精品一区三区 | 国产精品免费福利 | 99久久久国产精品免费消防器 | 国产精品久久久久久久久久久免费看 | 欧美成人精品一区二区男人看 | 久久久女女女女999久久 | 欧美性tv | av网站免费看 | 欧美一级精品片在线看 | 丝袜美腿一区二区三区 | 综合成人在线 | 特级做a爰片毛片免费看108 | 欧美日韩在线一区二区三区 | 波多野结衣在线观看一区二区三区 | 精久久久 | 久久综合伊人一区二区三 | 狠狠狠干| 日韩成人免费视频 | 高清一区二区 | 九九免费观看视频 | 成人午夜高清 | 久久精品无码一区二区三区 | 精品国产一区二区三区久久久四川 | 精品三级在线观看 | 日韩精品在线免费 | 青青久在线视频 | 日本 欧美 三级 高清 视频 | 亚洲一级毛片 | 亚洲欧洲一区二区 | 中文字幕免费 |