Android Open Source - LimeLight Act To View Helper






From Project

Back to project page LimeLight.

License

The source code is released under:

GNU General Public License

If you think the Android project LimeLight listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0, as well as to the Additional Term regarding proper
 attribution. The latter is located in Term 11 of the License.
 If a copy of the MPL with the Additional Term was not distributed
 with this file, You can obtain one at http://static.fuzzhq.com/licenses/MPL
 *//*from   w w  w . j  a  v  a2  s  . c  o  m*/

package com.fuzz.android.limelight.model;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fuzz.android.limelight.LimeLight;
import com.fuzz.android.limelight.R;
import com.fuzz.android.limelight.animation.AnimationHolder;
import com.fuzz.android.limelight.util.ViewUtils;
import com.fuzz.android.limelight.widget.ManualPositionFrameLayout;

/**
 * @author Leonard Collins (Fuzz)
 */
public class ActToViewHelper {
    /**
     * <p>Responsible for converting an Act into a view that will be places on the LimeLight windows
     * next to the view it is anchored to.</p>
     *
     * @param act
     * @return
     */
    public static LinearLayout toView(Act act) {
        View rootView = LimeLight.getRootView();
        LinearLayout view = new LinearLayout(LimeLight.getActivity());
        view.setOrientation(LinearLayout.VERTICAL);
        int graphicID = act.getGraphicResID();
        if (graphicID != 0) {
            ImageView imageView = createGraphicView(view);
            imageView.setImageResource(graphicID);
        }
        if (!act.hasMessage()) {
            act.setMessage("");
        }

        TextView messageTextView = createMessageTextView(view);
        updateMessageView(messageTextView, act);
        messageTextView.setMaxWidth(450);

        setLayoutParams(act, rootView, view);
        view.setTag(act);
        view.setClipChildren(false);
        return view;
    }

