Android抽獎(jiǎng)輪盤的制作方法
本文實(shí)例為大家分享了Android抽獎(jiǎng)輪盤的具體代碼,供大家參考,具體內(nèi)容如下
main布局(圖片資源請(qǐng)自行尋找,抱歉)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bigwheelgg"
/>
<ImageView
android:id="@+id/light"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/light"
/>
<ImageView
android:id="@+id/main_wheel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bigwheel"
/>
<ImageView
android:id="@+id/point"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/point"
/>
</FrameLayout>
main代碼
//設(shè)置一個(gè)時(shí)間常量,此常量有兩個(gè)作用,1.圓燈視圖顯示與隱藏中間的切換時(shí)間;2.指針轉(zhuǎn)一圈所需要的時(shí)間,現(xiàn)設(shè)置為500毫秒
private static final long ONE_WHEEL_TIME = 500;
//記錄圓燈視圖是否顯示的布爾常量
private boolean lightsOn = true;
//開(kāi)始轉(zhuǎn)動(dòng)時(shí)候的角度,初始值為0
private int startDegree = 0;
private ImageView lightIv;
private ImageView pointIv;
private ImageView wheelIv;
//指針轉(zhuǎn)圈圈數(shù)數(shù)據(jù)源
private int[] laps = { 5, 7, 10, 15 };
//指針?biāo)赶虻慕嵌葦?shù)據(jù)源,因?yàn)橛?個(gè)選項(xiàng),所有此處是6個(gè)值
private int[] angles = { 0, 60, 120, 180, 240, 300 };
//轉(zhuǎn)盤內(nèi)容數(shù)組
private String[] lotteryStr = { "索尼PSP", "10元紅包", "謝謝參與", "DNF錢包",
"OPPO MP3", "5元紅包", };
//子線程與UI線程通信的handler對(duì)象
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
if (lightsOn) {
// 設(shè)置lightIv不可見(jiàn)
lightIv.setVisibility(View.INVISIBLE);
lightsOn = false;
} else {
// 設(shè)置lightIv可見(jiàn)
lightIv.setVisibility(View.VISIBLE);
lightsOn = true;
}
break;
default:
break;
}
};
};
//監(jiān)聽(tīng)動(dòng)畫狀態(tài)的監(jiān)聽(tīng)器
private Animation.AnimationListener al = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
String name = lotteryStr[startDegree % 360 / 60];
Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
flashLights();
pointIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int lap = laps[(int) (Math.random() * 4)];
int angle = angles[(int) (Math.random() * 6)];
//每次轉(zhuǎn)圈角度增量
int increaseDegree = lap * 360 + angle;
//初始化旋轉(zhuǎn)動(dòng)畫,后面的四個(gè)參數(shù)是用來(lái)設(shè)置以自己的中心點(diǎn)為圓心轉(zhuǎn)圈
RotateAnimation rotateAnimation = new RotateAnimation(
startDegree, startDegree + increaseDegree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//將最后的角度賦值給startDegree作為下次轉(zhuǎn)圈的初始角度
startDegree += increaseDegree;
//計(jì)算動(dòng)畫播放總時(shí)間
long time = (lap + angle / 360) * ONE_WHEEL_TIME;
//設(shè)置動(dòng)畫播放時(shí)間
rotateAnimation.setDuration(time);
//設(shè)置動(dòng)畫播放完后,停留在最后一幀畫面上
rotateAnimation.setFillAfter(true);
//設(shè)置動(dòng)畫的加速行為,是先加速后減速
rotateAnimation.setInterpolator(MainActivity.this,
android.R.anim.accelerate_decelerate_interpolator);
//設(shè)置動(dòng)畫的監(jiān)聽(tīng)器
rotateAnimation.setAnimationListener(al);
//開(kāi)始播放動(dòng)畫
pointIv.startAnimation(rotateAnimation);
}
});
}
private void setupViews(){
lightIv = (ImageView) findViewById(R.id.light);
pointIv = (ImageView) findViewById(R.id.point);
wheelIv = (ImageView) findViewById(R.id.main_wheel);
}
//控制燈圈動(dòng)畫的方法
private void flashLights() {
Timer timer = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
// 向UI線程發(fā)送消息
mHandler.sendEmptyMessage(0);
}
};
// 每隔ONE_WHEEL_TIME毫秒運(yùn)行tt對(duì)象的run方法
timer.schedule(tt, 0, ONE_WHEEL_TIME);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 實(shí)現(xiàn)九宮格抽獎(jiǎng)功能
- Android自定義view制作抽獎(jiǎng)轉(zhuǎn)盤
- Android自定義View實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤
- Android自定義View實(shí)現(xiàn)QQ運(yùn)動(dòng)積分轉(zhuǎn)盤抽獎(jiǎng)功能
- Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤
- Android打造流暢九宮格抽獎(jiǎng)活動(dòng)效果
- Android中利用SurfaceView制作抽獎(jiǎng)轉(zhuǎn)盤的全流程攻略
- Android App中實(shí)現(xiàn)簡(jiǎn)單的刮刮卡抽獎(jiǎng)效果的實(shí)例詳解
- Android簡(jiǎn)單實(shí)現(xiàn)圓盤抽獎(jiǎng)界面
- Android實(shí)現(xiàn)九宮格抽獎(jiǎng)
相關(guān)文章
Android Lock鎖實(shí)現(xiàn)原理詳細(xì)分析
這篇文章主要介紹了Android Lock鎖實(shí)現(xiàn)原理,Lock接口的實(shí)現(xiàn)類提供了比使用synchronized關(guān)鍵字更加靈活和廣泛的鎖定對(duì)象操作,而且是以面向?qū)ο蟮姆绞竭M(jìn)行對(duì)象加鎖2023-02-02
從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制
這篇文章主要介紹了從源代碼分析Android Universal ImageLoader的緩存處理機(jī)制 的相關(guān)資料,需要的朋友可以參考下2016-01-01
Mac中配置gradle環(huán)境及使用android studio打包jar包與arr包的方法
這篇文章主要給大家介紹了關(guān)于在Mac中配置gradle環(huán)境,以及使用android studio打包jar包與arr包的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
Android WebView實(shí)現(xiàn)頂部進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android WebView實(shí)現(xiàn)頂部進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android Studio導(dǎo)入Project與Module的方法及實(shí)例
這篇文章主要介紹了Android Studio導(dǎo)入Project與Module的方法及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法
本篇文章主要介紹了Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
android開(kāi)發(fā)教程之用命令啟動(dòng)android模擬器并設(shè)置其內(nèi)存大小
用命令啟動(dòng)android模擬器并設(shè)置其內(nèi)存大小的方法,,需要的朋友可以參考下2014-02-02
Android使用ListView實(shí)現(xiàn)滾輪的動(dòng)畫效果實(shí)例
這篇文章主要介紹了Android使用ListView實(shí)現(xiàn)滾輪的動(dòng)畫效果實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

