当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

TensorFlow2 study notes: 4. The first neural network model, iris classification

【深度学习21天学习挑战赛】1、我的手写被模型成功识别——CNN实现mnist手写数字识别模型学习笔记

【CV-Learning】目标检测&实例分割

【go语言入门笔记】13、 结构体(struct)

【CV-Learning】卷积神经网络

剑指 Offer 2022/7/3

TensorFlow2学习笔记:8、tf.keras实现线性回归,Income数据集:受教育年限与收入数据集

攻防世界MISC—MISCall

【深度学习21天学习挑战赛】3、使用自制数据集——卷积神经网络(CNN)天气识别

flink on yarn指定第三方jar包
随机推荐
线性回归02---波士顿房价预测
VScode配置PHP环境
剑指 Offer 2022/7/2
ISCC2021———MISC部分复现(练武)
PHP课堂笔记(一)
剑指 Offer 2022/7/3
thymeleaf中 th:href使用笔记
Th in thymeleaf: href use notes
ISCC2021——web部分
(十二)树--哈夫曼树
flink onTimer定时器实现定时需求
线性回归简介01---API使用案例
Matplotlib中的fill_between;np.argsort()函数
flink-sql自定义函数
iptables防火墙
TensorFlow2学习笔记:7、优化器
两个APP进行AIDL通信
SQL练习 2022/7/3
(十四)平衡二叉树
pgsql函数中的return类型