当前位置:网站首页>The use of the attribute of the use of the animation and ButterKnife
The use of the attribute of the use of the animation and ButterKnife
2022-08-04 06:04:00 【N_Y_S】
重点
1.属性动画 2.黄油刀ButterKnife
内容
什么是属性动画:Property animation is from3.0and later(如果要兼容低版本,You can use a private version of the third-party onejar NineOldAndroid.jar,The usage is similar to the usage of the system).
Constantly control the property changes of the control to achieve the effect of animation,Generally we are some combination of attribute animation to achieve complex effects.
Usage of property animation
1.在xml文件中添加控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:id="@+id/lv_root"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_1"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画一" />
<Button
android:id="@+id/btn_2"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画二" />
<Button
android:id="@+id/btn_3"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画三" />
<Button
android:id="@+id/btn_4"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="animation four" />
<Button
android:id="@+id/btn_5"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animation five" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="@mipmap/a006" />
</LinearLayout>
2.在ActivityJavaThe click event of the button is set in the file
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_1;
private Button btn_2;
private Button btn_3;
private Button btn_4;
private Button btn_5;
private ImageView img;
private LinearLayout lv_root;
int width,height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_1=findViewById(R.id.btn_1);
btn_2=findViewById(R.id.btn_2);
btn_3=findViewById(R.id.btn_3);
btn_4=findViewById(R.id.btn_4);
btn_5=findViewById(R.id.btn_5);
img=findViewById(R.id.img);
lv_root=findViewById(R.id.lv_root);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_1:
break;
case R.id.btn_2:
case R.id.btn_3:
break;
case R.id.btn_4:
break;
case R.id.btn_5:
break;
}
}
}
3.Set the moving animation of the picture,(Here is the movement from top to bottom)
switch (view.getId()){
case R.id.btn_1:
width = lv_root.getWidth();
height = lv_root.getHeight();
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, height / 4, height / 2, height / 4 * 3, height);
valueAnimator.setDuration(3000L);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int y= (Integer) valueAnimator.getAnimatedValue();
int x=width/2;
moveView(img,x,y);
}
});
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.start();
break;
4.Set the rotation and zoom animation of the image
case R.id.btn_3:
AnimatorSet scaleSet=new AnimatorSet();
ValueAnimator setAnim=ValueAnimator.ofFloat(1.0f,0.5f,1.2f,1.0f,0.6f,1.2f,1.0f);
setAnim.setDuration(2000l);
ValueAnimator ra=ValueAnimator.ofInt(0,360);
ra.setDuration(2000l);
setAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float scale= (float)valueAnimator.getAnimatedValue();
img.setScaleX(scale);
img.setScaleY(scale);
}
});
ra.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int rotate=(int)valueAnimator.getAnimatedValue();
img.setRotation(rotate);
}
});
scaleSet.playTogether(setAnim,ra);
//scaleSet.play(setAnim).after(ra);
scaleSet.start();
5.Animate the rotation gradient of the image
case R.id.btn_4:
ValueAnimator raValue=ValueAnimator.ofInt(0,360);
raValue.setDuration(1000l);
raValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int rotate= (int) valueAnimator.getAnimatedValue();
img.setRotation(rotate);
float alpha=valueAnimator.getAnimatedFraction();
img.setAlpha(alpha);
}
});
raValue.setInterpolator(new DecelerateInterpolator());
raValue.start();
break;
ButterKnife的使用
1.引入依赖
implementation 'com.jakewharton:butterknife:10.2.3'// 添加此依赖 annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'// 添加此规则
2.在java文件中使用,绑定控件
@BindView(R.id.button1)
Button button1;
@BindView(R.id.button2)
Button button2;
@BindView(R.id.button3)
Button button3;
@BindView(R.id.button4)
Button button4;
3.在orCreateStart binding in
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this);
}
4.设置点击事件
@OnClick({R.id.button1,R.id.button2,R.id.button3,R.id.button4})
public void onClick(View v){
switch (v.getId()){
case R.id.button1:
Toast.makeText(this, "按钮1", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(this, "按钮2", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "按钮3", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
Toast.makeText(this, "按钮4", Toast.LENGTH_SHORT).show();
break;
}
}
边栏推荐
猜你喜欢
随机推荐
对象存储-分布式文件系统-MinIO-1:概念
IvNWJVPMLt
flink-sql所有数据类型
sklearn中的pipeline机制
CAS与自旋锁、ABA问题
智能合约安全——溢出漏洞
剑指 Offer 2022/7/9
网络大作业心得笔记
RecyclerView的用法
读研碎碎念
剑指 Offer 2022/7/4
SQl练习 2022/6/29
(TensorFlow)——tf.variable_scope和tf.name_scope详解
【CV-Learning】卷积神经网络预备知识
NFT市场开源系统
postgresql 游标(cursor)的使用
[NSSRound#1 Basic]
线性回归02---波士顿房价预测
Postgresql 快照
(十)树的基础部分(二)