RxJava初入學習(一)
Gifts-for-designers
Introduction
一個為設計師設計的APP( ′_ゝ`)一個特別簡單項目
主要用來練習Rx,當然這個適合初入Rx的看看。
自己參考的教程 給 Android 開發者的 RxJava 詳解
Github地址: Gifts-for-designers 可以給個star鼓勵下( ?? ̄)っ?╰?╯
Usage
( ′_ゝ`) 好像這個項目沒有很多代碼需要解釋,不過我還是整理一份
自定義字體
// 字體 Typeface fontFace = Typeface.createFromAsset(getAssets(), "font/Inconsolata.otf"); Typeface tvFontFace = Typeface.createFromAsset(getAssets(), "font/Lobster.ttf"); editTextHEX.setTypeface(fontFace); editTextRGB.setTypeface(fontFace); textView.setTypeface(tvFontFace);
Rx監聽輸入
RxTextView.textChanges(editTextHEX) .subscribeOn(AndroidSchedulers.mainThread()) .debounce(300, TimeUnit.MICROSECONDS) .map(new Func1<CharSequence, String>() { @Override public String call(CharSequence charSequence) { return charSequence.toString(); } }) .observeOn(AndroidSchedulers.mainThread()) .doOnNext(new Action1<String>() { @Override public void call(String s) { if (s.length() == 5) { setBackground("56abe4", 0); editTextRGB.setText(""); } if (!colors.isEmpty()) { colors.clear(); adapter.notifyDataSetChanged(); } } }) .filter(new Func1<String, Boolean>() { @Override public Boolean call(String s) { return s.length() > 5; } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<String>() { @Override public void call(String s) { setBackground(s, 0); // addColorAdapter(s); changeToRGB(s); } })
改變背景顏色(有個過渡效果)
/** * @param color * @param mark 0:HEX 1:RGB */ public void setBackground(String color, int mark) { int i = 0; ValueAnimator paramInteger; Drawable localDrawable = this.relativeLayout.getBackground(); if ((localDrawable instanceof ColorDrawable)) i = ((ColorDrawable) localDrawable).getColor(); if (mark == 0) { paramInteger = ValueAnimator.ofObject(new ArgbEvaluator(), i, Color .parseColor("#" + color)); } else { paramInteger = ValueAnimator.ofObject(new ArgbEvaluator(), i, mark); } paramInteger.setDuration(500L); paramInteger.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator paramAnonymousValueAnimator) { relativeLayout.setBackgroundColor((Integer) paramAnonymousValueAnimator.getAnimatedValue()); } } ); paramInteger.start(); }
將16進制轉化成RGB
private void changeToRGB(String s) { int red = Integer.parseInt(String.valueOf(s.charAt(0)) + s.charAt(1), 16); int green = Integer.parseInt(String.valueOf(s.charAt(2)) + s.charAt(3), 16); int blue = Integer.parseInt(String.valueOf(s.charAt(4)) + s.charAt(5), 16); Log.d(TAG, red + " " + green + " " + blue); editTextRGB.setText("(" + red + "," + green + "," + blue + ")"); }
Todo&Issues
- [x] 搭配色算法推薦
- [ ] 搭配色展示(RecyclerView)
- [ ] 目前只做了16進制顏色,RGB有些邏輯問題
- [ ] 刪除過快會有卡頓(不知道如何解決)
- [ ] 顏色保存列表
截圖
本文由用戶 idiotslkp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!