自定義ViewGroup、 動畫Animation

自定義ViewGroup

之前學過的五大布局具有侷限性,而自定義ViewGroup可以自已設計空間的佈局,據說自定義ViewGroup的運行速度比谷歌給出的佈局稍微快幾毫秒,下面給出一個很簡單的例子。

package com.example.administrator.myviewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2015/9/19.
 */
public class ViewGroupDemo extends ViewGroup {
    private int width;
    private int height;
    public ViewGroupDemo(Context context) {
        super(context);
    }

    public ViewGroupDemo(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(child1.getMeasuredWidth(),child1.getMeasuredHeight(),child1.getMeasuredWidth()+child2.getMeasuredWidth(),child1.getMeasuredWidth()+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);
        }
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.example.administrator.myviewgroup.ViewGroupDemo
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="100dp"
            android:layout_height="100dp" />
        <Button
            android:layout_width="100dp"
            android:layout_height="100dp" />
        <Button
            android:layout_width="100dp"
            android:layout_height="100dp" />
        <Button
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </com.example.administrator.myviewgroup.ViewGroupDemo>

</RelativeLayout>

動畫

Animation是一個抽象類,一張圖片通過改變它的位移(Translate)、大小(Scale)、透明度(Alpha)、旋轉(Rotate),達到動畫的效果

public class MainActivity extends Activity {
    private Button mButtonStart;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButtonStart = (Button) findViewById(R.id.button_start);
        imageView = (ImageView) findViewById(R.id.imageview);
        mButtonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlphaAnimation animation1 = new AlphaAnimation(0.0f,1.0f);
                TranslateAnimation animation2 = new TranslateAnimation(2*imageView.getMeasuredWidth(),0,0,0);
                RotateAnimation animation3= new RotateAnimation(0,360,200,0);
                ScaleAnimation animation4 = new ScaleAnimation(0.5f,1,0.5f,1);
                AnimationSet animationSet = new AnimationSet(false);
                animation1.setDuration(3000);
                animation2.setDuration(3000);
                animation3.setDuration(3000);
                animation4.setDuration(3000);
                animationSet.addAnimation(animation1);
                animationSet.addAnimation(animation2);
                animationSet.addAnimation(animation3);
                animationSet.addAnimation(animation4);
                imageView.startAnimation(animationSet);
            }
        });
    }
}

以上的效果也可以在xml文件中實現,在res中建一個anim的文件夾,在其中新建一個xml文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <rotate android:duration="3000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:toDegrees="360">
    </rotate>
    <scale android:duration="2000"
        android:fromXScale="0.5"
        android:toXScale="1.0"
        android:fromYScale="0.5"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%">
    </scale>
    <alpha android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1">
    </alpha>
</set>

然後在代碼中使用:

Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animation_rotate);
imageView.startAnimation(animation);

Value Animator

可以點擊圖片實現動畫效果。
屏蔽掉的是在代碼中的實現方法,其他的是在xml中的實現方法,同樣在res文件夾下新建一個animator的文件夾。
startImage方法名和佈局文件中 android:onClick=”startImage”/>一致,一般不推薦使用,因爲不符合MVC設計模式。

public void startImage(View view){
//  ObjectAnimator.ofFloat(imageView,"translationX",-100f,100f).setDuration(3000).start();
Animator animator = AnimatorInflater.loadAnimator(getApplicationContext(),R.animator.animator_scale);
animator.setTarget(imageView);
animator.start();
    }
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">
    <objectAnimator android:duration="2000"
        android:propertyName="scaleX"
        android:valueFrom="0.0"
        android:valueTo="1.0">
    </objectAnimator>
    <objectAnimator android:duration="2000"
        android:propertyName="scaleY"
        android:valueFrom="0.0"
        android:valueTo="1.0">
    </objectAnimator>
</set>

Layout Animation

佈局動畫,以動畫的效果在佈局中添加控件,添加的按鈕可以點擊移除。

package com.example.administrator.mylayoutanimation;

import android.animation.AnimatorInflater;
import android.animation.LayoutTransition;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends Activity {
    private Button mButtonAdd;
    private LinearLayout mLinearLayout;
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButtonAdd = (Button) findViewById(R.id.button_add);
        mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout);
        LayoutTransition transition = new LayoutTransition();
        transition.getDuration(2000);
        transition.setAnimator(LayoutTransition.APPEARING, AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator_scale));

//        transition.setAnimator(LayoutTransition.CHANGE_APPEARING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
//        transition.setAnimator(LayoutTransition.DISAPPEARING,transition.getAnimator(LayoutTransition.DISAPPEARING));
//        transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,transition.getAnimator(LayoutTransition.CHANGE_DISAPPEARING));
        mLinearLayout.setLayoutTransition(transition);
        mButtonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count++;
                Button btn = new Button(MainActivity.this);
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                btn.setLayoutParams(params);
                btn.setText("按鈕" + count);
                btn.setScaleX(0f);
                btn.setScaleY(0f);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mLinearLayout.removeView(view);
                    }
                });
                mLinearLayout.addView(btn);
            }
        });
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章