52

Androidオールスターズ2016 yanzm

Embed Size (px)

Citation preview

Page 1: Androidオールスターズ2016 yanzm
Page 2: Androidオールスターズ2016 yanzm

- -

Page 3: Androidオールスターズ2016 yanzm
Page 4: Androidオールスターズ2016 yanzm
Page 5: Androidオールスターズ2016 yanzm
Page 6: Androidオールスターズ2016 yanzm
Page 7: Androidオールスターズ2016 yanzm

private package-private

protected public

Page 8: Androidオールスターズ2016 yanzm

private String createDurationText(int duration) { int hour = duration / 3600; duration -= hour * 3600; int min = duration / 60; duration -= min * 60; int sec = duration; if (hour > 0) { return String.format(Locale.ENGLISH, "%d:%02d:%02d", hour, min, sec); } else if (min > 0) { return String.format(Locale.ENGLISH, "%02d:%02d", min, sec); } else { return String.format(Locale.ENGLISH, "0:%02d", sec); }}

x

Page 9: Androidオールスターズ2016 yanzm

static String createDurationText(int duration) { int hour = duration / 3600; duration -= hour * 3600; int min = duration / 60; duration -= min * 60; int sec = duration; if (hour > 0) { return String.format(Locale.ENGLISH, "%d:%02d:%02d", hour, min, sec); } else if (min > 0) { return String.format(Locale.ENGLISH, "%02d:%02d", min, sec); } else { return String.format(Locale.ENGLISH, "0:%02d", sec); }}

o

Page 10: Androidオールスターズ2016 yanzm

@Testpublic void durationTextTest() { assertEquals("1:24:32", MyCustomView.createDurationText(3600 + 24 * 60 + 32)); assertEquals("01:32", MyCustomView.createDurationText(60 + 32)); assertEquals("0:32", MyCustomView.createDurationText(32));}

o

Page 11: Androidオールスターズ2016 yanzm

private static String TAG = "MainActivity";x

private static final String TAG = "MainActivity";o

Page 12: Androidオールスターズ2016 yanzm

