当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢
随机推荐
自动化运维工具Ansible(6)Jinja2模板
SQL的性能分析、优化
Kubernetes基本入门-概念介绍(一)
MySql的concat和group_concat的区别
ThinkPHP5.0.x 反序列化分析
剑指 Offer 2022/7/12
Androd Day02
TensorFlow:tf.ConfigProto()与Session
Shell(3)条件控制语句
攻防世界MISC—MISCall
对象存储-分布式文件系统-MinIO-2:服务端部署
flink-sql大量使用案例
MySQL事务详解(事务隔离级别、实现、MVCC、幻读问题)
完美解决keyby造成的数据倾斜导致吞吐量降低的问题
属性动画的用法 以及ButterKnife的用法
Upload靶场搭建&&第一二关
android基础 [超级详细android存储方式解析(SharedPreferences,SQLite数据库存储)]
(十一)树--堆排序
yolov3数据读入(二)
线性回归02---波士顿房价预测








