目的
實(shí)現(xiàn)點(diǎn)擊一個(gè)控件按照一定規(guī)律彈出其他控件
QQ圖片20190923163850.jpg
相關(guān)技術(shù)摹菠、及其使用
1关翎、以RelativeLayout為父容器巍耗,分別添加ImageButton巾表,把所需要操作的控件添加到界面上猛计,并添加對(duì)應(yīng)的id
<ImageButton
android:id="@+id/ib_e"
style="@style/ManuBtnStyle"
android:src="@drawable/e"/>
<ImageButton
android:id="@+id/ib_d"
style="@style/ManuBtnStyle"
android:src="@drawable/d"/>
<ImageButton
android:id="@+id/ib_c"
style="@style/ManuBtnStyle"
android:src="@drawable/c"/>
<ImageButton
android:id="@+id/ib_b"
style="@style/ManuBtnStyle"
android:src="@drawable/b"/>
<ImageButton
android:id="@+id/ib_a"
style="@style/ManuBtnStyle"
android:src="@drawable/a"/>
2循头、定義initView()方法绵估,判斷當(dāng)前取出的按鈕狀態(tài)(關(guān)閉或者開啟)
private void initView() {
ImageButton menu = findViewById(R.id.ib_a);
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//遍歷數(shù)組 取出每一個(gè)按鈕
for(int i = 0 ;i < resId.length; i++){
//判斷當(dāng)前所處的狀態(tài) 打開還是關(guān)閉
if(isOpen == true){
//之前是打開炎疆,現(xiàn)在需要關(guān)閉
close(i);
}else {
//之前是關(guān)閉的,現(xiàn)在需要打開
open(i);
}
}
isOpen = !isOpen;
}
});
}
3国裳、寫一個(gè)animate方法來實(shí)現(xiàn)動(dòng)畫效果
平分之后兩者之間的角度:
//計(jì)算平分之后的兩個(gè)之間的角度
double angle = (float) (Math.PI) / (resId.length + 1);
ImageButton ib = findViewById(resId[i]);
通過id獲取對(duì)應(yīng)的控件然后計(jì)算當(dāng)前控件所處的位置
//獲取id對(duì)應(yīng)控件
//計(jì)算當(dāng)前控件對(duì)應(yīng)的角度
double mangle = (i + 1) * angle;
//計(jì)算x距離
float x = (float) (Math.cos(mangle) * 400);
//計(jì)算y距離
float y = (float) (Math.sin(mangle) * 400);
通過創(chuàng)建一個(gè)AnimationSet集合來包裹移動(dòng)和旋轉(zhuǎn)動(dòng)畫形入,然后開啟動(dòng)畫
//移動(dòng)的動(dòng)畫
TranslateAnimation tAnim = new TranslateAnimation(startx,tox,starty,toy);
tAnim.setDuration(500);
tAnim.setInterpolator(interpolator);
//旋轉(zhuǎn)動(dòng)畫
RotateAnimation rAnim = new RotateAnimation(0, 360 * 3,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF
, 0.5f);
rAnim.setDuration(500);
//創(chuàng)建一個(gè)AnimationSet集合 包裹多個(gè)動(dòng)畫
AnimationSet set = new AnimationSet(false);
set.setFillAfter(true);
set.addAnimation(rAnim);
set.addAnimation(tAnim);
//開始動(dòng)畫
ib.startAnimation(set);
PS
這個(gè)小demo中,學(xué)到了當(dāng)一個(gè)控件的多個(gè)屬性相同時(shí)缝左,可以直接創(chuàng)建一個(gè)style亿遂,方法來管理這些屬性,或者實(shí)現(xiàn)相應(yīng)的功能渺杉,進(jìn)一步的控件動(dòng)畫的熟悉和掌握蛇数,其中新學(xué)到了AnimationSet方法來包裹一個(gè)控件的多個(gè)動(dòng)畫。