黑白间转换

来源:互联网 发布:python 获取js跳转url 编辑:程序博客网 时间:2024/05/23 00:06

1:在values的 styles.xml中添加黑夜主题

<!--黑夜主题--><stylename="AppTheme.Black"parent="Theme.AppCompat"></style>

2:添加到res目录下anim 创建activity_out.xml

   <alphaandroid:duration="0" android:fromAlpha="1.0" android:interpolator="@android:anim/decelerate_interpolator"android:toAlpha="0.0">        </alpha>

创建activity_in.xml

<alphaandroid:duration="0"  android:fromAlpha="0.0"android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="1.0">     </alpha>

3:创建Preferences和UiUtils
Preferences

public class Preferences {    private static final    String            shared_name = "user_guide";    private static    SharedPreferences            sp;    public static String getString(Context context, String key,                                   String defaultValues) {        SharedPreferences sp = context.getSharedPreferences(shared_name,                context.MODE_PRIVATE);        return                sp.getString(key, defaultValues);    }    public static void    setString(Context context, String key, String Values) {        SharedPreferences sp = context.getSharedPreferences(shared_name,                context.MODE_PRIVATE);        sp.edit().putString(key, Values).commit();    }}

UiUtils

public class UiUtils {//获取主题    public static int    getAppTheme(Context ctx) {        String value = Preferences.getString(ctx,                "activity_theme",                "1");        switch                (Integer.valueOf(value)) {            case 1:                return  R.style.AppTheme;//白色主题            case  2:                return  R.style.AppTheme_Black;            default:                return  R.style.AppTheme;//默认白色        }    }//切换主题//当然也可以使用资源ID来进行标记    public static void switchAppTheme( Context ctx){ String value = Preferences.getString(ctx,"activity_theme","1");        switch(Integer.valueOf(value)){            case  1:                Preferences.setString(ctx,"activity_theme","2");                break;            case  2:                Preferences.setString(ctx,"activity_theme","1");                break;            default:                Preferences.setString(ctx,"activity_theme","1");                break;        }    }}

activity_in.xml中设置一个按钮

 <Button        android:onClick="click"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!" />

主Activity

public class MainActivity extends AppCompatActivity {    private int theme= 0;    @Override    protected void onCreate(Bundle savedInstanceState) {//切换主题必须放在onCreate()之前        if (savedInstanceState == null) {            theme  = UiUtils.getAppTheme(MainActivity.this);        } else {            theme  = savedInstanceState.getInt("theme");        }        setTheme(theme);        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public  void click(View v) {        UiUtils.switchAppTheme(MainActivity.this);        reload();    }    public void    reload() {        Intent intent = getIntent();        overridePendingTransition(R.anim.activity_in,                R.anim.activity_out);//进入动画                 finish();        overridePendingTransition(R.anim.activity_in,                R.anim.activity_out);               startActivity(intent);    }}