public class MyCustomView extends View { private int minHeight; public MyCustomView(Context context) { super(context); init(); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { minHeight = 10; }

x

Page 13: Androidオールスターズ2016 yanzm

public class MyCustomView extends View { private final int minHeight; public MyCustomView(Context context) { this(context, null); } public MyCustomView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); minHeight = 10; }

o

Page 14: Androidオールスターズ2016 yanzm
Page 15: Androidオールスターズ2016 yanzm

/** * Created by yanzm on 2016/08/07. */public class MainActivity extends AppCompatActivity {

x

/** * ホーム画面 * * ランチャーから起動される。 * 最初にトークンがローカルに保存されているか確認し、 * 保存されていない場合はログイン画面に遷移する。 */ public class MainActivity extends AppCompatActivity {

o

Page 16: Androidオールスターズ2016 yanzm

x

/** * 1分未満のときは 0:ss、1分以上1時間未満のときは mm:ss、 * 1時間以上のときは h:mm:ss 形式の文字列を返す * * @param duration 秒 * @return 再生時間文字列 */static String createDurationText(int duration) {

o

static String createDurationText(int duration) {

Page 17: Androidオールスターズ2016 yanzm

x

public interface MyInterface { /** * 再生時間を返す * * @param id 動画のID * @return 秒 */ int getDuration(String id);}

o

public interface MyInterface { int getDuration(String id);}

Page 18: Androidオールスターズ2016 yanzm

http://y-anz-m.blogspot.jp/2015/06/androidsupportannotation.html

Page 19: Androidオールスターズ2016 yanzm

x public interface MyInterface { /** * … */ int getDuration(String id);}

o public interface MyInterface { /** * … */ int getDuration(@NonNull String id);}

Page 20: Androidオールスターズ2016 yanzm

public class IconUtils { /** * 対応する画像リソースを返す * * @param type アイコンのタイプ * @return 画像リソース */ @DrawableRes public static int getIconResId(@NonNull IconType type) { … }}

o

Page 21: Androidオールスターズ2016 yanzm

x public interface ProfileService { Profile getProfile(String userId);}

o public interface ProfileService { @WorkerThread Profile getProfile(@NonNull String userId);}

Page 22: Androidオールスターズ2016 yanzm

o public interface ProfileService { /** * プロフィールを取得する * <p> * userId が null のときは自分のプロフィールが返る * * @param userId ユーザーのID * @return プロフィール */ @WorkerThread Profile getProfile(@Nullable Integer userId);}

Page 23: Androidオールスターズ2016 yanzm

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);

x

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);

o

Page 24: Androidオールスターズ2016 yanzm
Page 25: Androidオールスターズ2016 yanzm
Page 26: Androidオールスターズ2016 yanzm

x

o

Page 27: Androidオールスターズ2016 yanzm

x

o

Page 28: Androidオールスターズ2016 yanzm

x

o

Page 29: Androidオールスターズ2016 yanzm

x

o

Page 30: Androidオールスターズ2016 yanzm

x

o

Page 31: Androidオールスターズ2016 yanzm

x <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="Hello" /> </FrameLayout>

Page 32: Androidオールスターズ2016 yanzm

<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" tools:text="Hello" /> </FrameLayout>

o

Page 33: Androidオールスターズ2016 yanzm

<include> <merge>

Page 34: Androidオールスターズ2016 yanzm

tools:text

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="Hello" />

Page 35: Androidオールスターズ2016 yanzm

tools:layout_width tools:layout_height

<TextView android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_height="48dp" tools:text="Hello" />

Page 36: Androidオールスターズ2016 yanzm

tools:visibility

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" tools:text="Hello" tools:visibility="visible" />

Page 37: Androidオールスターズ2016 yanzm

tools:context

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.sample.MainActivity">

Page 38: Androidオールスターズ2016 yanzm

tools:layout

<fragment android:id="@+id/fragment_my" android:name="com.sample.MyFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout=“@layout/fragment_my" />

Page 39: Androidオールスターズ2016 yanzm

tools:showIn

<?xml version="1.0" encoding="utf-8"?><FrameLayout 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" tools:showIn="@layout/activity_main"> …</FrameLayout>

Page 40: Androidオールスターズ2016 yanzm
Page 41: Androidオールスターズ2016 yanzm

x abortOnError false

Page 42: Androidオールスターズ2016 yanzm

https://developer.android.com/studio/build/shrink-code.html

android {

...

buildTypes {

release {

shrinkResources true

minifyEnabled true

proguardFiles getDefaultProguardFile('proguard-android.txt'),

'proguard-rules.pro'

}

}

}

Page 43: Androidオールスターズ2016 yanzm

x

o dependencies { compile 'com.google.code.gson:gson:2.7'}

libs/gson-2.2.2.jar

Page 44: Androidオールスターズ2016 yanzm

final DisplayMetrics metrics = getResources() .getDisplayMetrics();final int widthPixels = metrics.widthPixels; final int heightPixels = metrics.heightPixels; final float density = metrics.density;

Page 45: Androidオールスターズ2016 yanzm

https://developer.android.com/reference/android/text/TextUtils.html

@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); final String userId = … if (TextUtils.isEmpty(userId)) { finish(); return; } …}

Page 46: Androidオールスターズ2016 yanzm
Page 47: Androidオールスターズ2016 yanzm

public class ProfileActivity extends AppCompatActivity { private static final String EXTRA_USER_ID = "user_id"; @NonNull public static Intent createIntent(@NonNull Context context, @NonNull String userId) { Intent intent = new Intent(context, ProfileActivity.class); intent.putExtra(EXTRA_USER_ID, userId); return intent; } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); final String userId = getIntent().getStringExtra(EXTRA_USER_ID); }}

Page 48: Androidオールスターズ2016 yanzm

public class ProfileFragment extends Fragment { private static final String ARGS_USER_ID = "user_id"; @NonNull public static ProfileFragment newInstance(@NonNull String userId) { ProfileFragment f = new ProfileFragment(); final Bundle args = new Bundle(); args.putString(ARGS_USER_ID, userId); f.setArguments(args); return f; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final String userId = getArguments() == null ? null : getArguments().getString(ARGS_USER_ID); }}

Page 49: Androidオールスターズ2016 yanzm
Page 50: Androidオールスターズ2016 yanzm

Alt + Command + lo

Page 51: Androidオールスターズ2016 yanzm
Page 52: Androidオールスターズ2016 yanzm

https://techbooster.github.io/c90/