Android自定義viewGroup實現(xiàn)點擊動畫效果
更新時間:2017年12月12日 14:44:15 作者:大城小小愛
這篇文章主要介紹了Android自定義viewGroup實現(xiàn)點擊動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了viewGroup實現(xiàn)點擊動畫效果展示的具體代碼,供大家參考,具體內(nèi)容如下
public class MyCustomView extends ViewGroup implements View.OnClickListener {
private OnMenuItemClickListener mMenuItemClickListener;
/**
* 點擊子菜單項的回調(diào)接口
*/
public interface OnMenuItemClickListener {
void onClick(View view, int pos);
}
public void setOnMenuItemClickListener(
OnMenuItemClickListener mMenuItemClickListener) {
this.mMenuItemClickListener = mMenuItemClickListener;
}
public enum Status {
OPEN, CLOSE
}
private int mRadius;
/**
* 菜單的狀態(tài)
*/
private Status mCurrentStatus = Status.CLOSE;
/**
* 菜單的主按鈕
*/
private View mCButton;
public MyCustomView(Context context) {//通過new對象來調(diào)用
this(context, null);
Log.i("jj", "super(context)");
}
public MyCustomView(Context context, AttributeSet attrs) {//在布局中使用時調(diào)用
this(context, attrs, 0);
Log.i("jj", "super(context, attrs)");
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.i("jj", "super(context, attrs, defStyleAttr)");
mRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
Log.i("jj", "mRadius1: " + mRadius);
// 獲取自定義屬性的值
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, 0);
mRadius = (int) a.getDimension(R.styleable.MyCustomView_radius, TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100,
getResources().getDisplayMetrics()));
Log.i("jj", "mRadius: " + mRadius);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
// 測量child
measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed) {
layoutCButton();
int count = getChildCount();
for (int i = 0; i < count - 1; i++) {
View child = getChildAt(i + 1);
child.setVisibility(View.GONE);
int cl = (int) (mRadius * Math.sin(Math.PI / 2 / (count - 2)
* i));
int ct = (int) (mRadius * Math.cos(Math.PI / 2 / (count - 2)
* i));
int cWidth = child.getMeasuredWidth();
int cHeight = child.getMeasuredHeight();
ct = getMeasuredHeight() - cHeight - ct;
cl = getMeasuredWidth() - cWidth - cl;
child.layout(cl, ct, cl + cWidth, ct + cHeight);
}
}
}
private void layoutCButton() {
mCButton = getChildAt(0);
mCButton.setOnClickListener(this);
int width = mCButton.getMeasuredWidth();
int height = mCButton.getMeasuredHeight();
int l = getMeasuredWidth() - width;
int t = getMeasuredHeight() - height;
mCButton.layout(l, t, l + width, t + width);
}
@Override
public void onClick(View v) {
rotateCButton(v, 0f, 360f, 300);
toggleMenu(300);
}
private void rotateCButton(View v, float start, float end, int duration) {
RotateAnimation anim = new RotateAnimation(start, end,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setDuration(duration);
anim.setFillAfter(true);
v.startAnimation(anim);
}
/**
* 切換菜單
*/
public void toggleMenu(int duration) {
// 為menuItem添加平移動畫和旋轉(zhuǎn)動畫
int count = getChildCount();
for (int i = 0; i < count - 1; i++) {
final View childView = getChildAt(i + 1);
childView.setVisibility(View.VISIBLE);
int cl = (int) (mRadius * Math.sin(Math.PI / 2 / (count - 2) * i));
int ct = (int) (mRadius * Math.cos(Math.PI / 2 / (count - 2) * i));
AnimationSet animset = new AnimationSet(true);
Animation tranAnim = null;
int xflag = 1;
int yflag = 1;
// to open
if (mCurrentStatus == Status.CLOSE) {
tranAnim = new TranslateAnimation(xflag * cl, 0, yflag * ct, 0);
childView.setClickable(true);
childView.setFocusable(true);
} else
// to close
{
tranAnim = new TranslateAnimation(0, xflag * cl, 0, yflag * ct);
childView.setClickable(false);
childView.setFocusable(false);
}
tranAnim.setFillAfter(true);
tranAnim.setDuration(duration);
tranAnim.setStartOffset((i * 100) / count);
tranAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (mCurrentStatus == Status.CLOSE) {
childView.setVisibility(View.GONE);
}
}
});
// 旋轉(zhuǎn)動畫
RotateAnimation rotateAnim = new RotateAnimation(0, 720,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(duration);
rotateAnim.setFillAfter(true);
animset.addAnimation(rotateAnim);
animset.addAnimation(tranAnim);
childView.startAnimation(animset);
final int pos = i + 1;
childView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mMenuItemClickListener != null)
mMenuItemClickListener.onClick(childView, pos);
menuItemAnim(pos - 1);
changeStatus();
}
});
}
// 切換菜單狀態(tài)
changeStatus();
}
/**
* 添加menuItem的點擊動畫
*
* @param
*/
private void menuItemAnim(int pos) {
for (int i = 0; i < getChildCount() - 1; i++) {
View childView = getChildAt(i + 1);
if (i == pos) {
childView.startAnimation(scaleBigAnim(300));
} else {
childView.startAnimation(scaleSmallAnim(300));
}
childView.setClickable(false);
childView.setFocusable(false);
}
}
private Animation scaleSmallAnim(int duration) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.0f);
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(alphaAnim);
animationSet.setDuration(duration);
animationSet.setFillAfter(true);
return animationSet;
}
/**
* 為當前點擊的Item設置變大和透明度降低的動畫
*
* @param duration
* @return
*/
private Animation scaleBigAnim(int duration) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0.0f);
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(alphaAnim);
animationSet.setDuration(duration);
animationSet.setFillAfter(true);
return animationSet;
}
public boolean isOpen() {
return mCurrentStatus == Status.OPEN;
}
/**
* 切換菜單狀態(tài)
*/
private void changeStatus() {
mCurrentStatus = (mCurrentStatus == Status.CLOSE ? Status.OPEN
: Status.CLOSE);
}
}
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="radius" format="dimension" />
<declare-styleable name="MyCustomView">
<attr name="radius" />
</declare-styleable>
</resources>
菜單布局文件:
<com.admom.mycanvas.view.MyCustomView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hyman="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/id_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
hyman:radius="160dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/composer_button">
<ImageView
android:id="@+id/id_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/composer_icn_plus" />
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_music"
android:tag="Music" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_place"
android:tag="Place" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_sleep"
android:tag="Sleep" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_thought"
android:tag="Sun" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/composer_with"
android:tag="People" />
</com.admom.mycanvas.view.MyCustomView>
主界面布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/menu_right_bottom"/> </RelativeLayout>
在主程序直接使用:
mMenu = (MyCustomView) findViewById(R.id.id_menu);
mMenu.setOnMenuItemClickListener(new MyCustomView.OnMenuItemClickListener() {
@Override
public void onClick(View view, int pos) {
Toast.makeText(MainActivity.this, pos + ":" + view.getTag(), Toast.LENGTH_SHORT).show();
}
});
mMenu.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(mMenu.isOpen()){
mMenu.toggleMenu(300);
}
return false;
}
});
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
Android中WebView實現(xiàn)點擊超鏈接啟動QQ的方法
這篇文章主要給大家介紹了在Android中WebView如何實現(xiàn)點擊超鏈接啟動QQ的方法,文中給出了詳細的示例代碼,相信對大家的學習或者工作具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04
Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽示例
這篇文章主要介紹了Android開發(fā)之RadioGroup的簡單使用與監(jiān)聽,結(jié)合實例形式分析了Android針對RadioGroup單選按鈕簡單實用技巧,需要的朋友可以參考下2017-07-07
android listview 水平滾動和垂直滾動的小例子
android listview 水平滾動和垂直滾動的小例子,需要的朋友可以參考一下2013-05-05
Android DataBinding單向數(shù)據(jù)綁定深入探究
看了谷歌官方文章確實寫的太簡略了,甚至看完之后有很多地方還不知道怎么回事兒或者怎么用,那么接下來我將通過文章全面介紹一下DataBinding單向數(shù)據(jù)綁定2022-11-11
Android編程之ListPreference用法實例分析
這篇文章主要介紹了Android編程之ListPreference用法,結(jié)合實例形式較為詳細的分析說明了ListPreference的功能、用法及相關注意事項,需要的朋友可以參考下2015-12-12

