总结三种实现ImageView以自身中心为原点旋转的方法
资源链接:加载等待–阿里云盘
三个layout文件用到的控件都是一样的,所以这里就只贴出一个
main_activty.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ImageView
android:id="@+id/img_loading"
android:layout_width="160dp"
android:layout_height="160dp"
android:src="@mipmap/loading_01"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="开启"
app:layout_constraintEnd_toEndOf="@+id/img_loading"
app:layout_constraintStart_toStartOf="@+id/img_loading"
app:layout_constraintTop_toBottomOf="@+id/img_loading" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="@+id/btn_start"
app:layout_constraintStart_toStartOf="@+id/btn_start"
app:layout_constraintTop_toBottomOf="@+id/btn_start" />
</androidx.constraintlayout.widget.ConstraintLayout>
方法一:通过动画的方式来实现(最简单的一种)
如果想进一步了解动画相关知识,建议点击下方连接跳转到官网学习 动画简介 | Android 开发者 | Android Developers
java代码 注意:此方法只有一张图片,是在布局文件中插入到IamgeView中的
public class Test01Activity extends AppCompatActivity {
private static final String TAG = "Test01Activity";
private ImageView mImgLoading;
private Button mBtnStart;
private Button mBtnStop;
private Animation mRotate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test01);
initView();
mRotate = new RotateAnimation(
0f,
360f,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
mRotate.setInterpolator(new LinearInterpolator());
mRotate.setDuration(2000);
mRotate.setRepeatCount(Animation.INFINITE);
mBtnStart.setOnClickListener(v -> {
mImgLoading.setAnimation(mRotate);
mImgLoading.startAnimation(mRotate);
});
mBtnStop.setOnClickListener(v -> {
mImgLoading.clearAnimation();
});
}
private void initView() {
mImgLoading = findViewById(R.id.img_loading);
mBtnStart = findViewById(R.id.btn_start);
mBtnStop = findViewById(R.id.btn_close);
}
}
也可以通过ObjectAnimator属性动画来实现
private ObjectAnimator mAnimator;
mAnimator = ObjectAnimator.ofFloat(mImgLoading,"rotation",0f,360f);
mAnimator.setDuration(2000);
mAnimator.setInterpolator(new LinearInterpolator());
mAnimator.setRepeatCount(ObjectAnimator.INFINITE);
mAnimator.start();
方法二:Timer+Handler
java代码
public class Test02Activity extends AppCompatActivity {
private ImageView mImgLoading;
private Button mBtnStart;
private Button mBtnStop;
private int[] mImgS = new int[] {
R.mipmap.loading_01,R.mipmap.loading_02,R.mipmap.loading_03,R.mipmap.loading_04,R.mipmap.loading_05,
R.mipmap.loading_06,R.mipmap.loading_07,R.mipmap.loading_08,R.mipmap.loading_09,R.mipmap.loading_10,
R.mipmap.loading_11,R.mipmap.loading_12
};
private int mImgStart = 0;
private Handler mHandler;
private Timer mTimer;
private TimerTask mTimerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test02);
initView();
mHandler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what == 333) {
mImgLoading.setImageResource(mImgS[mImgStart++ % 12]);
}
}
};
mBtnStart.setOnClickListener(v -> {
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(333);
}
};
mTimer.schedule(mTimerTask,0,200);
});
mBtnStop.setOnClickListener(v -> {
if (mTimerTask != null) {
mTimerTask.cancel();
mTimerTask = null;
}
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
});
}
private void initView() {
mImgLoading = findViewById(R.id.img_loading);
mBtnStart = findViewById(R.id.btn_start);
mBtnStop= findViewById(R.id.btn_stop);
}
}
方法三:Handler + Thread的的方法(不推荐)
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ImageView mImgLoading;
private Button mBtnStart,mBtnStop;
private Boolean isPause = false;
private int flag = 1;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initThread();
initHandler();
mBtnStart.setOnClickListener(v -> {
isPause = true;
});
mBtnStop.setOnClickListener(v -> {
isPause = false;
});
}
private void initThread() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (isPause) {
Message msg = Message.obtain();
msg.what = flag;
mHandler.sendMessage(msg);
if (flag == 6) {
flag = 1;
}
flag++;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
private void initHandler() {
mHandler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
mImgLoading.setImageResource(R.mipmap.loading_01);
break;
case 2:
mImgLoading.setImageResource(R.mipmap.loading_02);
break;
case 3:
mImgLoading.setImageResource(R.mipmap.loading_03);
break;
case 4:
mImgLoading.setImageResource(R.mipmap.loading_04);
break;
case 5:
mImgLoading.setImageResource(R.mipmap.loading_05);
break;
case 6:
mImgLoading.setImageResource(R.mipmap.loading_06);
break;
default:
Log.d(TAG,"default is load");
}
}
};
}
private void initView() {
mImgLoading = findViewById(R.id.img_loading);
mBtnStart = findViewById(R.id.btn_start);
mBtnStop= findViewById(R.id.btn_stop);
}
}
方法三为从别人的博客学来的,找不到原文链接了,如有冒犯,烦请私信我。
|