Java tutorial
/* * Copyright 2011 YAMAZAKI Makoto<makoto1975@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.zakky.memopad; import org.zakky.memopad.BgConfigActionProvider.OnBgConfigChangedListener; import org.zakky.memopad.PenConfigActionProvider.OnPenConfigChangedListener; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; /** * ??????? */ public class PadActivity extends FragmentActivity implements CanvasListener { private static final String FG_TAG_CANVAS = "canvas"; private CanvasFragment[] mCanvases; /* * ? ActionBar ????? */ /** * ? */ // private MenuItem mPenColorMenuItem; /** * ? */ private MenuItem mBgColorMenuItem; /** * ????????????? */ private CharSequence mBgColorMenuLabelBase; /** * ???? {@code mPenColorLabels} ?????? */ private int[] mPenColorValues; /** * ???? */ private float mCurrentPenSize; /** * ????? */ private String[] mBgColorLabels; /** * ???? {@code mBgColorLabels} ?????? */ private int[] mBgColorValues; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCanvases = new CanvasFragment[2]; for (int i = 0; i < mCanvases.length; i++) { mCanvases[i] = new CanvasFragment(); } fixOrientation(); setContentView(R.layout.placeholder); final FragmentManager fm = getSupportFragmentManager(); final FragmentTransaction tx = fm.beginTransaction(); try { final Fragment old = fm.findFragmentByTag(FG_TAG_CANVAS); if (old != null) { tx.remove(old); } tx.add(R.id.container, getCurrentCanvas(), FG_TAG_CANVAS); } finally { tx.commit(); } if (MyDialogFragment.showAtStartup(this)) { new MyDialogFragment().show(fm.beginTransaction(), "dialog"); } final Resources resources = getResources(); /* * ? */ mPenColorValues = resources.getIntArray(R.array.pen_color_value_list); /* * */ mBgColorLabels = resources.getStringArray(R.array.bg_color_label_list); mBgColorValues = resources.getIntArray(R.array.bg_color_value_list); } @Override protected void onStart() { super.onStart(); refresh(); } @Override protected void onStop() { super.onStop(); // save current canvas } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.pad, menu); /* * */ MenuItem penColorMenuItem = menu.findItem(R.id.menu_pen_color); PenConfigActionProvider actionProvider = (PenConfigActionProvider) penColorMenuItem.getActionProvider(); actionProvider.setOnColorChangedListener(new OnPenConfigChangedListener() { @Override public void onColorChanged(int index) { getCurrentCanvas().setPenColorIndex(index); } @Override public void onWidthChanged(float width) { mCurrentPenSize = width; getCurrentCanvas().setPenSize(width); } }); // ????2?????? getCurrentCanvas().setNextPenColor(mPenColorValues.length); mCurrentPenSize = 20f; getCurrentCanvas().setPenSize(mCurrentPenSize); // mPenColorMenuLabelBase = mPenColorMenuItem.getTitle(); // getCurrentCanvas().applyPenColor(); /* * */ // mBgColorMenuItem = menu.findItem(R.id.menu_bg_color); // mBgColorMenuLabelBase = mBgColorMenuItem.getTitle(); // getCurrentCanvas().applyBgColor(); final MenuItem bgColorMenuItem = menu.findItem(R.id.menu_bg_color); final BgConfigActionProvider bgActionProvider = (BgConfigActionProvider) bgColorMenuItem .getActionProvider(); bgActionProvider.setOnColorChangedListener(new OnBgConfigChangedListener() { @Override public void onColorChanged(int index) { getCurrentCanvas().setBgColorIndex(index); } }); getCurrentCanvas().applyBgColor(); return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch (item.getItemId()) { case R.id.menu_swap: /* SWAP */ swapCanvas(); return true; case R.id.menu_share: /* */ shareImage(); return true; case R.id.menu_clear: /* */ clearCanvas(); return true; default: return super.onMenuItemSelected(featureId, item); } } private CanvasFragment getCurrentCanvas() { return mCanvases[0]; } private void refresh() { final CanvasFragment currentCanvas = getCurrentCanvas(); currentCanvas.applyPenColor(); currentCanvas.setPenSize(mCurrentPenSize); currentCanvas.applyBgColor(); currentCanvas.invalidate(); } private void swapCanvas() { final FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); try { tx.replace(R.id.container, mCanvases[1], FG_TAG_CANVAS); tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); final CanvasFragment prevCurrent = mCanvases[0]; mCanvases[0] = mCanvases[1]; mCanvases[1] = prevCurrent; } finally { tx.commit(); } getSupportFragmentManager().executePendingTransactions(); refresh(); } /** * {@code penColorIndex} ???????? * @param penColorIndex */ public int penColorChanged(int penColorIndex) { // if (mPenColorMenuItem != null) { // mPenColorMenuItem.setTitle(mPenColorMenuLabelBase + mPenColorLabels[penColorIndex]); // } final int argb = mPenColorValues[penColorIndex]; return argb; } /** * {@code bgColorIndex} ???????? * @param bgColorIndex */ public int bgColorChanged(int bgColorIndex) { if (mBgColorMenuItem != null) { mBgColorMenuItem.setTitle(mBgColorMenuLabelBase + mBgColorLabels[bgColorIndex]); } final int bgArgb = mBgColorValues[bgColorIndex]; return bgArgb; } /** * ??????? {@link Intent#ACTION_SEND ACTION_SEND} ? ???? */ private void shareImage() { final Uri imageFile = getCurrentCanvas().saveImageAsPng(); if (imageFile == null) { Toast.makeText(this, R.string.failed_to_save_image, Toast.LENGTH_LONG).show(); return; } final Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/png"); intent.putExtra(Intent.EXTRA_STREAM, imageFile); startActivity(intent); } private void clearCanvas() { getCurrentCanvas().clearCanvas(); } private void fixOrientation() { final Configuration config = getResources().getConfiguration(); if (config.orientation == Configuration.ORIENTATION_PORTRAIT) { // TODO SCREEN_ORIENTATION_REVERSE_PORTRAIT ? setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else { // TODO SCREEN_ORIENTATION_REVERSE_LADSCAPE ? setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } }