android實現(xiàn)簡單的活動轉(zhuǎn)盤
更新時間:2021年10月10日 11:23:56 作者:勿安歐
這篇文章主要為大家詳細介紹了android實現(xiàn)簡單的活動轉(zhuǎn)盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了android實現(xiàn)簡單活動轉(zhuǎn)盤的具體代碼,供大家參考,具體內(nèi)容如下
頁面
public class CircleTurntableActivity extends AppCompatActivity {
private Animation mStartAnimation;
private ImageView mLuckyTurntable;
private boolean isRunning;
private boolean mIsLucky = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circle_turntable);
mLuckyTurntable = (ImageView) findViewById(R.id.id_lucky_turntable);
ImageView mStartBtn = (ImageView) findViewById(R.id.id_start_btn);
mStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isRunning) {
isRunning = true;
mIsLucky = !mIsLucky;
startAnimation();
}
}
});
}
/**
* 開啟動畫
* 5秒旋轉(zhuǎn)5圈+中獎所在位置角度
*/
private void startAnimation() {
float toDegree;//結(jié)束角度(以實際轉(zhuǎn)盤圖為準計算角度)
if (mIsLucky) {
toDegree = 360 * 5 + 30f;
} else {
toDegree = 360 * 5 + 90f;
}
if (mStartAnimation != null) {
mStartAnimation.reset();
}
// 按中心點旋轉(zhuǎn) toDegree度
// 參數(shù):旋轉(zhuǎn)的開始角度、旋轉(zhuǎn)的結(jié)束角度、X軸的伸縮模式、X坐標(biāo)的伸縮值、Y軸的伸縮模式、Y坐標(biāo)的伸縮值
mStartAnimation = new RotateAnimation(0, toDegree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
mStartAnimation.setDuration(5000); // 設(shè)置旋轉(zhuǎn)時間
mStartAnimation.setRepeatCount(0); // 設(shè)置重復(fù)次數(shù)
mStartAnimation.setFillAfter(true);// 動畫執(zhí)行完后是否停留在執(zhí)行完的狀態(tài)
mStartAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); // 動畫播放的速度
mStartAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
isRunning = false;
Toast.makeText(CircleTurntableActivity.this, mIsLucky ? "精美禮品" : "謝謝參與", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mLuckyTurntable.startAnimation(mStartAnimation);
}
}
頁面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--轉(zhuǎn)盤-->
<ImageView
android:id="@+id/id_lucky_turntable"
android:layout_width="613.33px"
android:layout_height="613.33px"
android:layout_centerInParent="true"
android:src="@mipmap/lucky_turntable_bg" />
<!--指針-->
<ImageView
android:paddingBottom="40px"
android:id="@+id/id_start_btn"
android:layout_width="266.66px"
android:layout_height="266.66px"
android:layout_centerInParent="true"
android:src="@mipmap/lucky_start_icon" />
</RelativeLayout>
效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 中clipToPadding 和 clipChildren區(qū)別和作用
這篇文章主要介紹了Android 中clipToPadding 和 clipChildren區(qū)別和作用的相關(guān)資料,需要的朋友可以參考下2017-06-06
Ubuntu中為Android HAL編寫JNI方法提供JAVA訪問硬件服務(wù)接口
本文主要介紹Ubuntu中為Android硬件抽象層模塊編寫JNI方法提供Java訪問硬件服務(wù)接口,這里給大家詳細說明如何編寫 JNI方法訪問硬件接口并附示例代碼,有需要的小伙伴參考下2016-08-08
解決eclipse啟動時報錯Failed to create the Java Virtural Machine.問題的
這篇文章主要介紹了解決eclipse啟動時報Failed to create the Java Virtural Machine.問題的方法,感興趣的小伙伴們可以參考一下2016-01-01

