平移 旋转 渐变 缩放
首先是布局
<gxq.com.day09.MyView
android:id="@+id/kaoimage"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#aaaddd" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/aa">
<Button
android:id="@+id/py"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="平移" />
<Button
android:id="@+id/jb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="渐变" />
<Button
android:id="@+id/xz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="旋转" />
<Button
android:id="@+id/sf"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="缩放" />
</LinearLayout>
<LinearLayout
android:id="@+id/aa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/vvvvv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ValueAnimator" />
<Button
android:id="@+id/set"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ObjectAnimator" />
</LinearLayout>
主页的点击事件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick({R.id.py, R.id.jb, R.id.xz, R.id.sf, R.id.vvvvv, R.id.set})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.py:
AnimatorSet set1 = new AnimatorSet();
ObjectAnimator translationX = ObjectAnimator.ofFloat(kaoimage, "translationX", 0, 600);
ObjectAnimator translationY = ObjectAnimator.ofFloat(kaoimage, "translationY", 0, 900);
//translationX.start();
set1.play(translationX).with(translationY);
set1.setDuration(3000);
set1.start();
break;
case R.id.jb:
ObjectAnimator alpha = ObjectAnimator.ofFloat(kaoimage, "alpha", 1, 0,1);
alpha.setDuration(3000);
alpha.start();
break;
case R.id.xz:
ObjectAnimator rotation = ObjectAnimator.ofFloat(kaoimage, "rotation", 360, 0);
rotation.setDuration(3000);
rotation.start();
break;
case R.id.sf:
AnimatorSet set2 = new AnimatorSet();
ObjectAnimator scaleX = ObjectAnimator.ofFloat(kaoimage, "scaleX", 1, 3,1);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(kaoimage, "scaleY", 1,3, 1);
set2.play(scaleX).with(scaleY);
set2.setDuration(3000);
set2.start();
break;
case R.id.vvvvv:
break;
case R.id.set:
break;
}
}
自定义动画
public class MyView extends android.support.v7.widget.AppCompatImageView {
private int i;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
//半径
i = Math.min(width, height) / 2;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画笔
Paint paint = new Paint();
//绘制
canvas.drawCircle(i,i,i,paint);
}
}