안드로이드에서 스위치 버튼을 추가하는 방법

 

이 게시물에서는 Android에서 스위치 활성화 및 비활성화 버튼을 구현하는 방법을 보여줍니다.

 

1. Android Studio에서 프로젝트를 열고 build.gradle 파일에 아래의 종속성을 추가 한 다음 프로젝트를 동기화하십시오

implementation 'com.github.zcweng:switch-button:0.0.3@aar'

2. 그런 다음 XML 파일에 아래 코드를 추가하십시오.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <com.suke.widget.SwitchButton
        android:id="@+id/sb"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginLeft="30dp"
        android:layout_margin="10dp"
        android:tint="@color/colorAccent"
        android:layout_centerHorizontal="true"
        android:paddingRight="16dp"
        android:layout_gravity="center"
        app:sb_button_color="@color/white"
        app:sb_shadow_color="#A36F95"
        app:sb_background="#FFF"
        app:sb_checkline_color="@color/green"
        app:sb_checked_color="@color/green"
        app:sb_uncheck_color="@color/red"
        app:sb_uncheckcircle_color="@color/red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

3. Java 클래스 파일에 아래 코드를 추가하십시오.

package com.jwlee.switch_test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.suke.widget.SwitchButton;

public class MainActivity extends AppCompatActivity {

    SwitchButton swithButton;

    SharedPreferences.Editor prefEditor;
    SharedPreferences prefs;

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

        swithButton = findViewById(R.id.sb);
        prefEditor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        swithButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {

                if (isChecked){

                    // DO what ever you want here when button is enabled
                    Toast.makeText(MainActivity.this, "Enabled", Toast.LENGTH_SHORT).show();
                    prefEditor.putString("checked","yes");
                    prefEditor.apply();

                }else {

                    // DO what ever you want here when button is disabled
                    Toast.makeText(MainActivity.this, "Disabled", Toast.LENGTH_SHORT).show();
                    prefEditor.putString("checked","false");
                    prefEditor.apply();
                }


            }
        });


        if (prefs.getString("checked","no").equals("yes")){

            swithButton.setChecked(true);

        }else {

            swithButton.setChecked(false);
        }
    }
}

4. color.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name ="white">#ffffff</color>
    <color name ="red">#ff0000</color>
    <color name ="green">#00ff00</color>
</resources>

5. 더 나은 이해를 위해 아래 비디오 시청

 

Android Studio를 사용하여 애니메이션 된 스플래시 화면을 Android 앱에 추가하는 방법

 

 

이 튜토리얼에서는 Android Studio를 사용하여 Animated Splash Screen을 Android 애플리케이션에 추가하는 방법을 보여줍니다. 이 스플래시 화면을 기존 Android Studio 프로젝트 또는 New Android Studio 프로젝트에 추가 할 수 있습니다.

 

구현에 필요한 코드는 여기에서 복사 할 수 있습니다. 

Splashscreen.java

package com.jwlee.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.Random;

public class Splashscreen extends Activity {
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }
    /** Called when the activity is first created. */
    Thread splashTread;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        StartAnimations();
    }
    private void StartAnimations() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
        l.clearAnimation();
        l.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.translate);
        anim.reset();
        ImageView iv = (ImageView) findViewById(R.id.splash);
        iv.clearAnimation();
        iv.startAnimation(anim);

        splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    // Splash screen pause time
                    while (waited < 3500) {
                        sleep(100);
                        waited += 100;
                    }
                    //Intent intent = new Intent(Splashscreen.this, MainActivity.class);
                    Intent intent = new Intent(Splashscreen.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);
                    Splashscreen.this.finish();
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Splashscreen.this.finish();
                }

            }
        };
        splashTread.start();

    }

}

layout/activity_splashscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#242729"
    android:layout_gravity="center"
     android:id="@+id/lin_lay"
    android:gravity="center"
    android:orientation="vertical" >
 
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/splash"
        android:background="@drawable/splash_img" />
 
</LinearLayout>

 

anim/alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="3000" />

anim/translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">
 
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%"
    android:toXDelta="0%"
    android:fromYDelta="200%"
    android:toYDelta="0%"
    android:duration="2000"
    android:zAdjustment="top" />
 
</set>

이름이 splash_img 인 이미지를 png 형식으로 복사하십시오.

이렇게하면 Animated Splashscreen을 Android 앱에 추가 할 수 있습니다.

 

 

AndroidManifest.xml  에서 splashscreen를 앞에 나오게 한다. 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jwlee.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Splashscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">

        </activity>

    </application>

</manifest>

 

우리는 보통 인간 수명이 80살 정도 된다고 생각한다. 

이제 밀레리엄세대는 120~140살까지 살수 있을까? 

수명연장은 어디까지 갈까? 

 

자연적인 인간 수명은 몇살일까? 

바이오 알카이브[Bio Rxiv]라는 잡지에서 인간수명에 따르면 

인간의 자연적인 수명은 38세이다. 

 

네안데르탈인, 데니소바인, 사람, 침팬지가 자연적 수명이 37.8~39.7세로 조사됐다. 

즉, 사람은 자연에 나두면 오래 못산다. (과학과 의학의 혜택을 보지 못하면 일찍 죽는다.)

현대 사람들은  한번이라도 아플때 약을 먹지 않거나

한번이라도 병원을 가지 않거나

한번이라도 예방주사를 맞지 않은 사람은 없을거다. 

 

건강한 삶을 유지하면서 살던 사람들이 왜 죽는걸까?

시간은 따른 나이와 상관없이 생물학적 나이가 많을수록 조기 사망률은 올라가게 된다. 

즉, 생물학적 나이가 많다는건 실제 나이보다 더 빨리 늙는것이다. (유전자가 일찍 죽게 되어 있는 경우다.)

 

 

60세의 두 남자 A와 B, 둘 다 흡연, 음주을 하는 등 생활패턴 조건은 똑같고 

A는 노화율 상위 5%안에 들고 B의 노화율은 평균치라고 했을때 

10년 뒤에 B가 사망할 확률은 60%, A가 사망할 확률은 75%이다. 

유전자에 따라 수명이 달라진다는 것이다. 

 

건강한 생활방식이 자기의 기대수명을 연장해 주는데 도움은 되겠지만 

근본적인 장수의 비결은 될 수 없다고 한다. 

하지만 흡연, 잘못돈 식습관은 아무리 유전자가 좋아도 조기 사망 확률이 높아진다. 

 

또한 가정환경에 따라서 인간수명은 영향을 받는다고도 한다. 

부모님의 사회적 지위, 집안형편 등 안정되고 좋은 가정환경에서 자란 아이들이 평균적으로 1~2년은 더 오래 살 수 있는 확률이 높이진다고 한다. 인간수명은 인명 제천이 아니고 인명 제사회라는 것이다. 이제 인간수명도 사회적 책임이 있는것이다.  최소한의 수명을 위한 사회적 장치가 마련되어야 한다. 

 

의학과 생명과학의 발전에는 사회안전망을 깔고 가야한다고 한다. 

 

 

2020년 3월 17일 코로나19 전세계 현황, 이탈리아, 스페인, 독일, 프랑스 등 유럽 대폭 증가 !! 

 

 

다우지수는 2020년 3월 12일 마이너스 9프로 빠지고 다음날 13일 플러스 9프로 다시 16일 마이너스 12프로 대폭했네요. 정말 보기드문 수치입니다. 저 수치를 합치면 몇달동안 일어날들이 3일로 압축해서 일어나네요. 

 

 

코스피는 다우보다는 점잖은데 오늘 과연 어떻게 될까요? 

 

+ Recent posts