<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android自定義雙向滑動控制元件的具體程式碼,供大家參考,具體內容如下
先看一下效果圖
1.SeekBarPressure工具類
public class SeekBarPressure extends View { private static final String TAG = "SeekBarPressure"; private static final int CLICK_ON_LOW = 1; //點選在前滾軸上 private static final int CLICK_ON_HIGH = 2; //點選在後滾軸上 private static final int CLICK_IN_LOW_AREA = 3; private static final int CLICK_IN_HIGH_AREA = 4; private static final int CLICK_OUT_AREA = 5; private static final int CLICK_INVAILD = 0; /* * private static final int[] PRESSED_STATE_SET = { * android.R.attr.state_focused, android.R.attr.state_pressed, * android.R.attr.state_selected, android.R.attr.state_window_focused, }; */ private static final int[] STATE_NORMAL = {}; private static final int[] STATE_PRESSED = { android.R.attr.state_pressed, android.R.attr.state_window_focused, }; private Drawable hasScrollBarBg; //滑動條滑動後背景圖 private Drawable notScrollBarBg; //滑動條未滑動背景圖 private Drawable mThumbLow; //前滾軸 private Drawable mThumbHigh; //後滾軸 private int mScollBarWidth; //控制元件寬度=滑動條寬度+滑動塊寬度 private int mScollBarHeight; //滑動條高度 private int mThumbWidth; //滑動塊寬度 private int mThumbHeight; //滑動塊高度 private double mOffsetLow = 0; //前滾軸中心座標 private double mOffsetHigh = 0; //後滾軸中心座標 private int mDistance = 0; //總刻度是固定距離 兩邊各去掉半個滾軸距離 private int mThumbMarginTop = 30; //滑動塊頂部距離上邊框距離,也就是距離字型頂部的距離 private int mFlag = CLICK_INVAILD; private OnSeekBarChangeListener mBarChangeListener; private double defaultScreenLow = 0; //預設前滾軸位置百分比 private double defaultScreenHigh = 100; //預設後滾軸位置百分比 private boolean isEdit = false; //輸入框是否正在輸入 public SeekBarPressure(Context context) { this(context, null); } public SeekBarPressure(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SeekBarPressure(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // this.setBackgroundColor(Color.BLACK); Resources resources = getResources(); notScrollBarBg = resources.getDrawable(R.color.red); hasScrollBarBg = resources.getDrawable(R.color.blue); mThumbLow = resources.getDrawable(R.drawable.blue); mThumbHigh = resources.getDrawable(R.drawable.red); mThumbLow.setState(STATE_NORMAL); mThumbHigh.setState(STATE_NORMAL); mScollBarWidth = notScrollBarBg.getIntrinsicWidth(); mScollBarHeight = notScrollBarBg.getIntrinsicHeight(); mThumbWidth = mThumbLow.getIntrinsicWidth(); mThumbHeight = mThumbLow.getIntrinsicHeight(); } //預設執行,計算view的寬高,在onDraw()之前 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = measureWidth(widthMeasureSpec); // int height = measureHeight(heightMeasureSpec); mScollBarWidth = width; mOffsetHigh = width - mThumbWidth / 2; mOffsetLow = mThumbWidth / 2; mDistance = width - mThumbWidth; mOffsetLow = formatDouble(defaultScreenLow / 100 * (mDistance ))+ mThumbWidth / 2; mOffsetHigh = formatDouble(defaultScreenHigh / 100 * (mDistance)) + mThumbWidth / 2; setMeasuredDimension(width, mThumbHeight + mThumbMarginTop+2); } private int measureWidth(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); //wrap_content if (specMode == MeasureSpec.AT_MOST) { } //fill_parent或者精確值 else if (specMode == MeasureSpec.EXACTLY) { } return specSize; } private int measureHeight(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); int defaultHeight = 100; //wrap_content if (specMode == MeasureSpec.AT_MOST) { } //fill_parent或者精確值 else if (specMode == MeasureSpec.EXACTLY) { defaultHeight = specSize; } return defaultHeight; } protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Paint text_Paint = new Paint(); // text_Paint.setTextAlign(Paint.Align.CENTER); // text_Paint.setColor(Color.RED); // text_Paint.setTextSize(50); int aaa = mThumbMarginTop + mThumbHeight / 2 - mScollBarHeight / 2; int bbb = aaa + mScollBarHeight; //白色,不會動 notScrollBarBg.setBounds(mThumbWidth / 2, aaa, mScollBarWidth - mThumbWidth / 2, bbb); notScrollBarBg.draw(canvas); //藍色,中間部分會動 hasScrollBarBg.setBounds((int)mOffsetLow, aaa, (int)mOffsetHigh, bbb); hasScrollBarBg.draw(canvas); //前滾軸 mThumbLow.setBounds((int)(mOffsetLow - mThumbWidth / 2), mThumbMarginTop, (int)(mOffsetLow + mThumbWidth / 2), mThumbHeight + mThumbMarginTop); mThumbLow.draw(canvas); //後滾軸 mThumbHigh.setBounds((int)(mOffsetHigh - mThumbWidth / 2), mThumbMarginTop, (int)(mOffsetHigh + mThumbWidth / 2), mThumbHeight + mThumbMarginTop); mThumbHigh.draw(canvas); double progressLow = formatDouble((mOffsetLow - mThumbWidth / 2) * 100 / mDistance); double progressHigh = formatDouble((mOffsetHigh - mThumbWidth / 2) * 100 / mDistance); // Log.d(TAG, "onDraw-->mOffsetLow: " + mOffsetLow + " mOffsetHigh: " + mOffsetHigh + " progressLow: " + progressLow + " progressHigh: " + progressHigh); // canvas.drawText((int) progressLow + "", (int)mOffsetLow - 2 - 2, 15, text_Paint); // canvas.drawText((int) progressHigh + "", (int)mOffsetHigh - 2, 15, text_Paint); if (mBarChangeListener != null) { if (!isEdit) { mBarChangeListener.onProgressChanged(this, progressLow, progressHigh); } } } @Override public boolean onTouchEvent(MotionEvent e) { //按下 if (e.getAction() == MotionEvent.ACTION_DOWN) { if (mBarChangeListener != null) { mBarChangeListener.onProgressBefore(); isEdit = false; } mFlag = getAreaFlag(e); // Log.d(TAG, "e.getX: " + e.getX() + "mFlag: " + mFlag); // Log.d("ACTION_DOWN", "------------------"); if (mFlag == CLICK_ON_LOW) { mThumbLow.setState(STATE_PRESSED); } else if (mFlag == CLICK_ON_HIGH) { mThumbHigh.setState(STATE_PRESSED); } else if (mFlag == CLICK_IN_LOW_AREA) { mThumbLow.setState(STATE_PRESSED); //如果點選0-mThumbWidth/2座標 if (e.getX() < 0 || e.getX() <= mThumbWidth/2) { mOffsetLow = mThumbWidth/2; } else if (e.getX() > mScollBarWidth - mThumbWidth/2) { // mOffsetLow = mDistance - mDuration; mOffsetLow = mThumbWidth/2 + mDistance; } else { mOffsetLow = formatDouble(e.getX()); // if (mOffsetHigh<= mOffsetLow) { // mOffsetHigh = (mOffsetLow + mDuration <= mDistance) ? (mOffsetLow + mDuration) // : mDistance; // mOffsetLow = mOffsetHigh - mDuration; // } } } else if (mFlag == CLICK_IN_HIGH_AREA) { mThumbHigh.setState(STATE_PRESSED); // if (e.getX() < mDuration) { // mOffsetHigh = mDuration; // mOffsetLow = mOffsetHigh - mDuration; // } else if (e.getX() >= mScollBarWidth - mThumbWidth/2) { // mOffsetHigh = mDistance + mThumbWidth/2; if(e.getX() >= mScollBarWidth - mThumbWidth/2) { mOffsetHigh = mDistance + mThumbWidth/2; } else { mOffsetHigh = formatDouble(e.getX()); // if (mOffsetHigh <= mOffsetLow) { // mOffsetLow = (mOffsetHigh - mDuration >= 0) ? (mOffsetHigh - mDuration) : 0; // mOffsetHigh = mOffsetLow + mDuration; // } } } //設定進度條 refresh(); //移動move } else if (e.getAction() == MotionEvent.ACTION_MOVE) { // Log.d("ACTION_MOVE", "------------------"); if (mFlag == CLICK_ON_LOW) { if (e.getX() < 0 || e.getX() <= mThumbWidth/2) { mOffsetLow = mThumbWidth/2; } else if (e.getX() >= mScollBarWidth - mThumbWidth/2) { mOffsetLow = mThumbWidth/2 + mDistance; mOffsetHigh = mOffsetLow; } else { mOffsetLow = formatDouble(e.getX()); if (mOffsetHigh - mOffsetLow <= 0) { mOffsetHigh = (mOffsetLow <= mDistance+mThumbWidth/2) ? (mOffsetLow) : (mDistance+mThumbWidth/2); } } } else if (mFlag == CLICK_ON_HIGH) { if (e.getX() < mThumbWidth/2) { mOffsetHigh = mThumbWidth/2; mOffsetLow = mThumbWidth/2; } else if (e.getX() > mScollBarWidth - mThumbWidth/2) { mOffsetHigh = mThumbWidth/2 + mDistance; } else { mOffsetHigh = formatDouble(e.getX()); if (mOffsetHigh - mOffsetLow <= 0) { mOffsetLow = (mOffsetHigh >= mThumbWidth/2) ? (mOffsetHigh) : mThumbWidth/2; } } } //設定進度條 refresh(); //擡起 } else if (e.getAction() == MotionEvent.ACTION_UP) { // Log.d("ACTION_UP", "------------------"); mThumbLow.setState(STATE_NORMAL); mThumbHigh.setState(STATE_NORMAL); if (mBarChangeListener != null) { mBarChangeListener.onProgressAfter(); } //這兩個for迴圈 是用來自動對齊刻度的,註釋後,就可以自由滑動到任意位置 // for (int i = 0; i < money.length; i++) { // if(Math.abs(mOffsetLow-i* ((mScollBarWidth-mThumbWidth)/ (money.length-1)))<=(mScollBarWidth-mThumbWidth)/(money.length-1)/2){ // mprogressLow=i; // mOffsetLow =i* ((mScollBarWidth-mThumbWidth)/(money.length-1)); // invalidate(); // break; // } // } // // for (int i = 0; i < money.length; i++) { // if(Math.abs(mOffsetHigh-i* ((mScollBarWidth-mThumbWidth)/(money.length-1) ))<(mScollBarWidth-mThumbWidth)/(money.length-1)/2){ // mprogressHigh=i; // mOffsetHigh =i* ((mScollBarWidth-mThumbWidth)/(money.length-1)); // invalidate(); // break; // } // } } return true; } public int getAreaFlag(MotionEvent e) { int top = mThumbMarginTop; int bottom = mThumbHeight + mThumbMarginTop; if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetLow - mThumbWidth / 2) && e.getX() <= mOffsetLow + mThumbWidth / 2) { return CLICK_ON_LOW; } else if (e.getY() >= top && e.getY() <= bottom && e.getX() >= (mOffsetHigh - mThumbWidth / 2) && e.getX() <= (mOffsetHigh + mThumbWidth / 2)) { return CLICK_ON_HIGH; } else if (e.getY() >= top && e.getY() <= bottom && ((e.getX() >= 0 && e.getX() < (mOffsetLow - mThumbWidth / 2)) || ((e.getX() > (mOffsetLow + mThumbWidth / 2)) && e.getX() <= ((double) mOffsetHigh + mOffsetLow) / 2))) { return CLICK_IN_LOW_AREA; } else if (e.getY() >= top && e.getY() <= bottom && (((e.getX() > ((double) mOffsetHigh + mOffsetLow) / 2) && e.getX() < (mOffsetHigh - mThumbWidth / 2)) || (e .getX() > (mOffsetHigh + mThumbWidth/2) && e.getX() <= mScollBarWidth))) { return CLICK_IN_HIGH_AREA; } else if (!(e.getX() >= 0 && e.getX() <= mScollBarWidth && e.getY() >= top && e.getY() <= bottom)) { return CLICK_OUT_AREA; } else { return CLICK_INVAILD; } } //更新滾軸 private void refresh() { invalidate(); } //設定前滾軸的值 public void setProgressLow(double progressLow) { this.defaultScreenLow = progressLow; mOffsetLow = formatDouble(progressLow / 100 * (mDistance ))+ mThumbWidth / 2; isEdit = true; refresh(); } //設定後滾軸的值 public void setProgressHigh(double progressHigh) { this.defaultScreenHigh = progressHigh; mOffsetHigh = formatDouble(progressHigh / 100 * (mDistance)) + mThumbWidth / 2; isEdit = true; refresh(); } public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener) { this.mBarChangeListener = mListener; } //回撥函數,在滑動時實時呼叫,改變輸入框的值 public interface OnSeekBarChangeListener { //滑動前 public void onProgressBefore(); //滑動時 public void onProgressChanged(SeekBarPressure seekBar, double progressLow, double progressHigh); //滑動後 public void onProgressAfter(); } /* private int formatInt(double value) { BigDecimal bd = new BigDecimal(value); BigDecimal bd1 = bd.setScale(0, BigDecimal.ROUND_HALF_UP); return bd1.intValue(); }*/ public static double formatDouble(double pDouble) { BigDecimal bd = new BigDecimal(pDouble); BigDecimal bd1 = bd.setScale(2, BigDecimal.ROUND_HALF_UP); pDouble = bd1.doubleValue(); return pDouble; } }
2.activity_main.xml 佈局
<?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=".MainActivity" android:background="#000" android:orientation="vertical"> <com.yjjk.doubleseekbarsss.SeekBarPressure android:id="@+id/seekBar_tg2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> <TextView android:id="@+id/editTexts_min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#fff"></TextView> <TextView android:id="@+id/editTexts_max" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#fff"></TextView> </LinearLayout>
3.MainActivity
public class MainActivity extends AppCompatActivity { private boolean isScreen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //id final SeekBarPressure seekBarPressures = findViewById(R.id.seekBar_tg2); final TextView editTexts_min = findViewById(R.id.editTexts_min); final TextView editTexts_max = findViewById(R.id.editTexts_max); seekBarPressures.setOnSeekBarChangeListener(new SeekBarPressure.OnSeekBarChangeListener() { @Override public void onProgressBefore() { isScreen = true; } @Override public void onProgressChanged(SeekBarPressure seekBar, double progressLow, double progressHigh) { editTexts_min.setText((int) progressLow + ""); editTexts_max.setText((int) progressHigh + ""); //獲取SharedPreferences物件 SharedPreferences sharedPre=getSharedPreferences("config", MODE_PRIVATE); //獲取Editor物件 SharedPreferences.Editor edit = sharedPre.edit(); edit.clear(); //設定引數 edit.putString("progressHigh", String.valueOf(progressHigh)); edit.putString("progressLow", String.valueOf(progressLow)); //提交 edit.commit(); } @Override public void onProgressAfter() { isScreen = false; } }); //取值 SharedPreferences sharedPre=getSharedPreferences("config",MODE_PRIVATE); String progressHighs = sharedPre.getString("progressHigh", ""); String progressLows = sharedPre.getString("progressLow", ""); if (progressHighs.length()!=0){ seekBarPressures.setProgressHigh(Double.parseDouble(progressHighs)); seekBarPressures.setProgressLow(Double.parseDouble(progressLows)); editTexts_max.setText(progressHighs); editTexts_min.setText(progressLows); }else { seekBarPressures.setProgressLow(30); seekBarPressures.setProgressHigh(70); editTexts_min.setText("最小值:"+30); editTexts_max.setText("最大值:"+70); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45