2014년 7월 4일 금요일

Android PreferenceActivity

설정 화면을 위한 액티비티



package com.example.preference;
import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import com.example.test.R;
public class SettingsActivity extends PreferenceActivity {
    private static final String TAG = "SettingsActivity";
    private Context mContext = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        
        addPreferencesFromResource(R.xml.preference_settings);
    }
}


<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="settings" >
        <EditTextPreference
            android:dialogTitle="edittext"
            android:key="edittext_key"
            android:summary="edittext_summary"
            android:title="edittext_title" />
    </PreferenceCategory>
</PreferenceScreen>

2014년 6월 30일 월요일

Android Slide Menu (Animation)

Android Slide Menu (Animation)

View를 추가하여 애니메이션을 통해 Slide Menu를 구현한다.

SlideMenuActivity.java

package com.example.slidemneu;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import com.example.test.R;
public class SlideMenuActivity extends Activity implements AnimationListener {
    private Animation mAnimation;
    private View mSlideMenu;
    private boolean menuOut = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide_menu);
        Button btSlideUp = (Button) findViewById(R.id.bt_slide_up);
        btSlideUp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mAnimation = AnimationUtils.loadAnimation(
                        SlideMenuActivity.this, R.anim.push_left_in);
                mAnimation.setAnimationListener(SlideMenuActivity.this);
                mSlideMenu.startAnimation(mAnimation);
            }
        });
        Button btSlideOut = (Button) findViewById(R.id.bt_slide_down);
        btSlideOut.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mAnimation = AnimationUtils.loadAnimation(
                        SlideMenuActivity.this, R.anim.push_right_out);
                mAnimation.setAnimationListener(SlideMenuActivity.this);
                mSlideMenu.startAnimation(mAnimation);
            }
        });
        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mSlideMenu = inflater.inflate(R.layout.slide_menu, null);
        mSlideMenu.setVisibility(View.INVISIBLE);
        addContentView(mSlideMenu, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
    }
    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub
        menuOut = !menuOut;
        if (!menuOut) {
            mSlideMenu.setVisibility(View.INVISIBLE);
        } else {
            mSlideMenu.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
    }
}

push_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="1000"
        android:fromXDelta="100%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0" />
</set>

push_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="100%p" />
</set>

activity_slide_menu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="640" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="48"
        android:orientation="horizontal"
        android:weightSum="360" >
        <Button
            android:id="@+id/bt_slide_up"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="180"
            android:text="menu slide up" />
        <Button
            android:id="@+id/bt_slide_down"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="180"
            android:text="menu slide out" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="592"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>

slide_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:svgimageview="http://schemas.android.com/apk/res-auto"
    android:id="@+id/slide_menu_layer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    android:weightSum="640" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="592"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="menu1" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="menu2" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="menu3" />
    </LinearLayout>
</LinearLayout>










jogging

내 시간: 11:59.92


Android Path Effect (Dash)


DashPathEffect

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(3.0F);
        paint.setPathEffect(new DashPathEffect(new float[] { 10, 10 }, 0 ));

Bearing Calculation Tip


2차원 평면그래프는 
X축이 우측으로 갈수록 + 좌측으로 갈수록 -이고
Y축은 위로 갈수록 +, 아래로 갈수록 -이다

하지만 픽셀 좌표에서는
left는 2차원 평면 그래프와 같지만,
top은 반대로 위로 갈수록 마이너스 아래로 갈수록 +이다.

atan을 이용한 bearing 구하기

getBearing

private double getBearing(float curX, float curY, float tarX, float tarY) {
        
        double degree = 0;
        double radians = 0;
        
        double width = tarX-curX; 
        double height = -(tarY-curY);
        
        if(height == 0) {
            height = 1;
        }
        
        try {
            radians = Math.atan(width / height);
            degree = Math.toDegrees(radians);
            degree = (degree + 360) % 360;
            
        } catch(Exception e) {
            ;
        }
        
        return (double) degree;
    }

Android Thread Pause Technic


Android Life Cycle의 onResume(), onStop()을 통해 flag를 설정하여 thread의 시작과 정지를 설정한다.


ThreadPauseActivity

package com.example.threadtest;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import com.example.test.R;
public class ThreadPauseActivity extends Activity {
    private boolean bPressPause = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_threadtest);
        mThread.start();
    }
    Thread mThread = new Thread() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                if (!bPressPause) {
                    SystemClock.sleep(1000);
                }
            }
        }
    };
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bPressPause = true;
    }
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        bPressPause = false;
    }
}