ViewGroup
MyViewGroup
public class MyViewGroup extends ViewGroup {
private int width;
private int height;
public MyViewGroup(Context context) {
super(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height=getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
measureChildren(width,height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {//左、上、右、下
View child1=getChildAt(0);
View child2=getChildAt(1);
View child3=getChildAt(2);
View child4=getChildAt(3);
if(child1!=null){
child1.layout(0,0,child1.getMeasuredWidth(),child1.getMeasuredHeight());
}
if(child2!=null){
child2.layout(r-child2.getMeasuredWidth(),0,r,child2.getMeasuredHeight());
}
if(child3!=null){
child3.layout(0,b-child3.getMeasuredHeight(),child3.getMeasuredWidth(),b);
}
if(child4!=null){
child4.layout(r-child4.getMeasuredWidth(),b-child4.getMeasuredHeight(),r,b);
}
}
}
Animation(anim、animator文件都是參數配置,和styles類似)
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始動畫"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="anim開始動畫"/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/baby"
android:onClick="startAnimation"/>
</LinearLayout>
anim : res目錄下,anim文件夾,animation.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="5000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5">
</rotate>
<scale
android:duration="5000"
android:fromXScale="0.5"
android:toXScale="0.8"
android:fromYScale="0.5"
android:toYScale="0.8"
android:pivotX="50%"
android:pivotY="50%">
</scale>
</set>
MainActivity
public class MainActivity extends Activity implements View.OnClickListener{
private Button mButton;
private Button mButton2;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton= (Button) findViewById(R.id.button);
mButton2= (Button) findViewById(R.id.button2);
mImageView= (ImageView) findViewById(R.id.img);
mButton.setOnClickListener(this);
mButton2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
startAnimation();
break;
case R.id.button2:
Animation animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation);
mImageView.startAnimation(animation);
break;
}
}
public void startAnimation(View view){
//view裏包含的屬性:scaleX,scaleY,alpha,rotateX,rotateY,translationX,translationY,translationZ
//ObjectAnimator.ofFloat(mImageView,"scaleX",0.0f,1.0f).setDuration(5000).start();
Animator animator= AnimatorInflater.loadAnimator(getApplicationContext(),R.animator.animator_scale);
animator.setTarget(mImageView);
animator.start();
}
public void startAnimation(){
AnimationSet set=new AnimationSet(false);//集合
AlphaAnimation animation1=new AlphaAnimation(0.5f,1.0f);//透明度:從半透明到不透明
TranslateAnimation animation2=new TranslateAnimation(0,200,0,200);//位移:從(0,0)到(200,200)
RotateAnimation animation3=new RotateAnimation(0,360,300,300);//旋轉,從0度到360度,中心點(300,300)
ScaleAnimation animation4=new ScaleAnimation(0.5f,0.8f,0.5f,0.8f);//放大縮小:x從0.5到0.8倍,要從0.5到0.8倍
animation1.setDuration(5000);//時長5秒
animation2.setDuration(5000);
animation3.setDuration(5000);
animation4.setDuration(5000);
set.addAnimation(animation1);
set.addAnimation(animation2);
set.addAnimation(animation3);
set.addAnimation(animation4);
mImageView.startAnimation(set);
}
}
activity_viewgroup:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加按鈕"/>
<LinearLayout
android:orientation="vertical"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
animator
animator_scale.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="5000"
android:propertyName="scaleX"
android:valueFrom="1"
android:valueTo="0.5">
</objectAnimator>
<objectAnimator
android:duration="5000"
android:propertyName="scaleY"
android:valueFrom="1"
android:valueTo="0.5">
</objectAnimator>
</set>
ViewGroupActivity
public class ViewGroupActivity extends Activity {
private Button mButton;
private LinearLayout mLinearLayout;
private int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewgroup);
mButton= (Button) findViewById(R.id.buttonAdd);
mLinearLayout= (LinearLayout) findViewById(R.id.linearlayout);
LayoutTransition transition=new LayoutTransition();
transition.setDuration(5000);
transition.setAnimator(LayoutTransition.APPEARING, AnimatorInflater.loadAnimator(getApplicationContext(),R.animator.animator_scale));//自定義動畫效果
//系統效果
// transition.setAnimator(LayoutTransition.DISAPPEARING,transition.getAnimator(LayoutTransition.DISAPPEARING));
// transition.setAnimator(LayoutTransition.CHANGE_APPEARING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
// transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,transition.getAnimator(LayoutTransition.CHANGE_DISAPPEARING));
mLinearLayout.setLayoutTransition(transition);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count++;
Button button=new Button(ViewGroupActivity.this);
ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(layoutParams);
button.setText("按鈕"+count);
button.setScaleX(0f);
button.setScaleY(0f);
mLinearLayout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mLinearLayout.removeView(view);//移除按鈕
}
});
}
});
}
}
添加按鈕:
移除按鈕: