Android自学 – Android自定义ViewGroup界面
关于ViewGroup就不多讲了,看下面资料
动画效果
旋转动画:
/**
*设置主按钮旋转动画
*/
private void rotatemButton(View v, float start, float end, int time) {
//中心旋转动画
RotateAnimation anim = new RotateAnimation(start, end,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setDuration(time); //设置旋转的时间
anim.setFillAfter(true); //停止最后一帧的位置
v.startAnimation(anim); //开始动画
}
组合动画:
/**
*收缩子按钮动画
*/
public void shrinkMenu(int time) {
int count = getChildCount(); //获取所有的子按钮
for (int i = 0; i < count - 1; i++) //便利所有按钮
{
final View childView = getChildAt(i + 1); //获取子按钮下标
childView.setVisibility(View.VISIBLE); //显示子按钮
childView.setTag("woshi anniu");
//==================中=================
//动画开始与结束的位置x,y
int clx = (int) (mRadius * Math.sin(Math.PI / (count - 2) * i));
int cty = (int) (mRadius * Math.cos(Math.PI / (count - 2) * i));
if (i == 0) { //第一个子按钮
clx = mRadius;
cty = 0;
} else if (i == 1) { //第二个子按钮
} else if (i == 2) { //第三个子按钮
clx = 0;
cty = mRadius;
} else if (i == 3) { //第四个子按钮
clx=(int) (mRadius * Math.cos(Math.PI / (count - 2) * i));
cty=-cty;
} else if (i == 4) { //第五个子按钮
clx = - mRadius;
cty = 0;
}
//设置动画集合
AnimationSet animset = new AnimationSet(true);
Animation tranAnim = null;
//如果当前按钮菜单为关闭状态
if (mCurrentStatus == Status.CLOSE)
{ //设置打开按钮菜单动画
tranAnim = new TranslateAnimation(clx,0 , cty, 0);
childView.setClickable(true); //子按钮可以单击
childView.setFocusable(true); //子按钮可调焦
} else//如果当前按钮菜单为开启状态
{ //设置关闭按钮菜单动画
tranAnim = new TranslateAnimation(0, clx, 0, cty);
childView.setClickable(false); //子按钮不可点击
childView.setFocusable(false); //子按钮不可调焦
}
tranAnim.setDuration(time); // 动画显示的时间
tranAnim.setFillAfter(true); //停止最后一帧的位置
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);
}
}
});
// 旋转动画
RotateAnimation rotateAnim = new RotateAnimation(0, 720,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(time); //设置旋转的时间
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);
childButtonClickAnim(pos - 1); //调用字按钮单击动画方法
menuStatus(); //调用菜单状态方法
}
});
}
// 切换菜单状态
menuStatus();
}
里面有一些不用的代码 主要看组合代码部分。
完整代码见:添加链接描述
附加:
将动画效果定义在xml中。
比如在styles.xml定义style
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="mystyle" parent="android:Animation">
<!--进入时的动画-->
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<!--退出时的动画-->
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
</resources>
这里设置的是退出dialog的进退效果。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
dlg = new AlertDialog.Builder(this).create(); //创建一个alerdialog
dlg.show(); //显示
Window window = dlg.getWindow(); // 获取dig的窗体类
window.setGravity(Gravity.CENTER);//将窗体类放在中央
window.setWindowAnimations(R.style.mystyle); //给设置动画资源
window.setContentView(R.layout.dialog_layout); // 更换原有的布局文件
btn_de = window.findViewById(R.id.btn_determine);
btn_can = window.findViewById(R.id.btn_cancel);
initEvent();
}
return super.onKeyDown(keyCode, event);
}