自定义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);
            }
        });
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值