Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated Action #64

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions actionbar/.classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
16 changes: 16 additions & 0 deletions actionbar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# built application files
*.apk
*.ap_

# files for the dex VM
*.dex

# Java class files
*.class

# generated files
bin/
gen/

# Local configuration file (sdk path, etc)
local.properties
3 changes: 3 additions & 0 deletions actionbar/default.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
android.library=true
# Project target.
target=android-8
5 changes: 3 additions & 2 deletions actionbar/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
#proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt

android.library=true
# Project target.
target=android-15
target=android-8
android.library=true
10 changes: 10 additions & 0 deletions actionbar/res/anim/rotate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:startOffset="0" />
30 changes: 30 additions & 0 deletions actionbar/res/layout/animated_actionbar_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2012 Mystic Tree Games <http://www.mystictreegames.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.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/titleLayout"
android:background="@drawable/actionbar_background" >
<ImageButton
android:id="@+id/actionbar_item"
style="@style/ActionBarItem"
android:background="#00FFFFFF"
android:layout_centerVertical="true"
android:singleLine="true"
android:visibility="visible"
/>
</RelativeLayout>
2 changes: 1 addition & 1 deletion actionbar/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

<resources>
<declare-styleable name="ActionBar">
<attr name="title" format="string" localization="suggested" />
<attr name="actionbar_title" format="string" localization="suggested" />
</declare-styleable>
</resources>
152 changes: 142 additions & 10 deletions actionbar/src/com/markupartist/android/widget/ActionBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -69,7 +71,7 @@ public ActionBar(Context context, AttributeSet attrs) {

TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ActionBar);
CharSequence title = a.getString(R.styleable.ActionBar_title);
CharSequence title = a.getString(R.styleable.ActionBar_actionbar_title);
if (title != null) {
setTitle(title);
}
Expand Down Expand Up @@ -212,6 +214,26 @@ public void removeAction(Action action) {
final Object tag = view.getTag();
if (tag instanceof Action && tag.equals(action)) {
mActionsView.removeView(view);
break;
}
}
}
}

/**
* Change the visibility of an action
* @param action
* @param visibility
*/
public void setActionVisibility(Action action, int visibility) {
int childCount = mActionsView.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = mActionsView.getChildAt(i);
if (view != null) {
final Object tag = view.getTag();
if (tag instanceof Action && tag.equals(action)) {
view.setVisibility(visibility);
break;
}
}
}
Expand All @@ -231,15 +253,7 @@ public int getActionCount() {
* @return a view
*/
private View inflateAction(Action action) {
View view = mInflater.inflate(R.layout.actionbar_item, mActionsView, false);

ImageButton labelView =
(ImageButton) view.findViewById(R.id.actionbar_item);
labelView.setImageResource(action.getDrawable());

view.setTag(action);
view.setOnClickListener(this);
return view;
return action.onInflateView(this, mInflater.inflate(action.getLayoutResId(), mActionsView, false) );
}

/**
Expand All @@ -255,6 +269,12 @@ public static class ActionList extends LinkedList<Action> {
public interface Action {
public int getDrawable();
public void performAction(View view);

/** Used to perform any special action once the {@link ActionBar} inflated our view */
public View onInflateView(ActionBar actionBar, View view);

/** The layout used to inflate a new view for the action */
public int getLayoutResId();
}

public static abstract class AbstractAction implements Action {
Expand All @@ -268,6 +288,23 @@ public AbstractAction(int drawable) {
public int getDrawable() {
return mDrawable;
}

@Override
public View onInflateView(ActionBar actionBar, View view) {
ImageButton labelView =
(ImageButton) view.findViewById(R.id.actionbar_item);
labelView.setImageResource(this.getDrawable());

view.setTag(this);
view.setOnClickListener(actionBar);

return view;
}

@Override
public int getLayoutResId() {
return R.layout.actionbar_item;
}
}

public static class IntentAction extends AbstractAction {
Expand Down Expand Up @@ -299,4 +336,99 @@ public SearchAction() {
}
}
*/

/** Interface used to define the listener of an onClickAction */
public static interface OnClickActionListener {

/** Called when an onClickAction gets fired, flag will be the same as the actions flag */
public void onClickAction(int flag);
}

/**
* Simple action that just performs an onClick when the user taps the action button
* @author Moritz "Moss" Wundke ([email protected])
*
*/
public static class OnClickAction extends AbstractAction {
private OnClickActionListener mClickListener;
private int mFlag;

public OnClickAction(Context context, OnClickActionListener clickListener, int flag, int drawable) {
super(drawable);
mClickListener = clickListener;
mFlag = flag;
}

@Override
public void performAction(View view) {
if (mClickListener != null) {
mClickListener.onClickAction(mFlag);
}
}
}

/**
* Animated action with start/stop methods.
* @author Moritz "Moss" Wundke ([email protected])
*
*/
public static class AnimatedAction extends AbstractAction {
private Animation mAnimation;
private ImageButton mAnimatedView;
private OnClickListener mClickListener;

public AnimatedAction(Context context, OnClickListener clickListener, int drawable) {
super(drawable);
initAction(context, clickListener, R.anim.rotate);
}

public AnimatedAction(Context context, OnClickListener clickListener, int drawable, int animResId) {
super(drawable);
initAction(context, clickListener, animResId);
}

private void initAction(Context context, OnClickListener clickListener, int animResId) {
mAnimation = AnimationUtils.loadAnimation(context, animResId);
mAnimation.setRepeatCount(Animation.INFINITE);
mClickListener = clickListener;
}

/** Start the infinite animation for this action */
public void startAnimation() {
if ( mAnimatedView != null ) {
mAnimatedView.startAnimation(mAnimation);
}
}

/** Stop the infinite animation for this action */
public void stopAnimation() {
if ( mAnimatedView != null ) {
mAnimatedView.clearAnimation();
}
}

@Override
public View onInflateView(ActionBar actionBar, View view) {
mAnimatedView = (ImageButton) view.findViewById(R.id.actionbar_item);
mAnimatedView.setImageResource(this.getDrawable());

view.setTag(this);
mAnimatedView.setTag(this);
mAnimatedView.setOnClickListener(actionBar);

return view;
}

@Override
public void performAction(View view) {
if (mClickListener != null) {
mClickListener.onClick(view);
}
}

@Override
public int getLayoutResId() {
return R.layout.animated_actionbar_item;
}
}
}
16 changes: 16 additions & 0 deletions actionbarexample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# built application files
*.apk
*.ap_

# files for the dex VM
*.dex

# Java class files
*.class

# generated files
bin/
gen/

# Local configuration file (sdk path, etc)
local.properties