仿QQ白夜间模式切换


 

MainActivity

 
package com.example.fangqqnight;

import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * 日间模式
     */
    private final static int DAY_THEME = 1;

    /**
     * 夜间模式
     */
    private final static int NIGHT_THEME = 2;

    private int width;
    private int height;
    private int statusBarHeight;

    private ImageView mMianDay;
    private ImageView mMainNone;
    private LinearLayout mLinearNightMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getInitData();
    }

    private void getInitData() {
        WindowManager wm = this.getWindowManager();
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();

        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
    }

    private void initView() {
        mMianDay = (ImageView) findViewById(R.id.mian_day);
        mMainNone = (ImageView) findViewById(R.id.main_none);
        mMainNone.setOnClickListener(this);
        mLinearNightMode = (LinearLayout) findViewById(R.id.linear_nightMode);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_none:
                switchDayNightTheme();
                //加载当前的主题
                if (getCurrentTheme() == DAY_THEME) {
                    setDayThemeInfo();
                } else if (getCurrentTheme() == NIGHT_THEME) {
                    setNightThemeInfo();
                } else {
                    setDayThemeInfo();
                }
                break;

        }
    }
    /**
     * 设置模式
     *
     * @param dayTheme
     */
    private void setMyTheme(int dayTheme) {
        switch (dayTheme) {
            case DAY_THEME:
                setDayTheme();
                break;
            case NIGHT_THEME:
                setNightTheme();
                break;
            default:
                setDayTheme();
                break;
        }
    }

    /**
     * 设置夜间模式
     */
    private void setNightTheme() {
        final ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height - statusBarHeight));
        Bitmap bitmap = loadBitmapFromView(mLinearNightMode);
        imageView.setImageBitmap(bitmap);
        mLinearNightMode.addView(imageView);
        //设置新主题
        setNightThemeInfo();
        int colorA = Color.parseColor("#ffffff");
        int colorB = Color.parseColor("#333444");
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(imageView, "backgroundColor", colorA, colorB);
        objectAnimator.setDuration(800);
        objectAnimator.setEvaluator(new ArgbEvaluator());
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mLinearNightMode.removeView(imageView);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        objectAnimator.start();
    }

    boolean flag = true;

    /**
     * 设置夜间模式具体代码
     */
    private void setNightThemeInfo() {
        mLinearNightMode.setBackgroundColor(Color.parseColor("#333444"));
        mMianDay.setImageResource(R.mipmap.yueliang2);
        mMainNone.setImageResource(R.mipmap.select);
    }

    /**
     * 设置日渐模式具体代码
     */
    private void setDayThemeInfo() {
        mLinearNightMode.setBackgroundColor(Color.parseColor("#FFFFFF"));
        mMianDay.setImageResource(R.mipmap.yueliang1);
        mMainNone.setImageResource(R.mipmap.none);
    }

    /**
     * 设置日间模式
     */
    private void setDayTheme() {
        final ImageView imageView = new ImageView(this);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height - statusBarHeight));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        Bitmap bitmap = loadBitmapFromView(mLinearNightMode);
        imageView.setImageBitmap(bitmap);
        mLinearNightMode.addView(imageView);
        //设置新主题
        setDayThemeInfo();

        int colorA = Color.parseColor("#333444");
        int colorB = Color.parseColor("#ffffff");
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(imageView, "backgroundColor", colorA, colorB);
        objectAnimator.setDuration(800);
        objectAnimator.setEvaluator(new ArgbEvaluator());
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mLinearNightMode.removeView(imageView);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        objectAnimator.start();
    }

    /**
     * 测试方法
     *
     * @param imageView
     */
    private void testDay(final ImageView imageView) {
        //设置imageView渐隐动画
        AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
        animation.setDuration(2000);
        animation.setFillAfter(true);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e("zhihu", "zhihu -- onAnimationStart");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.e("zhihu", "zhihu -- onAnimationEnd");
                mLinearNightMode.removeView(imageView);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        imageView.startAnimation(animation);
    }

    /**
     * 获取view截图对应的bitmap
     *
     * @param v
     *
     * @return
     */
    public Bitmap loadBitmapFromView(View v) {
        Bitmap b = Bitmap.createBitmap(width, height - statusBarHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
        v.draw(c);
        return b;
    }

    /**
     * 切换日渐模式或夜间模式
     */
    private void switchDayNightTheme() {
        int curMode = getCurrentTheme();
        switch (curMode) {
            case DAY_THEME:
                saveCurrentTheme(NIGHT_THEME);
                setMyTheme(NIGHT_THEME);
                break;
            case NIGHT_THEME:
                saveCurrentTheme(DAY_THEME);
                setMyTheme(DAY_THEME);
                break;
            default:
                saveCurrentTheme(DAY_THEME);
                setMyTheme(DAY_THEME);
                break;
        }
    }


    /**
     * 保存当前模式
     *
     * @param mode
     */
    private void saveCurrentTheme(int mode) {
        SharedPreferences preferences = getSharedPreferences("AppTheme", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("mode", mode);
        editor.apply();
    }


    /**
     * 获取当前模式
     *
     * @m mode
     */
    private int getCurrentTheme() {
        SharedPreferences preferences = getSharedPreferences("AppTheme", Context.MODE_PRIVATE);
        int currentMode = preferences.getInt("mode", 0);
        return currentMode;
    }

}

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:id="@+id/linear_nightMode"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.fangqqnight.MainActivity">

    <ImageView
        android:id="@+id/mian_day"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@mipmap/yueliang1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="夜间模式" />

    <ImageView
        android:id="@+id/main_none"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="100dp"
        android:src="@mipmap/none" />


</LinearLayout>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值