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.watasan.infospider.fragment; import java.util.List; import com.watasan.infospider.R; import com.watasan.infospider.config.Constants; import com.watasan.infospider.config.Factory; import com.watasan.infospider.factory.ShowTweetFactory; import com.watasan.infospider.factory.AnimeFactory; import com.watasan.infospider.factory.AnimeItem; import com.watasan.infospider.ignorecertapi.CApiParams; import com.watasan.infospider.item.ImageUtil; import com.watasan.infospider.item.ItemBase; import com.watasan.infospider.item.ItemIds; import com.watasan.infospider.item.ItemUserTimeline; import com.watasan.infospider.manager.ItemListManager; import com.watasan.infospider.MainActivity; import android.support.v4.app.Fragment; import android.content.Context; import android.graphics.Bitmap; 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.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; 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; /** UserId */ String userId = null; /** * 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); /** */ 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(); return mRootView; } /** * Returns the page number represented by this fragment object. */ public int getPageNumber() { return mPageNumber; } /** SCRIPT ACTION AREA <-------------------------------------------------- */ public void update(int status) { switch (status) { case 0: // testAnimation((float)mPageNumber * mShapeWidth, Factory.getMaxHeightF() - mShapeWidth); /** Animation Factory <------------------------------------------- */ float delta = Factory.getMaxWidthF() / 4; AnimeFactory.setSrc(delta * (float) (mPageNumber % 4) + mPaddingW, 0.0f + mPaddingH); AnimeFactory.setVelocity(2.0f * (float) (mPageNumber + 1), 0.1f * (float) (mPageNumber + 1)); AnimeFactory.setAccelerate(0.0f, 0.80f); AnimeFactory.setBoundaryRightBottom(Factory.getMaxWidthF() - mPaddingW, Factory.getMaxHeightF() - mPaddingH); AnimeFactory.setBoundaryLeftTop(mPaddingW, mPaddingH); AnimeFactory.setFinalDest(delta * (float) (mPageNumber % 4) + mPaddingW, Factory.getMaxHeightF() - (delta * (float) (mPageNumber / 4) + mPaddingH)); mAnimeItemList = AnimeFactory.makeAnimetion(); mIsAlive = true; move(delta * (float) (mPageNumber % 4) + mPaddingW, 0.0f + mPaddingH);// ?? /** Animation Factory -------------------------------------------> */ break; default: } } /** SCRIPT ACTION AREA --------------------------------------------------> */ 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)); } 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); } else { // this.mIsAlive = false; } } @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)); if (userId != null) { ShowTweetFactory.getUserTimeline(userId); } // ??? this.mIsMove = false; } } public void bringToFront() { this.getView().bringToFront(); } /** Image <--------------------------------------------------------------- */ private boolean isDrawable() { if (mRootView == null) return false; if (((ImageView) mRootView.findViewById(R.id.IBBackImage)) == null) return false; return true; } public void setImage(Drawable image) { if (!isDrawable()) return; // ?? ((ImageView) mRootView.findViewById(R.id.IBBackImage)).setImageDrawable(image); // ??? mRootView.invalidate(); // ??????????? // ((ImageView) mRootView.findViewById(R.id.IBBackImage)).invalidate(); } public void refreshImage(Drawable image) { // ??????????? mRootView.invalidate(); } public void setImage(Bitmap bitmap) { if (!isDrawable()) return; // ?? ((ImageView) mRootView.findViewById(R.id.IBBackImage)).setImageBitmap(bitmap); // ??? mRootView.invalidate(); // ??????????? } public void drawProfileImage(CApiParams params) { // UserId?? userId = params.getScreenName(); // ?Ids?? ItemBase item = ItemListManager.getItem(params.getScreenName()); if (item == null) return; byte[] data = null; data = ((ItemIds) item).getProfileImageData(); if (data != null) { Log.v("--image--", "data= " + params.getImageIndex() + " type= " + params.getApiType()); int width = ((ItemIds) item).getProfileImageDataWidth(); int height = ((ItemIds) item).getProfileImageDataHeight(); Bitmap bitmap = null; if (width == 0 && height == 0) { bitmap = ImageUtil.createBitmapFromByteArrayOfResource(data); } else { bitmap = ImageUtil.createBitmapFromByteArrayOfBitmap(data, width, height); } setImage(bitmap.copy(Bitmap.Config.ARGB_4444, true)); // ? data = null; if (bitmap.isRecycled()) bitmap.recycle(); bitmap = null; } else { Log.v("------- Image index", "NULL data--index =" + params.getImageIndex()); } } /** Image ---------------------------------------------------------------> */ }