目前在很多的app都具备抽奖功能来吸引用户,让app具有趣味性和可玩性,以此来提供用户对app的使用率,还可以通过此途径来回报用户,运营上也会得到一定的丰收。比如支付宝的抽红包和抽黄金活动,都举办得很成功,吸引了一大批用户,增加用户粘性。
接下来带大家实践以下如何实现APP抽奖功能,先看下效果图。
中奖弹框
- public class PrizeWinningLayout extends RelativeLayout {
- private RelativeLayout.LayoutParams mParams;
- private static final int MP = RelativeLayout.LayoutParams.MATCH_PARENT;
- private View mWinningLayout; //获奖弹框
- private TextView mShare;
- // private LinearLayout mBack;
- private ImageView mClose;
- private TextView mTitle;
- private TextView mContent;
- private RelativeLayout mDialogBg;
- private PrizeWinningListener mListener;
- public PrizeWinningLayout(Context context) {
- super(context);
- initView(context);
- }
- private void initView(final Context context) {
- this.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
- return true;
- }
- });
- mParams = new RelativeLayout.LayoutParams(MP, MP);
- //初始化获奖弹框
- mWinningLayout = LayoutInflater.from(context).inflate(R.layout.prize_winning_layout, null);
- addView(mWinningLayout, mParams);
- mParams = new RelativeLayout.LayoutParams(PXUtils.getRealPixel(652), PXUtils.getRealPixel(874));
- mParams.topMargin = PXUtils.getRealPixel(40);
- mParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
- mDialogBg = (RelativeLayout) mWinningLayout.findViewById(R.id.prize_winning_dialog_layout);
- mDialogBg.setLayoutParams(mParams);
- mShare = (TextView) mWinningLayout.findViewById(R.id.prize_winning_dialog_share);
- mClose = (ImageView) mWinningLayout.findViewById(R.id.winning_close);
- // mBack = (LinearLayout) mWinningLayout.findViewById(R.id.prize_winning_bar_layout);
- mTitle = (TextView) mWinningLayout.findViewById(R.id.prize_winning_dialog_grade);
- mContent = (TextView) mWinningLayout.findViewById(R.id.prize_winning_dialog_tip);
- mShare.setOnClickListener(mOnClickListener);
- mClose.setOnClickListener(mOnClickListener);
- // mBack.setOnClickListener(mOnClickListener);
- }
- public void setListener(PrizeWinningListener listener) {
- mListener = listener;
- }
- /**
- * 设置中奖内容
- * @param title
- * @param content
- */
- public void setContent(String title, String content) {
- mTitle.setText(title);
- mContent.setText(content);
- }
- private OnClickListener mOnClickListener = new OnClickListener() {@Override public void onClick(View v) {
- if (v == mShare) {
- if (mListener != null) {
- mListener.share();
- }
- } else if (v == mClose) {
- if (mListener != null) {
- mListener.closeDialog();
- }
- }
- // else if (v == mBack){
- // if (mListener != null){
- // mListener.back();
- // }
- // }
- }
- };
- public interface PrizeWinningListener {
- void share();
- void closeDialog();
- // void back();
- }
- }
布局prize_winning_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#0C0B32">
- <RelativeLayout
- android:id="@+id/prize_winning_dialog_layout"
- android:layout_width="326dp"
- android:layout_height="437dp"
- android:layout_marginTop="20dp"
- android:layout_centerHorizontal="true"
- android:background="@mipmap/prize_winning_dialog">
- <TextView
- android:id="@+id/prize_winning_dialog_wel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="158dp"
- android:text="恭喜您!"
- android:paddingLeft="6dp"
- android:textColor="#ffffffff"
- android:textSize="20dp"
- android:textStyle="bold"/>
- <TextView
- android:id="@+id/prize_winning_dialog_grade"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/prize_winning_dialog_wel"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="6dp"
- android:text="获得特等奖"
- android:textColor="#FFFDD649"
- android:textSize="30dp"
- android:textStyle="bold"
- />
- <TextView
- android:id="@+id/prize_winning_dialog_tip"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/prize_winning_dialog_grade"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="2dp"
- android:gravity="center"
- android:text="价值10万元的中脉独家定制臻品钻石\n(精工品质靓丽光彩)"
- android:textColor="#ffffffff"
- android:textSize="14dp"/>
- <TextView
- android:id="@+id/prize_winning_dialog_share"
- android:layout_width="115dp"
- android:layout_height="30dp"
- android:padding="2dp"
- android:layout_below="@+id/prize_winning_dialog_tip"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="120dp"
- android:background="@mipmap/prize_winning_btn_bg"
- android:gravity="center"
- android:text="查看获奖记录"
- android:textColor="#F73532"
- android:textSize="14dp"/>
- </RelativeLayout>
- <ImageView
- android:id="@+id/winning_close"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_below="@+id/prize_winning_dialog_layout"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="27dp"
- android:src="@mipmap/prize_winning_close"/>
- </RelativeLayout>
这里主要通过xml布局弹框,然后通过接口回调来设置点击。 注意: 弹框继承RelativeLayout,需要设置setOnTouchListener方法返回true,也就是将触摸事件交给自己处理,否则有可能将触摸事件传递到上一层。
- public class PrizeNotWinningLayout extends RelativeLayout {
- private View mNotWinningLayout;//没有获奖弹框
- private RelativeLayout.LayoutParams mParams;
- private static final int MP = RelativeLayout.LayoutParams.MATCH_PARENT;
- private OnNotWinningListener mListener;
- // private LinearLayout mBack;
- private ImageView mClose;
- private TextView mGoOn;
- private RelativeLayout mDialogLayout;
- public PrizeNotWinningLayout(Context context) {
- super(context);
- initView(context);
- }
- private void initView(Context context) {
- this.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- return true;
- }
- });
- mParams = new RelativeLayout.LayoutParams(MP, MP);
- mNotWinningLayout = LayoutInflater.from(context).inflate(R.layout.prize_not_winninglayout, null);
- addView(mNotWinningLayout, mParams);
- mParams = new RelativeLayout.LayoutParams(PXUtils.getRealPixel(650), PXUtils.getRealPixel(760));
- mParams.topMargin = PXUtils.getRealPixel(160);
- mParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
- mDialogLayout = (RelativeLayout) this.findViewById(R.id.prize_not_winning_dialog_layout);
- mDialogLayout.setLayoutParams(mParams);
- // mBack = (LinearLayout) mNotWinningLayout.findViewById(R.id.prize_not_winning_bar_layout);
- mClose = (ImageView) mNotWinningLayout.findViewById(R.id.not_winning_close);
- mGoOn = (TextView) mNotWinningLayout.findViewById(R.id.prize_not_winning_dialog_share);
- // mBack.setOnClickListener(mOnClickListener);
- mClose.setOnClickListener(mOnClickListener);
- mGoOn.setOnClickListener(mOnClickListener);
- }
- public void setListener(OnNotWinningListener listener) {
- mListener = listener;
- }
- private OnClickListener mOnClickListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // if (v == mBack) {
- // if (mListener != null) {
- // mListener.back();
- // }
- // } else
- if (v == mClose) {
- if (mListener != null) {
- mListener.closeLayout();
- }
- } else if (v == mGoOn) {
- if (mListener != null) {
- mListener.goOn();
- }
- }
- }
- };
- public interface OnNotWinningListener {
- // void back();//返回
- void closeLayout();//关闭按钮
- void goOn();//继续玩
- }
- }
布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#0C0B32">
- <RelativeLayout
- android:id="@+id/prize_not_winning_dialog_layout"
- android:layout_width="325dp"
- android:layout_height="380dp"
- android:layout_marginTop="80dp"
- android:layout_centerHorizontal="true"
- android:background="@mipmap/prize_not_winning_dialog">
- <TextView
- android:id="@+id/prize_not_winning_dialog_wel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="124dp"
- android:text="没有抽中奖品哦"
- android:textColor="#ffffffff"
- android:textSize="22dp"
- android:textStyle="bold"/>
- <TextView
- android:id="@+id/prize_not_winning_dialog_tip"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/prize_not_winning_dialog_wel"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="12dp"
- android:gravity="center"
- android:text="时来运转 再试试手气"
- android:textColor="#ffffffff"
- android:textSize="12dp"/>
- <TextView
- android:id="@+id/prize_not_winning_dialog_share"
- android:layout_width="110dp"
- android:layout_height="30dp"
- android:layout_below="@+id/prize_not_winning_dialog_tip"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="140dp"
- android:background="@mipmap/prize_winning_btn_bg"
- android:gravity="center"
- android:text="继续玩"
- android:textColor="#F73532"
- android:textSize="16dp"/>
- </RelativeLayout>
- <ImageView
- android:id="@+id/not_winning_close"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_below="@+id/prize_not_winning_dialog_layout"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="27dp"
- android:src="@mipmap/prize_winning_close"/>
- </RelativeLayout>
跟中奖弹框差不多,就是布局不一样而已。中奖弹框跟没中奖弹框,读者可以根据需求来设置不一样的界面。
Activity
- public class PrizingActivity extends BaseActivity implements View.OnClickListener {
- private TextView mPrizing;
- private RelativeLayout mLayout;
- private PrizeWinningLayout mWinningLayout; //获奖弹框
- private PrizeNotWinningLayout mNotWinningLayout; //没有获奖弹框
- private RelativeLayout.LayoutParams rParams;
- private LinearLayout.LayoutParams lParams;
- private static final int WC = RelativeLayout.LayoutParams.WRAP_CONTENT;
- private static final int MP = RelativeLayout.LayoutParams.MATCH_PARENT;
- private boolean isOnBack = true;
- private TextView mRules;
- private TextView mShare;
- private Handler mHandler = new Handler();
- private int mPrizeCount = 0;
- private TextView mPrizingNum;
- private LinearLayout mLayoutBg;
- private boolean mCanPrizing = false;
- private boolean isGoOn = false;
- private LinearLayout mBack;
- @Override protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- setContentView(R.layout.activity_prizing);
- PXUtils.init(this); //需要注册
- initView();
- initListener();
- getLottery(); //查询抽奖次数
- }
- /**
- * 初始化View
- */
- private void initView() {
- mLayout = (RelativeLayout) this.findViewById(R.id.prize_home_layout);
- lParams = new LinearLayout.LayoutParams(PXUtils.getRealPixel(600), PXUtils.getRealPixel(874));
- lParams.topMargin = PXUtils.getRealPixel(80);
- lParams.gravity = Gravity.CENTER_HORIZONTAL;
- mLayoutBg = (LinearLayout) this.findViewById(R.id.prize_home_bg);
- mLayoutBg.setLayoutParams(lParams);
- lParams = new LinearLayout.LayoutParams(WC, WC);
- lParams.topMargin = PXUtils.getRealPixel(316);
- TextView mTitle = (TextView) this.findViewById(R.id.prizing_title);
- mTitle.setLayoutParams(lParams);
- mPrizing = (TextView) this.findViewById(R.id.prize_taking);
- mPrizing.setOnClickListener(this);
- mRules = (TextView) this.findViewById(R.id.prize_rules);
- mRules.setOnClickListener(this);
- mShare = (TextView) this.findViewById(R.id.prizing_share_to_friend);
- mShare.setOnClickListener(this);
- mPrizingNum = (TextView) this.findViewById(R.id.prize_num);
- mBack = (LinearLayout) this.findViewById(R.id.prize_winning_bar_layout);
- mBack.setOnClickListener(this);
- //初始化获奖弹框
- mWinningLayout = new PrizeWinningLayout(PrizingActivity.this);
- mWinningLayout.setVisibility(View.GONE);
- //初始化没有获奖的弹框
- mNotWinningLayout = new PrizeNotWinningLayout(PrizingActivity.this);
- mNotWinningLayout.setVisibility(View.GONE);
- }
- private void initListener() {
- //没有中奖弹框监听
- mNotWinningLayout.setListener(new PrizeNotWinningLayout.OnNotWinningListener() {
- // @Override
- // public void back() {
- // isOnBack = false;
- // onBackPressed();
- // }
- @Override public void closeLayout() {
- onBackPressed();
- }
- @Override public void goOn() {
- onBackPressed();
- isGoOn = true;
- }
- });
- mWinningLayout.setListener(new PrizeWinningLayout.PrizeWinningListener() {@Override public void share() {
- Intent home_intent = new Intent(PrizingActivity.this, PrizingRecordActivity.class);
- startActivity(home_intent);
- }
- @Override public void closeDialog() {
- onBackPressed();
- }
- // @Override
- // public void back() {
- // isOnBack = false;
- // onBackPressed();
- // }
- });
- }
- @Override public void onClick(View v) {
- if (v == mPrizing) {
- //中奖
- prizing(); //抽奖
- } else if (v == mRules) {
- Intent rIntent = new Intent(PrizingActivity.this, PrizingRulesActivity.class);
- startActivity(rIntent);
- } else if (v == mShare) {
- Intent home_intent = new Intent(PrizingActivity.this, PrizingRecordActivity.class);
- startActivity(home_intent);
- } else if (v == mBack) {
- onBackPressed();
- }
- }
- private void prizing() {
- if (!mCanPrizing) {
- ToastUtil.show(PrizingActivity.this, "正在查询你的抽奖次数,请稍后...");
- return;
- }
- if (mPrizeCount <= 0) {
- ToastUtil.show(PrizingActivity.this, "你的抽奖次数为0,不能抽奖");
- return;
- }
- ProgressDialogUtils.createProgressDialog(PrizingActivity.this, "加载中...", getProgressBarDrawable()).show();
- String url = Constant.BASE_MOBILE_LOTTERY + Constant.BASE_LOTTERY_MOTHOD + "?" + "userId=" + MemberDataMgr.getUserId(PrizingActivity.this);
- LogUtils.i("prizing_url==>", url);
- StringRequest stringRequest = new StringRequest(url, new Response.Listener < String > () {
- @Override public void onResponse(String response) {
- // TODO Auto-generated method stub
- try {
- Gson gson = new Gson();
- java.lang.reflect.Type type = new TypeToken < PrizingBean.PrizingLists > () {}.getType();
- final PrizingBean.PrizingLists data = gson.fromJson(response, type);
- mHandler.post(new Runnable() {@Override public void run() {
- setDate(data);
- }
- });
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- },
- new Response.ErrorListener() {@Override public void onErrorResponse(VolleyError error) {
- // TODO Auto-generated method stub
- ProgressDialogUtils.dismissProgressDialog();
- ToastUtil.show(PrizingActivity.this, "访问出现错误,请检查您的网络环境");
- }
- });
- RequestManager.getInstance(this).addRequest(stringRequest, this);
- }
- /**
- * 设置抽奖结果
- *
- * @param data
- */
- private void setDate(PrizingBean.PrizingLists data) {
- ProgressDialogUtils.dismissProgressDialog();
- if (data != null) {
- if (data.code == 0) {
- showMyDialog(data.data);
- } else {
- ToastUtil.show(PrizingActivity.this, "验证失败");
- }
- } else {
- ToastUtil.show(PrizingActivity.this, "访问出现错误,请检查您的网络环境");
- }
- }
- private void showMyDialog(PrizingBean.PrizingInfo data) {
- mPrizeCount--;
- setNum(mPrizeCount);
- if (data.level == 5) {
- //未中奖
- mNotWinningLayout.setVisibility(View.VISIBLE);
- rParams = new RelativeLayout.LayoutParams(MP, MP);
- mLayout.addView(mNotWinningLayout, rParams);
- mLayout.setBackgroundColor(0Xff0C0B32);
- mNotWinningLayout.startAnimation(AnimationUtils.loadAnimation(this, R.anim.animated_slide_bottom_in));
- } else {
- //中奖
- mWinningLayout.setContent(data.prize, data.awardName);
- mWinningLayout.setVisibility(View.VISIBLE);
- rParams = new RelativeLayout.LayoutParams(MP, MP);
- mLayout.addView(mWinningLayout, rParams);
- mLayout.setBackgroundColor(0Xff0C0B32);
- mWinningLayout.startAnimation(AnimationUtils.loadAnimation(this, R.anim.animated_slide_bottom_in));
- }
- }
- private Drawable getProgressBarDrawable() {
- int color = getResources().getColor(R.color.theme_color);
- return new CircularProgressDrawable.Builder(this).color(color).build();
- }
- /**
- * 查询抽奖次数
- */
- public void getLottery() {
- String url = Constant.BASE_MOBILE_LOTTERY + Constant.BASE_LOTTERY_COUNT + "?" + "userId=" + MemberDataMgr.getUserId(PrizingActivity.this);
- LogUtils.i("prizing_url==>", url);
- StringRequest stringRequest = new StringRequest(url, new Response.Listener < String > () {
- @Override public void onResponse(String response) {
- // TODO Auto-generated method stub
- try {
- Gson gson = new Gson();
- java.lang.reflect.Type type = new TypeToken < PrizingBean.PrizingCount > () {}.getType();
- final PrizingBean.PrizingCount data = gson.fromJson(response, type);
- mHandler.post(new Runnable() {@Override public void run() {
- setCount(data);
- }
- });
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- },
- new Response.ErrorListener() {@Override public void onErrorResponse(VolleyError error) {
- // TODO Auto-generated method stub
- mCanPrizing = true;
- ProgressDialogUtils.dismissProgressDialog();
- ToastUtil.show(PrizingActivity.this, "访问出现错误,请检查您的网络环境");
- }
- });
- RequestManager.getInstance(this).addRequest(stringRequest, this);
- }
- /**
- * 设置查询次数
- *
- * @param count
- */
- public void setCount(PrizingBean.PrizingCount count) {
- mCanPrizing = true;
- if (count != null) {
- if (count.code == 0) {
- mPrizeCount = count.data;
- setNum(mPrizeCount);
- } else {
- ToastUtil.show(PrizingActivity.this, "查询抽奖次数失败");
- }
- } else {
- ToastUtil.show(PrizingActivity.this, "访问出现错误,请检查您的网络环境");
- }
- }
- /**
- * 提示抽奖次数
- *
- * @param count
- */
- private void setNum(int count) {
- if (count >= 0) {
- mPrizingNum.setText("有 " + count + " 抽奖机会");
- }
- }
- @Override public void onBackPressed() {
- if (isOnBack && mWinningLayout != null && mWinningLayout.getVisibility() == View.VISIBLE) {
- isOnBack = false;
- TranslateAnimation animation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.animated_slide_bottom_out);
- mWinningLayout.startAnimation(animation);
- animation.setAnimationListener(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) {
- // TODO Auto-generated method stub
- isOnBack = true;
- mLayout.removeView(mWinningLayout);
- mWinningLayout.setVisibility(View.GONE);
- mLayout.setBackgroundColor(0XFF151463);
- }
- });
- return;
- }
- if (isOnBack && mNotWinningLayout != null && mNotWinningLayout.getVisibility() == View.VISIBLE) {
- isOnBack = false;
- TranslateAnimation animation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.animated_slide_bottom_out);
- mNotWinningLayout.startAnimation(animation);
- animation.setAnimationListener(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) {
- // TODO Auto-generated method stub
- isOnBack = true;
- mLayout.removeView(mNotWinningLayout);
- mNotWinningLayout.setVisibility(View.GONE);
- mLayout.setBackgroundColor(0XFF151463);
- if (isGoOn) {
- prizing(); //抽奖
- isGoOn = false;
- }
- }
- });
- return;
- }
- super.onBackPressed();
- }
- @Override protected void onDestroy() {
- super.onDestroy();
- }
- }
这里主要是通过addView和removeView的方式将弹框add进来,并且设置弹框动画即可,还需要设置弹框的回调事件,让弹框的点击事件可以让用户点击。 抽奖流程: 1、查询抽奖次数 2、判断是否可以抽奖 3、抽奖 4、根据是否中奖进行弹框。
Activity布局
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- 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:id="@+id/prize_home_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#FF151463"
- android:fitsSystemWindows="true"
- tools:context="com.joymain.jmobile.activity.PrizingActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@mipmap/prize_home_bg"
- android:orientation="vertical">
- <LinearLayout
- android:id="@+id/prize_home_bg"
- android:layout_width="300dp"
- android:layout_height="450dp"
- android:layout_marginTop="40dp"
- android:layout_gravity="center_horizontal"
- android:background="@mipmap/prize_home_dialog"
- android:gravity="center_horizontal"
- android:orientation="vertical">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="黄金十月,钻石相约"
- android:textColor="#B1019A"
- android:textSize="15dp"/>
- <TextView
- android:id="@+id/prizing_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="158dp"
- android:text="10万大奖等你来拿"
- android:textColor="#F96E6F"
- android:textSize="20dp"
- android:textStyle="bold"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="3dp"
- android:text="JM独家定制名贵臻品钻石"
- android:textColor="#ff000000"
- android:textSize="12dp"/>
- <ImageView
- android:layout_width="80dp"
- android:layout_height="80dp"
- android:layout_marginTop="10dp"
- android:src="@mipmap/prize_home_icon"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:text="新加入家人即可抽奖"
- android:textSize="12dp"/>
- <TextView
- android:id="@+id/prize_taking"
- android:layout_width="120dp"
- android:layout_height="35dp"
- android:layout_marginBottom="5dp"
- android:layout_marginTop="6dp"
- android:background="@mipmap/prize_home_take_bg"
- android:gravity="center"
- android:text="抽奖"
- android:textColor="#FEE210"/>
- <TextView
- android:id="@+id/prize_num"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dp"
- android:text="有0次抽奖机会"
- android:textColor="#FFFE9FA0"
- android:textSize="12dp"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="16dp"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/prize_rules"
- android:layout_width="60dp"
- android:layout_height="20dp"
- android:background="@mipmap/prize_home_rules"
- android:gravity="center"
- android:text="抽奖规则"
- android:textColor="#FE9FA0"
- android:textSize="10dp"/>
- <View
- android:layout_width="1dp"
- android:layout_height="20dp"
- android:layout_marginLeft="5dp"
- android:background="#ffffffff"/>
- <TextView
- android:id="@+id/prizing_share_to_friend"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="查看获奖记录"
- android:padding="4dp"
- android:textColor="#ffffffff"
- android:textSize="10dp"/>
- </LinearLayout>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/prize_winning_bar_layout"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:layout_alignParentLeft="true"
- android:paddingRight="10dp"
- android:orientation="horizontal">
- <ImageView
- android:layout_width="20dp"
- android:layout_height="20dp"
- android:layout_marginLeft="15dp"
- android:layout_gravity="center_vertical"
- android:src="@mipmap/prize_winning_back"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:text="返回"
- android:textColor="#ffffffff"
- android:textSize="16dp"/>
- </LinearLayout>
- </RelativeLayout>
中奖列表就是一个RecyclerView,再次就不需要给出来了,读者可以自行完成。
除了以上的弹框方式,其实还可以使用Dialog来实现弹框。自定义弹框内容,将定义的View通过Dialog的setContentView来add进去。
CustomPrizeDialog:
- public class CustomPrizeDialog {
- private Dialog mDialog;
- private ContentView mContentView;
- private Context mContext;
- private int defTextSize = 15;
- private int defOKBtnTextSize = 15;
- private int defTop = 65;
- private int defBottom = 40;
- private int defSpace = 30;
- private int defLineSpace = 4;
- private int contentHorizontalMargin = 50;
- public CustomPrizeDialog(Context context) {
- initDialog(context);
- }
- public CustomPrizeDialog(Context context, AttributeSet attrs) {
- initDialog(context);
- }
- public CustomPrizeDialog(Context context, AttributeSet attrs, int defStyle) {
- initDialog(context);
- }
- private void initDialog(Context context) {
- mContext = context;
- mDialog = new Dialog(context, R.style.custom_alter_dialog);
- Window window = mDialog.getWindow();
- WindowManager.LayoutParams lp = window.getAttributes();
- lp.width = WindowManager.LayoutParams.MATCH_PARENT;
- window.setAttributes(lp);
- window.setGravity(Gravity.CENTER);
- mContentView = new ContentView(context);
- mDialog.setContentView(mContentView);
- mDialog.setCancelable(true);
- mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
- @Override public void onDismiss(DialogInterface dialog) {
- if (mOnDismissListener != null) {
- mOnDismissListener.onDismiss(dialog);
- }
- }
- });
- }
- public void setCanceledOnTouchOutside(boolean cancel) {
- mDialog.setCanceledOnTouchOutside(cancel);
- }
- private DialogInterface.OnDismissListener mOnDismissListener;
- public void setOnDismissListener(DialogInterface.OnDismissListener l) {
- mOnDismissListener = l;
- }
- public void show() {
- if (mContext != null && !((Activity) mContext).isFinishing()) {
- mDialog.show();
- }
- }
- public void dismiss() {
- mDialog.dismiss();
- }
- public void setContentView(View view) {
- mContentView.setContentView(view);
- }
- public void setContentView(View view, LinearLayout.LayoutParams params) {
- mContentView.setContentView(view, params);
- }
- public void addCustomView(View view, LinearLayout.LayoutParams params) {
- mContentView.addCustomView(view, params);
- }
- public void setPositiveButton(String text, View.OnClickListener l) {
- mContentView.setPositiveButton(l);
- }
- public void setClickDismissEnabled(boolean dismissAble) {
- mContentView.setClickDismissEnabled(dismissAble);
- }
- public void setText(String text1, String text2) {
- setText(text1, defTextSize, text2, defTextSize);
- }
- public void setText(String text1, int size1, String text2, int size2) {
- setText(text1, size1, Gravity.CENTER_HORIZONTAL, text2, size2, Gravity.CENTER_HORIZONTAL);
- }
- public void setText(String text1, int size1, int gravity1, String text2, int size2, int gravity2) {
- setText(text1, size1, gravity1, text2, size2, gravity2, defSpace);
- }
- public void setText(String text1, int size1, int gravity1, String text2, int size2, int gravity2, int space) {
- mContentView.setText(text1, size1, gravity1, text2, size2, gravity2, space);
- }
- public void setText(String text, int size, int gravity) {
- mContentView.setText(null, 0, Gravity.CENTER_HORIZONTAL, text, size, gravity, 0);
- }
- public void setText(String text, int size) {
- mContentView.setText(null, 0, Gravity.CENTER_HORIZONTAL, text, size, Gravity.CENTER_HORIZONTAL, 0);
- }
- private class ContentView extends RelativeLayout {
- private LinearLayout mContent;
- private LinearLayout mContentView;
- private ImageView close;
- private OnClickListener mPositiveClickListener;
- private ScrollView mScrollView;
- private boolean mClickDismissAble = true;
- public ContentView(Context context) {
- super(context);
- initialize(context);
- }
- public void setClickDismissEnabled(boolean enabled) {
- mClickDismissAble = enabled;
- }
- private void initialize(Context context) {
- LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- params.addRule(RelativeLayout.CENTER_IN_PARENT);
- mContentView = new LinearLayout(context);
- mContentView.setId(R.id.content_id);
- mContentView.setBackgroundResource(R.drawable.prize_winning_dialog);
- addView(mContentView, params);
- mContentView.setOrientation(LinearLayout.VERTICAL);
- LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- mScrollView = new ScrollView(context);
- mContentView.addView(mScrollView, lparams);
- mScrollView.setVerticalFadingEdgeEnabled(false);
- mContent = new LinearLayout(context);
- mContent.setPadding(0, defTop, 0, defBottom);
- FrameLayout.LayoutParams fl_lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
- mScrollView.addView(mContent, fl_lp);
- mContent.setOrientation(LinearLayout.VERTICAL);
- params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- params.addRule(RelativeLayout.CENTER_HORIZONTAL);
- params.addRule(RelativeLayout.BELOW, mContentView.getId());
- params.topMargin = 200;
- close = new ImageView(context);
- close.setOnClickListener(mOnClickListener);
- close.setImageResource(R.drawable.prize_close);
- addView(close, params);
- }
- public void setPositiveButton(OnClickListener l) {
- mPositiveClickListener = l;
- }
- public void setText(String text1, int size1, int gravity1, String text2, int size2, int gravity2, int space) {
- mContent.removeAllViews();
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- params.leftMargin = 60;
- params.rightMargin = 60;
- params.topMargin = 250;
- params.gravity = Gravity.CENTER_HORIZONTAL;
- TextView tv = new TextView(getContext());
- tv.setText("恭喜你");
- tv.setTextColor(0xffffffff);
- tv.setGravity(Gravity.CENTER_HORIZONTAL);
- tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
- mContent.addView(tv, params);
- TextView tv1 = new TextView(getContext());
- tv1.getPaint().setFakeBoldText(true);
- tv1.setTextColor(0xFFFDD649);
- tv1.setGravity(gravity1);
- tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size1);
- TextView tv2 = new TextView(getContext());
- tv2.setTextColor(0xffffffff);
- tv2.setLineSpacing(defLineSpace, 1.0f); //行距12
- tv2.setGravity(gravity2);
- tv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size2);
- if (text1 != null && text1.length() > 0 && text2 != null && text2.length() > 0) {
- params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- params.leftMargin = 60;
- params.rightMargin = 60;
- params.topMargin = 10;
- params.bottomMargin = space;
- tv1.setText(text1);
- mContent.addView(tv1, params);
- params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- params.leftMargin = contentHorizontalMargin;
- params.rightMargin = contentHorizontalMargin;
- tv2.setText(text2);
- mContent.addView(tv2, params);
- } else if (text1 != null && text1.length() > 0) {
- params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- params.leftMargin = 60;
- params.rightMargin = 60;
- tv1.setText(text1);
- mContent.addView(tv1, params);
- } else if (text2 != null && text2.length() > 0) {
- params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- params.leftMargin = contentHorizontalMargin;
- params.rightMargin = contentHorizontalMargin;
- tv2.setText(text2);
- mContent.addView(tv2, params);
- }
- }
- public void setContentView(View view) {
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- setContentView(view, params);
- }
- public void setContentView(View view, LinearLayout.LayoutParams params) {
- mContent.removeAllViews();
- mContent.addView(view, params);
- }
- public void addCustomView(View view, LinearLayout.LayoutParams params) {
- mContent.addView(view, params);
- }
- private OnClickListener mOnClickListener = new OnClickListener() {
- @Override public void onClick(View v) {
- if (v == close) {
- if (mPositiveClickListener != null) {
- mPositiveClickListener.onClick(CustomPrizeDialog.ContentView.this);
- }
- if (mClickDismissAble) {
- dismiss();
- }
- }
- }
- };
- }
- public interface onBackKeyListener {
- public void onBack();
- }
- public void setonBackKeyListener(final onBackKeyListener backKeyListener) {
- mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
- @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- backKeyListener.onBack();
- return true;
- }
- return false;
- }
- });
- }
- }
dialog的样式
- <style name="custom_alter_dialog" parent="@android:style/Theme.Dialog">
- <item name="android:windowFrame">@null</item>
- <item name="android:windowIsFloating">true</item>
- <item name="android:windowIsTranslucent">false</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowBackground">@android:color/transparent</item>
- <!--<item name="android:windowBackground">@color/dialog_bg_color</item>-->
- <item name="android:backgroundDimEnabled">true</item>
- <item name="android:backgroundDimAmount">0.6</item>
- </style>
这里并没有直接使用xml来布局,而是直接使用java来编写布局。同样需要设置里面需要的点击事件的回调即可。
欢迎大家关注我的公众号,我会继续努力,分享技术、生活和感悟。
来源: https://juejin.im/post/5a30f77f51882575cb741a18