Android实现多次刮奖,如何用多线程处理

Amy安 2016-04-15 01:13:38

新手一枚,现在在做一个刮奖的功能 在网上找了一些实例 都不能直接用 于是拿了一个自己改

public class ErnieActivity extends Activity{

Button erniebtn;
RelativeLayout container;
TextView rCount;
public int userID = 0;
public int restCount;
public int reward = 0;
public int type = 0;
RelativeLayout rubblerBG;
RubblerShow rubblerShow;
Button getReward;
@android.support.annotation.IdRes int rubblerBGId = 10001;
@android.support.annotation.IdRes int getRewardId = 10002;
public int level;
public String sServerMsg = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.erinieview);

Log.i("ErnieActivity", "IN");

Intent intent=getIntent();
Bundle bundle=intent.getExtras();//
userID = bundle.getInt("userID");
restCount = bundle.getInt("count");

rCount = (TextView)findViewById(R.id.restCount);
rCount.setText("您还有"+restCount+"次刮奖机会");

erniebtn=(Button) findViewById(R.id.erniebtn);
container=(RelativeLayout) findViewById(R.id.container);

erniebtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

if(restCount != 0){
Log.i("onClick", "IN_S");
erniebtn.setVisibility(View.INVISIBLE);
container.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
showErnie();
}else{
Log.i("onClick", "IN_F");
Toast.makeText(getApplicationContext(), "您的次数已用完!", Toast.LENGTH_SHORT).show();
}

}
});

}


public void showErnie(){
container.removeAllViews();
level = getLevel();

//erinieShow=new ErinieShow(this, level, userID, restCount);
//container.addView(erinieShow, new LayoutParams(-2, -8));

getElement();
setElementLP();
setElementStyle();
setElement();
}

/**
* 获取奖励等级
* @return
*/
public int getLevel(){
//随机,看看几等奖
double d=Math.random()*100;
if(d<50){
return 0;
}
if(d<80){
return 3;
}
if(d<95){
return 2;
}
return 1;

}

@Override
protected void onDestroy() {
super.onDestroy();
}

private void getElement() {
rubblerBG = new RelativeLayout(this);
rubblerShow = new RubblerShow(this,handler);
getReward = new Button(this);

rubblerBG.setId(rubblerBGId);
getReward.setId(getRewardId);
rubblerBG.addView(rubblerShow);
container.addView(getReward);
container.addView(rubblerBG);
}

private void setElementLP() {
int[] resolution = PhoneUtil.getResolution(this);
RelativeLayout.LayoutParams rubblerBG_LP = new RelativeLayout.LayoutParams(
resolution[0], PhoneUtil.getFitHeight(this, 300)); //设置刮层宽高 宽为100% 高300

rubblerBG.setLayoutParams(rubblerBG_LP);
rubblerShow.setLayoutParams(rubblerBG_LP);

RelativeLayout.LayoutParams getReward_LP = new RelativeLayout.LayoutParams(-2, -8);
getReward_LP.addRule(RelativeLayout.CENTER_HORIZONTAL);
getReward_LP.addRule(RelativeLayout.BELOW, rubblerBGId);
getReward.setLayoutParams(getReward_LP);

}

private void setElementStyle() {
switch (level) {
case 0:
rubblerBG.setBackgroundResource(R.drawable.rewardlevel0);
break;
case 1:
rubblerBG.setBackgroundResource(R.drawable.rewardlevel1);
this.reward = 50;
this.type = 1;
break;
case 2:
rubblerBG.setBackgroundResource(R.drawable.rewardlevel2);
this.reward = 20;
this.type = 1;
break;
default:
rubblerBG.setBackgroundResource(R.drawable.rewardlevel3);
this.reward = 10;
this.type = 1;
break;
}
// getReward.setBackgroundResource(R.drawable.get_award);
}

private void setElement() {
rubblerShow.beginRubbler(Color.parseColor("#d3d3d3"), 60, 20); //待刮蒙版颜色及刮动时的宽度

getReward.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(level==0){
Toast.makeText( getApplicationContext(), "很遗憾,此次未中奖,再接再厉吧!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "恭喜您,获得了"+level+"等奖。", Toast.LENGTH_SHORT).show();
}

String uID = userID+"";
String res = restCount+"";
String ty = type+"";
Log.i("doScratch1", uID);
Log.i("doScratch2", res);
Log.i("doScratch3", ty);


//点击领奖 更新剩余次数
// TODO Auto-generated method stub
Message msg = handler.obtainMessage();
if(v.getId() == getRewardId){
Log.i("getRewardId", "IN");
restCount--;
rCount.setText("您还有"+restCount+"次刮奖机会");
//msg.what = 1;

// handler.sendMessage(msg);
}
erniebtn.setVisibility(View.VISIBLE);
container.setVisibility(View.INVISIBLE);

doScratch(userID,reward,type);//执行发奖

}
});
//先设置为不可点击
getReward.setClickable(false);

getReward.setText("领奖");
}

public void doScratch(int userID,int reward,int type){

//showErnie();
Log.i("doScratch", "IN");
String sreward = this.reward+"";
Log.i("reward", sreward);
//这里执行发奖

}

Handler handler=new Handler(){

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==200){
getReward.setClickable(true);
}else if(msg.what==1){
//Log.i("Handler", "IN");
//rCount.setText(restCount+"");
}

}
};
}




erinieview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/restCount"
/>
<Button
android:id="@+id/erniebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="点击开始刮奖"

/>

<RelativeLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
>

</RelativeLayout>

</RelativeLayout>


这个代码没有报错 但是是有潜在问题的 如果
点击 点击开始刮奖 -》刮动蒙版图层 -》点击 领奖 这一流程的操作手速比较快 app就崩了未响应了
而且接下来还要进行发奖
由于是webapp 目前能想到的就是http请求 或 与h5页面交互 具体现在还不知道怎么做 想请教一下各位大神
另外这一块要怎样改可以解决这个潜在问题,没什么Android开发经验 实在不明白内存泄露 多线程什么的...
求教
...全文
171 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Amy安 2016-04-15
  • 打赏
  • 举报
回复
引用 1 楼 小雨_的回复:
两种方法,一种设置个boolean类型的数据,点击后设成false,全部操作完了后再改成true;第二种就是与服务器交互的时候,不让用户点击,操作结束后再回复点击事件
这样的确回避掉了问题 强制使得用户操作速度慢 但是我看一些app里刮奖的活动都是很连贯的 长远考虑 以后需求增多了 一直保持这样的感觉也不是很好 总之谢谢了,发奖new线程处理也自己摸索出来了
AImmorta1 2016-04-15
  • 打赏
  • 举报
回复
两种方法,一种设置个boolean类型的数据,点击后设成false,全部操作完了后再改成true;第二种就是与服务器交互的时候,不让用户点击,操作结束后再回复点击事件

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