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

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

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

不少時候咱們須要給ImageView添加點擊效果,例如title上的back按鈕。android

一般來說,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文件。例如:git

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

這樣固然沒問題,畢竟都是你們熟悉的套路。不料,你忽然接到了一個需求,爲了支持動態換膚,這個back的圖片須要從網絡上獲取,而且仍然須要支持點擊效果。頓時,無數程序猿心中衆多那個啥在奔騰。github

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

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的舞臺呢?不過加載以下網頁的時候會有坑ide

沒錯,這個網頁裏面有視頻,用戶播放視頻,而後點擊了返回鍵,此時若是直接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.0post

4.控制Recyclerview滑動的問題

不少場景下,產品須要咱們經過代碼控制Recyclerview滑動到第幾個position,例如:用戶下拉刷新當天節目列表,咱們應該計算當前時間播放的是第幾個節目,而後滑動到這個position,注:此時這個position應該居於屏幕的中間ui

解決方案: 這裏只說一下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);
複製代碼

就妥啦。效果以下。

About Me

contact way value
mail weixinjie1993@gmail.com
wechat W2006292
github https://github.com/weixinjie
blog https://juejin.im/user/57673c83207703006bb92bf6
相關文章
相關標籤/搜索