歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android實現刮獎的效果

突發奇想,Android實現類似於刮彩票的效果,於是完成了一個小例子,其中有不完善的地方,比如判斷彩票是否中獎是以是否刮彩票3次判斷的,而不是以面積來計算的。

核心部分是借鑒網上代碼的,不過找不到源地址了。

實現了基本的刮獎效果,並且進行了一定的封裝,拷貝其中的類即可使用。

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2014年資料/5月/26日/Android實現刮獎的效果

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

示例圖:

代碼如下:

ErnieActivity類:

 public class ErnieActivity extends Activity{
 Button erniebtn;
 RelativeLayout container;
 ErinieShow erinieShow;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  erniebtn=(Button) findViewById(R.id.erniebtn);
  container=(RelativeLayout) findViewById(R.id.container);
 
  erniebtn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    showErnie();
   }
  });
 
 
 }

 
 private void showErnie(){
//  container
  container.removeAllViews();
 
  int level = getLevel();
  erinieShow=new ErinieShow(this, level);
 
  container.addView(erinieShow,new LayoutParams(-2,-2));
 }
 
 /**
  * 獲取獎勵等級
  * @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();
 }

}

ErinieShow類:

public class ErinieShow extends RelativeLayout {
 Context context;
 RelativeLayout rubblerBG;
 RubblerShow rubblerShow;
 Button getReward;

 int rubblerBGId = 10001;
 int getRewardId = 10002;

 int level;

 public ErinieShow(Context context, int level) {
  super(context);
  this.context = context;
  this.level = level;
  getElement();
  setElementLP();
  setElementStyle();
  setElement();
 }

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

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

 private void setElementLP() {
  int[] resolution = PhoneUtil.getResolution(context);
  RelativeLayout.LayoutParams rubblerBG_LP = new RelativeLayout.LayoutParams(
    resolution[0], PhoneUtil.getFitHeight(context, 125));

  rubblerBG.setLayoutParams(rubblerBG_LP);
  rubblerShow.setLayoutParams(rubblerBG_LP);
 
  RelativeLayout.LayoutParams getReward_LP = new LayoutParams(-2, -2);
  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);

   break;
  case 2:
   rubblerBG.setBackgroundResource(R.drawable.rewardlevel2);
   break;
  default:
   rubblerBG.setBackgroundResource(R.drawable.rewardlevel3);
   break;
  }
//  getReward.setBackgroundResource(R.drawable.get_award);
 }

 private void setElement() {
  rubblerShow.beginRubbler(Color.parseColor("#d3d3d3"), 30, 10);
 
 
  getReward.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    if(level==0){
     Toast.makeText(context, "很遺憾,此次未中獎,再接再厲吧!", Toast.LENGTH_SHORT).show();
    }else{
     Toast.makeText(context, "恭喜您,獲得了"+level+"等獎。", Toast.LENGTH_SHORT).show();
    }
   }
  });
  //先設置為不可點擊
  getReward.setClickable(false);
 
  getReward.setText("領獎");
 }
 
 Handler handler=new Handler(){

  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   if(msg.what==200){
    getReward.setClickable(true);
   }else{
   
   }
   
  }
 
 };

}

RubblerShow類:

public class RubblerShow extends TextView {

 private float TOUCH_TOLERANCE; // 填充距離,使線條更自然,柔和,值越小,越柔和。

 // private final int bgColor;

 private Bitmap mBitmap;
 private Canvas mCanvas;
 private Paint mPaint;
 private Path mPath;
 private float mX, mY;

 private boolean isDraw = false;
 
 Handler handler;
 int time=0;
 
 public RubblerShow(Context context, Handler handler) {
  super(context);
  this.handler=handler;
  // bgColor =
  // attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android",
  // "textColor", 0xFFFFFF);
  // System.out.println(bgColor);
  // System.out.println(attrs.getAttributeValue("http://schemas.android.com/apk/res/android",
  // "layout_width"));
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  if (isDraw) {
   mCanvas.drawPath(mPath, mPaint);
   // mCanvas.drawPoint(mX, mY, mPaint);
   canvas.drawBitmap(mBitmap, 0, 0, null);
  }
 }

 /**
  * 開啟檫除功能
  *
  * @param bgColor
  *            覆蓋的背景顏色
  * @param paintStrokeWidth
  *            觸點(橡皮)寬度
  * @param touchTolerance
  *            填充距離,值越小,越柔和。
  */
 public void beginRubbler(final int bgColor, final int paintStrokeWidth,
   float touchTolerance) {
  TOUCH_TOLERANCE = touchTolerance;
  // 設置畫筆
  mPaint = new Paint();
  // mPaint.setAlpha(0);
  // 畫筆劃過的痕跡就變成透明色了
  mPaint.setColor(Color.BLACK); // 此處不能為透明色
  mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
  // 或者
  // mPaint.setAlpha(0);
  // mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

  mPaint.setAntiAlias(true);
  mPaint.setDither(true);
  mPaint.setStyle(Paint.Style.STROKE);
  mPaint.setStrokeJoin(Paint.Join.ROUND); // 前圓角
  mPaint.setStrokeCap(Paint.Cap.ROUND); // 後圓角
  mPaint.setStrokeWidth(paintStrokeWidth); // 筆寬

  // 痕跡
  mPath = new Path();
  // 覆蓋
  LayoutParams layoutParams = getLayoutParams();
  int height = layoutParams.height;
  int width;
  if (getLayoutParams().width == LayoutParams.MATCH_PARENT) {
   width = 700;
  } else {
   width = layoutParams.width;
  }

  mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  mCanvas = new Canvas(mBitmap);

  mCanvas.drawColor(bgColor);
  isDraw = true;
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  if (!isDraw) {
   return true;
  }
  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN: // 觸點按下
   // touchDown(event.getRawX(),event.getRawY());
   touchDown(event.getX(), event.getY());
   invalidate();
   break;
  case MotionEvent.ACTION_MOVE: // 觸點移動
   touchMove(event.getX(), event.getY());
   invalidate();
   if(time++>3&&handler!=null){
    handler.sendEmptyMessage(200);
   }
   break;
  case MotionEvent.ACTION_UP: // 觸點彈起
   touchUp(event.getX(), event.getY());
   invalidate();
   break;
  default:
   break;
  }
  return true;
 }

 private void touchDown(float x, float y) {
  mPath.reset();
  mPath.moveTo(x, y);
  mX = x;
  mY = y;
 }

 private void touchMove(float x, float y) {
  float dx = Math.abs(x - mX);
  float dy = Math.abs(y - mY);
  if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
   mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
   mX = x;
   mY = y;
  }

 }

 private void touchUp(float x, float y) {
  mPath.lineTo(x, y);
  mCanvas.drawPath(mPath, mPaint);
  mPath.reset();
 }

}

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved