添加相关依赖:
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:cardview-v7:23.2.1'
主界面布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ucai.cn.myanimation.MainActivity">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"// 添加圆角
app:cardElevation="10dp"// 显示阴影
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hani_gift_1"
android:layout_gravity="center"
/>
</android.support.v7.widget.CardView>
</RelativeLayout>
MainActivity代码:
import android.animation.Animator;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
CardView cardView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
}
private void initEvent() {
cardView.setOnClickListener(this);
}
private void initView() {
cardView = (CardView) findViewById(R.id.card_view);
}
@Override
public void onClick(View v) {
startAnimation(cardView);
}
private void startAnimation(View view) {
// SDK 21以上可以使用
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
int cicular_R = view.getHeight()/2 > view.getWidth()/2 ? view.getHeight()/2 : view.getWidth()/2;
// Animator animator = ViewAnimationUtils.createCircularReveal(view,(int)view.getWidth()/2,(int)view.getHeight()/2,0,cicular_R);
Animator animator = ViewAnimationUtils.createCircularReveal(view,view.getWidth()/2,view.getHeight()/2,2*cicular_R,0);
animator.setDuration(1000);
animator.start();
}else {
Toast.makeText(this, "SDK版本过低", Toast.LENGTH_SHORT).show();
}
}
}