Java tutorial
/* * Copyright 2012 The Android Open Source Project * * 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 com.example.fragment; import java.util.List; import com.example.config.Constants; import com.example.config.Factory; import com.example.factory.AnimeFactory; import com.example.factory.AnimeItem; import com.example.testfragment.MainActivity; import com.example.testfragment.R; import android.support.v4.app.Fragment; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.view.animation.Animation.AnimationListener; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView.OnEditorActionListener; import android.widget.RelativeLayout; /** * A fragment representing a single step in a wizard. The fragment shows a dummy title indicating * the page number, along with some dummy text. * * <p>This class is used by the {@link CardFlipActivity} and {@link * ScreenSlideActivity} samples.</p> */ public class PrimitiveFragment extends Fragment implements AnimationListener { /** * The argument key for the page number this fragment represents. */ public static final String ARG_PAGE = "page"; /** * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}. */ private int mPageNumber; ViewGroup mRootView; /** Animation <----------------------------------------------------------- */ // ??() private float mDestX; private float mSrcX; private float mDestY; private float mSrcY; private boolean mIsMoving; // ????? private boolean mIsMove; // Down?Up???????????? private boolean mIsAlive; private List<AnimeItem> mAnimeItemList = null; // ? private int mShapeWidth; // Padding:10 private float mPaddingW; // () private float mPaddingH; // () private int mImageWidth; /** Animation -----------------------------------------------------------> */ /** */ EditText mGroupTitle; /** * Factory method for this fragment class. Constructs a new fragment for the given page number. */ public static PrimitiveFragment create(int pageNumber) { PrimitiveFragment fragment = new PrimitiveFragment(); Bundle args = new Bundle(); args.putInt(ARG_PAGE, pageNumber); fragment.setArguments(args); return fragment; } public PrimitiveFragment() { //setRetainInstance(true); // ???? } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPageNumber = getArguments().getInt(ARG_PAGE); /** Animation <------------------------------------------------------- */ // ?? this.mDestX = 0.0f; this.mSrcX = 0.0f; this.mDestY = 0.0f; this.mSrcY = 0.0f; this.mIsMoving = false; this.mIsMove = false; this.mIsAlive = false; /** Animation -------------------------------------------------------> */ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** Animation <------------------------------------------------------> */ // ? this.mShapeWidth = (int) (Factory.getMaxWidthI() / 4); this.mPaddingW = (float) (this.mShapeWidth / 2); this.mPaddingH = (float) (this.mShapeWidth / 2); String padding = getString(R.dimen.padding_m); // xml???padding10???????? String[] paddingArray = padding.split("\\Q.\\E"); this.mImageWidth = this.mShapeWidth - (int) (Integer.parseInt(paddingArray[0]) * Factory.getDensity()) * 2; // this.mImageWidth = this.mShapeWidth - (int)(10 * Factory.getDensity()) * 2; /** Layout <---------------------------------------------------------> */ // Inflate the layout containing a title and body text. mRootView = (ViewGroup) inflater.inflate(R.layout.primitive_fragment, container, false); // ? FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); ((FrameLayout) mRootView.findViewById(R.id.FLBackLayer)).setLayoutParams(layoutParams); // ? FrameLayout.LayoutParams layoutParamsForFrame = new FrameLayout.LayoutParams(this.mShapeWidth, this.mShapeWidth); ((RelativeLayout) mRootView.findViewById(R.id.RLFrameLayer)).setLayoutParams(layoutParamsForFrame); // ? RelativeLayout.LayoutParams layoutParamsForImage = new RelativeLayout.LayoutParams(this.mImageWidth, this.mImageWidth); // layoutParamsForImage.setMargins(100, 300, 0, 0); ((ImageView) mRootView.findViewById(R.id.IBBackImage)).setLayoutParams(layoutParamsForImage); // ? RelativeLayout.LayoutParams layoutParamsForText = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); ((TextView) mRootView.findViewById(R.id.TVText)).setLayoutParams(layoutParamsForText); ((TextView) mRootView.findViewById(R.id.TVText)) .setText(getString(R.string.title_template_step, mPageNumber + 1)); // testAnimation((float)mPageNumber * mShapeWidth, Factory.getMaxHeightF() - mShapeWidth); /** Animation Factory <----------------------------------------------- */ AnimeFactory.setSrc(100.0f * (float) mPageNumber, 0.0f); AnimeFactory.setVelocity(2.0f * (float) (mPageNumber + 1), 0.0f); AnimeFactory.setAccelerate(0.0f, 0.80f); AnimeFactory.setBoundary(Factory.getMaxWidthF(), Factory.getMaxHeightF()); mAnimeItemList = AnimeFactory.makeAnimetion(); mIsAlive = true; move(0.0f, 0.0f); // ?? /** Animation Factory -----------------------------------------------> */ /** */ this.mGroupTitle = (EditText) mRootView.findViewById(R.id.groupTitle); this.mGroupTitle.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { refreshImage(null); } return false; } }); // 1. ?????? mGroupTitle.setFocusableInTouchMode(false); // 2. ? mGroupTitle.clearFocus(); /** Restore Latest State <-------------------------------------------- */ // ? restoreLatest(savedInstanceState); return mRootView; } /** * Returns the page number represented by this fragment object. */ public int getPageNumber() { return mPageNumber; } public void testAnimation(float destX, float destY) { this.mDestX = Factory.getAdjustedX(destX); this.mDestY = Factory.getAdjustedY(destY); // SELECT LAYER FrameLayout relate = (FrameLayout) mRootView.findViewById(R.id.FLBackLayer); // ScrollView relate = (ScrollView)mRootView.findViewById(R.id.content); // relate.setVisibility(View.VISIBLE); AnimationSet set = new AnimationSet(true); set.setAnimationListener(this); TranslateAnimation translate; float toX = this.mDestX; float fromX = this.mSrcX; float toY = this.mDestY; float fromY = this.mSrcY; translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, fromX, Animation.RELATIVE_TO_PARENT, toX, Animation.RELATIVE_TO_PARENT, fromY, Animation.RELATIVE_TO_PARENT, toY); translate.setDuration(Constants.Animation.IPF_START); translate.setInterpolator(new AccelerateInterpolator()); set.addAnimation(translate); set.setFillBefore(true); // set.setFillBefore(false); // set.setFillAfter(false); set.setFillAfter(true); relate.startAnimation(set); } /** InterFace <----------------------------------------------------------- */ public void up(float destX, float destY) { if (mIsAlive) { this.mIsAlive = false; if (mIsMove) { Log.v("--Fragment--", " mIsMove = true"); setImage(getResources().getDrawable(R.drawable.ss_c_0002), R.drawable.ss_c_0002); } else { Log.v("--Fragment--", " mIsMove = false"); EditText groupTitle = (EditText) mRootView.findViewById(R.id.groupTitle); // 3. ???? groupTitle.setFocusableInTouchMode(true); groupTitle.requestFocus(); InputMethodManager imm = (InputMethodManager) (((MainActivity) getActivity()) .getSystemService(Context.INPUT_METHOD_SERVICE)); imm.showSoftInput(groupTitle, 0); } } } @Override public void onAnimationEnd(Animation arg0) { ((MainActivity) getActivity()).tellFromFragment(getPageNumber()); // setImage(getResources().getDrawable(R.drawable.ss_c_0002)); ((TextView) mRootView.findViewById(R.id.TVText)) .setText(getString(R.string.title_template_step2, mPageNumber + 10)); // ????? this.mSrcX = this.mDestX; this.mSrcY = this.mDestY; this.mIsMoving = false; if (mAnimeItemList != null && mAnimeItemList.size() > 0) { List<AnimeItem> animeItemList = mAnimeItemList; AnimeItem item = mAnimeItemList.get(0); mIsAlive = true; move(item.getDestX(), item.getDestY()); mAnimeItemList.remove(0); } } @Override public void onAnimationRepeat(Animation arg0) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation arg0) { // TODO Auto-generated method stub } public void move(float destX, float destY) { if (!this.mIsAlive) return; if (this.mIsMoving) { return;// ADD Array } this.mDestX = Factory.getAdjustedX(destX - this.mPaddingW); this.mDestY = Factory.getAdjustedY(destY - this.mPaddingH); // SELECT LAYER FrameLayout relate = (FrameLayout) mRootView.findViewById(R.id.FLBackLayer); // ScrollView relate = (ScrollView)mRootView.findViewById(R.id.content); // relate.setVisibility(View.VISIBLE); AnimationSet set = new AnimationSet(true); set.setAnimationListener(this); TranslateAnimation translate; float toX = this.mDestX; float fromX = this.mSrcX; float toY = this.mDestY; float fromY = this.mSrcY; translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, fromX, Animation.RELATIVE_TO_PARENT, toX, Animation.RELATIVE_TO_PARENT, fromY, Animation.RELATIVE_TO_PARENT, toY); translate.setDuration(Constants.Animation.IPF); translate.setInterpolator(new AccelerateInterpolator()); set.addAnimation(translate); set.setFillBefore(true); // set.setFillBefore(false); // set.setFillAfter(false); set.setFillAfter(true); relate.startAnimation(set); this.mIsMoving = true; this.mIsMove = true; } private boolean isInMe(float destX, float destY) { if ((this.mSrcX < Factory.getAdjustedX(destX)) && (Factory.getAdjustedX(destX) < (this.mSrcX + Factory.getAdjustedX(mShapeWidth))) && (this.mSrcY < Factory.getAdjustedY(destY)) && (Factory.getAdjustedY(destY) < (this.mSrcY + Factory.getAdjustedY(mShapeWidth)))) { return true; } return false; } public void down(float destX, float destY) { this.mIsAlive = isInMe(destX, destY); if (mIsAlive) { bringToFront(); // ? setImage(getResources().getDrawable(R.drawable.ss_c_0001), R.drawable.ss_c_0001); // ??? this.mIsMove = false; } } public void bringToFront() { this.getView().bringToFront(); } /** Image <--------------------------------------------------------------- */ public void setImage(Drawable image, int resorceId) { if (mRootView == null) return; if (((ImageView) mRootView.findViewById(R.id.IBBackImage)) == null) return; ((ImageView) mRootView.findViewById(R.id.IBBackImage)).setImageDrawable(image); // Tag??? ViewCache cache = (ViewCache) ((ImageView) mRootView.findViewById(R.id.IBBackImage)).getTag(); if (cache == null) { cache = new ViewCache(); } cache.resourceId = resorceId; ((ImageView) mRootView.findViewById(R.id.IBBackImage)).setTag(cache); // ??? mRootView.invalidate(); // ??????????? // ((ImageView) mRootView.findViewById(R.id.IBBackImage)).invalidate(); } public void refreshImage(Drawable image) { // ??????????? // ((ImageView) mRootView.findViewById(R.id.IBBackImage)).setImageDrawable(image); // ((ImageView) mRootView.findViewById(R.id.IBBackImage)).invalidate(); mRootView.invalidate(); } /** InstanceState <------------------------------------------------------- */ // public Object onRetainNonConfigurationInstance() {???????Flagment????flag????? // ?? @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); ViewCache cache = (ViewCache) ((ImageView) mRootView.findViewById(R.id.IBBackImage)).getTag(); if (cache != null) { outState.putInt("resorceId", cache.resourceId); } } // ?(onCreateView??) private void restoreLatest(Bundle savedInstanceState) { if (savedInstanceState != null && !savedInstanceState.isEmpty()) { int ResourceId = savedInstanceState.getInt("resorceId", -1); if (ResourceId != -1) { setImage(getResources().getDrawable(ResourceId), ResourceId); } } } /** Inner Class */ // ??? private static class ViewCache { private long date; private String contents; private int resourceId; // private Bitmap bmp; } }