    private static ImageView createGraphicView(ViewGroup parent) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        ImageView imageView = new ImageView(LimeLight.getActivity());
        imageView.setId(R.id.actImage);
        parent.addView(imageView, 0, params);
        return imageView;
    }

    private static TextView createMessageTextView(ViewGroup parent) {
        TextView textView = new TextView(LimeLight.getActivity());
        textView.setId(R.id.actText);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        textView.setPadding(7, 5, 7, 5);
        params.setMargins(0, 5, 0, 0);
        parent.addView(textView, params);
        return textView;
    }

    private static void updateMessageView(TextView textView, Act act) {
        if (act.hasTypeface()) {
            textView.setTypeface(act.getTypeface());
        }
        textView.setText(act.getMessage());
        textView.setTextColor(act.getTextColor());
        textView.setBackgroundColor(act.hasTransparentBackground() ?
                Color.TRANSPARENT :
                act.getTextBackgroundColor());

        if (act.getTextSize() == 0) {
            act.setTextSize(12);
        }
        textView.setTextSize(act.getTextSize());

    }

    protected static void setLayoutParams(Act act, View rootView, View view) {

        View anchorView = rootView.findViewById(act.getAnchorViewID());

        double widthRatio = act.getWidthRatio();
        double heightRatio = act.getHeightRatio();

        int[] xy = new int[2];
        Rect absolutePositionOnScreen;
        if (anchorView != null) {
            anchorView.getLocationOnScreen(xy);
            absolutePositionOnScreen = ModelHelper.getAbsolutePositionOnScreen(anchorView);
        } else {
            absolutePositionOnScreen = ModelHelper.getAbsolutePositionOnScreen(rootView);
            widthRatio = heightRatio = 0.5;
        }

        // START OF ALIGNMENT CODE
        int x = (int) (xy[0] + (widthRatio * absolutePositionOnScreen.width()));
        int y = (int) (xy[1] + (heightRatio * absolutePositionOnScreen.height()));

        // END OF ALIGNMENT CODE

        int statusBarHeight = 0;
        statusBarHeight += ViewUtils.getStatusBarHeight();

        ManualPositionFrameLayout.MPLayoutParams mpLayoutParams =
                new ManualPositionFrameLayout.MPLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
        mpLayoutParams.absoluteXPosition = x;
        mpLayoutParams.absoluteYPosition = y - statusBarHeight;
        view.setLayoutParams(mpLayoutParams);
    }

    public static void updateView(final View view, boolean isChangeAnimation) {
        Act act = (Act) view.getTag();
        view.setId(act.getAnchorViewID());
        ImageView imageView = (ImageView) view.findViewById(R.id.actImage);
        TextView textView = (TextView) view.findViewById(R.id.actText);

        int graphicID = act.getGraphicResID();

        if (graphicID != 0) {
            if (imageView == null) {
                imageView = createGraphicView((ViewGroup) view);
            }
            imageView.setImageResource(graphicID);
        }

        if (!act.hasMessage()) {
            act.setMessage("");
        }

        if (textView == null) {
            textView = createMessageTextView((ViewGroup) view);
        }

        updateMessageView(textView, act);

        final Animator animation = getAnimationForAct(act);
        Animation prevAnim = view.getAnimation();
        if (animation != null && isChangeAnimation) {
            if (prevAnim != null) {
                prevAnim.cancel();
            }
            try {
                animation.start();
            }catch (Throwable t){}
        } else {
            view.clearAnimation();
        }
    }

    public static Animator getAnimationForAct(Act act) {
        Animator animation = null;
        String animationString = null;
        if (act.getAnimationHolder() != null) {
            animation = act.getAnimationHolder().getAnimation();
        } else {
            act.getAnimation();
        }
        if (animationString != null) {
            int animationID = 0;
            try {
                animationID = Integer.parseInt(animationString);
            } catch (Throwable t) {
            }
            if (animationID != 0) {
                animation = AnimatorInflater.loadAnimator(LimeLight.getActivity(), animationID);
            } else {
                try {
                    Class objectClass = Class.forName(animationString);
                    Object anim = objectClass.newInstance();
                    if (anim instanceof Animator) {
                        animation = (Animator) anim;
                    } else if (anim instanceof AnimationHolder) {
                        animation = ((AnimationHolder) anim).getAnimation();
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        act.setAnimation(animation);
        return animation;
    }

    public static String getAnimationName(Act act) {
        String animationString = act.getAnimation();
        if (animationString != null) {
            try {
                Class objectClass = Class.forName(animationString);
                Object anim = objectClass.newInstance();
                if (anim instanceof AnimationHolder) {
                    animationString = ((AnimationHolder) anim).getAnimationName();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return animationString;
    }

}




Java Source Code List

com.fuzz.android.limelight.LimeLight.java
com.fuzz.android.limelight.animation.AnimationHolder.java
com.fuzz.android.limelight.animation.LeftRightAnimation.java
com.fuzz.android.limelight.animation.UpDownAnimation.java
com.fuzz.android.limelight.automate.DrawerAutomator.java
com.fuzz.android.limelight.automate.ViewAutomator.java
com.fuzz.android.limelight.model.ActToViewHelper.java
com.fuzz.android.limelight.model.Act.java
com.fuzz.android.limelight.model.BaseChapter.java
com.fuzz.android.limelight.model.Book.java
com.fuzz.android.limelight.model.ChapterTransition.java
com.fuzz.android.limelight.model.Chapter.java
com.fuzz.android.limelight.model.ModelHelper.java
com.fuzz.android.limelight.model.Text.java
com.fuzz.android.limelight.model.Transition.java
com.fuzz.android.limelight.recorder.FontListAdapter.java
com.fuzz.android.limelight.recorder.RecorderClickListener.java
com.fuzz.android.limelight.recorder.RecorderWindow.java
com.fuzz.android.limelight.recorder.Recorder.java
com.fuzz.android.limelight.recorder.widget.color.ColorHueSlider.java
com.fuzz.android.limelight.recorder.widget.color.ColorPickDialog.java
com.fuzz.android.limelight.recorder.widget.color.ColorSquareView.java
com.fuzz.android.limelight.recorder.widget.color.OnColorChangedListener.java
com.fuzz.android.limelight.recorder.widget.drag.DragAndEditView.java
com.fuzz.android.limelight.recorder.widget.drag.EditorFrameLayout.java
com.fuzz.android.limelight.recorder.widget.drag.OffSetChangeListener.java
com.fuzz.android.limelight.recorder.widget.editor.ActEditor.java
com.fuzz.android.limelight.recorder.widget.editor.ActIconAdapter.java
com.fuzz.android.limelight.recorder.widget.editor.operations.BaseOperation.java
com.fuzz.android.limelight.recorder.widget.editor.operations.ChangeAnimationOperation.java
com.fuzz.android.limelight.recorder.widget.editor.operations.ChangeBackgroundColorOperation.java
com.fuzz.android.limelight.recorder.widget.editor.operations.ChangeIconOperation.java
com.fuzz.android.limelight.recorder.widget.editor.operations.SetTextColorOperation.java
com.fuzz.android.limelight.recorder.widget.filedialog.FileDirectoryDialog.java
com.fuzz.android.limelight.recorder.widget.filedialog.OnDirectorySelectListener.java
com.fuzz.android.limelight.recorder.widget.snap.SnapHelper.java
com.fuzz.android.limelight.recorder.widget.snap.SnapInfo.java
com.fuzz.android.limelight.text.PrimeTextWatcher.java
com.fuzz.android.limelight.util.FontUtils.java
com.fuzz.android.limelight.util.IOUtils.java
com.fuzz.android.limelight.util.JSONTool.java
com.fuzz.android.limelight.util.LimeLightLog.java
com.fuzz.android.limelight.util.ViewUtils.java
com.fuzz.android.limelight.view.PrimeClickListener.java
com.fuzz.android.limelight.widget.ManualPositionFrameLayout.java