补间动画
参考网址
Android 动画:手把手教你使用 补间动画 (视图动画)
主界面的布局
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移"
android:id="@+id/btn1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"
android:id="@+id/btn2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
android:id="@+id/btn3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明"
android:id="@+id/btn4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="组合"
android:id="@+id/btn5"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/cat"
android:id="@+id/iv_cat"/>
</LinearLayout>
主界面的代码
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button btn1;
private Button btn2;
private ImageView iv_cat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_cat = findViewById(R.id.iv_cat);
}
}
平移
在res目录下新建文件夹 ,命名为anim
新建translate.xml文件 内容如下:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="400"
android:toYDelta="400"
android:duration="3000"
android:repeatCount="3"
android:repeatMode="reverse"
/>
测试平移动画
btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
translateUseJava();
}
});
/**
* 使用Java加载平移动画
*/
private void translateUseJava() {
TranslateAnimation translateAnimation = new TranslateAnimation
(100, 400, 100 ,400);
translateAnimation.setDuration(2000);
translateAnimation.setRepeatMode(Animation.REVERSE);
translateAnimation.setFillAfter(true);
translateAnimation.setRepeatCount(-1);
iv_cat.startAnimation(translateAnimation);
}
/**
* 使用xml加载动画
*/
private void translateUseXml() {
Animation animation = AnimationUtils.loadAnimation
(MainActivity.this, R.anim.translate);
iv_cat.startAnimation(animation);
}
缩放
在anim文件夹中创建scale.xml 内容如下
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:fromYScale="1"
android:toXScale="3"
android:toYScale="3"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:duration="3000"
/>
获取屏幕的宽高和密度
DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
float density = dm.density;
Log.e("DisplayMetrics", "width = " + screenWidth + " height = "+ screenHeight +
"density = " + density);
测试缩放动画
btn2 = findViewById(R.id.btn2);
btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scaleUseJava();
}
});
/**
* 使用Java加载缩放动画
*/
private void scaleUseJava() {
ScaleAnimation scaleAnimation = new ScaleAnimation(1,3,1, 3, 75,75);
scaleAnimation.setDuration(2000);
scaleAnimation.setRepeatCount(3);
iv_cat.startAnimation(scaleAnimation);
}
/**
* 使用xml加载缩放动画
*/
private void scaleUseXML() {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this,
R.anim.scale);
iv_cat.startAnimation(animation);
}