Android 開發中不得不知道的 Tips 集合 (第二波)(持續更新 ing)

weixinjie發表於2019-02-27

相關文章 Android 開發中不得不知道的 Tips 集合 (持續更新 ing)

1.你還在寫Drawable來實現Imageview的點選效果?

很多時候我們需要給ImageView新增點選效果,例如title上的back按鈕。

Android 開發中不得不知道的 Tips 集合 (第二波)(持續更新 ing)
通常來講,UI那邊會給我們兩張圖,一張選中效果,一張nomal效果;我們會風騷的擼一個Drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按壓時 -->
    <item android:drawable="@mipmap/btn_enter_pressed" android:state_pressed="true" />
    <!-- 預設時 -->
    <item android:drawable="@mipmap/btn_enter_normal" />
</selector>
複製程式碼

然後在佈局檔案中把Imageview的background屬性設定成你寫的Drawable檔案。例如:

<ImageView
        android:id="@+id/tv_login"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/back_click" />
複製程式碼

這樣當然沒問題,畢竟都是大家熟悉的套路。不料,你突然接到了一個需求,為了支援動態換膚,這個back的圖片需要從網路上獲取,並且仍然需要支援點選效果。頓時,無數程式猿心中眾多那個啥在奔騰。

解決方案: 繼承ImageView,監聽OnTouchListener的事件,動態設定setColorFilter

public class ClickImageView extends AppCompatImageView {

    public ClickImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public ClickImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClickImageView(Context context) {
        super(context);
        init();
    }

    private void init() {
        setOnTouchListener(onTouchListener);
    }

    private OnTouchListener onTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    setColorFilter(null);
                    break;
                case MotionEvent.ACTION_DOWN:
                    changeLight();
                    break;
                case MotionEvent.ACTION_MOVE:
                    break;
                case MotionEvent.ACTION_CANCEL:
                    setColorFilter(null);
                    break;
                default:
                    break;
            }
            return false;
        }
    };

    private void changeLight() {
        int brightness = -80;
        ColorMatrix matrix = new ColorMatrix();
        matrix.set(new float[]{1, 0, 0, 0, brightness, 0, 1, 0, 0,
                brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0});
        setColorFilter(new ColorMatrixColorFilter(matrix));
    }
}

複製程式碼

佈局檔案中這麼搞妥了

    <你的包名.ClickImageView
        android:id="@+id/iv_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/act_ic_share" />
複製程式碼

2.WebView載入視訊or音訊時候的二次元世界

現在APP裡面怎麼能少的了WebView的舞臺呢?不過載入如下網頁的時候會有坑

Android 開發中不得不知道的 Tips 集合 (第二波)(持續更新 ing)
沒錯,這個網頁裡面有視訊,使用者播放視訊,然後點選了返回鍵,此時如果直接finish掉當前的WebActivity時會出現靈異的現象:剛才看的視訊仍然在播放,仍然會有聲音發出。除非你exit掉我們們的App。

解決方案: 在WebActivity中控制一下WebView,親測有效

    @Override
    protected void onResume() {
        super.onResume();
        wb_content.onResume();
    }
    
     @Override
    protected void onDestroy() {
        super.onDestroy();
        wb_content.destroy();  //手動銷燬WebView
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        wb_content.onPause();
    }
複製程式碼

3.是時候從Rxjava1換到Rxjava2啦

還沒玩過Rxjava的同學們建議直接從Rxjava2學起,現在還在奮鬥在Rxjava1的同學們建議儘快轉到Rxjava2的戰線。Rxjava1很快就停止更新了。廢話不多說,直接祭出官方wiki https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0

4.控制Recyclerview滑動的問題

很多場景下,產品需要我們通過程式碼控制Recyclerview滑動到第幾個position,例如:使用者下拉重新整理當天節目列表,我們應該計算當前時間播放的是第幾個節目,然後滑動到這個position,注:此時這個position應該居於螢幕的中間

解決方案: 這裡只說一下LinearLayoutManager下的解決方式

public class CenterLayoutManager extends LinearLayoutManager {

    public CenterLayoutManager(Context context) {
        super(context);
    }

    public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    private static class CenterSmoothScroller extends LinearSmoothScroller {

        CenterSmoothScroller(Context context) {
            super(context);
        }

        @Override
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
            return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
        }
    }
}
複製程式碼

然後使用Recyclerview的時候,設定LayoutManager為CenterLayoutManager。需要滾動到第幾個item直接呼叫

recyclerview.smoothScrollToPosition(position);
複製程式碼

就妥啦。效果如下。

Android 開發中不得不知道的 Tips 集合 (第二波)(持續更新 ing)

About Me

contact way value
mail weixinjie1993@gmail.com
wechat W2006292
github https://github.com/weixinjie
blog https://juejin.im/user/57673c83207703006bb92bf6

相關文章