android刮奖效果

<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"
     >

   
   	
    
</RelativeLayout>


<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"
     >

   
    <Button 
        android:id="@+id/erniebtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击开始抽奖"
        />
    
    <RelativeLayout 
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        >
        
    </RelativeLayout>
        
</RelativeLayout>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lxl.lotteries"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name="com.lxl.lotteries.ErnieActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

package com.lxl.lotteries.show;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lxl.lotteries.R;
import com.lxl.lotteries.util.PhoneUtil;

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{
				
			}
			
		}
		
	};

}

package com.lxl.lotteries.show;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

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();
	}

}

package com.lxl.lotteries.util;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.telephony.TelephonyManager;
import android.view.WindowManager;

/**
 * get all manager from this class
 * @author liu
 *
 */
public class PhoneManager {
	
	
	/**
	 * get ActivityManager
	 */
	public static ActivityManager getActivityManager(Context context){
		if(context==null)return null;
		ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);  
		return activityManager;
	}
	
	/**
	 * get ActivityManager
	 */
	public static PowerManager getPowerManager(Context context){
		if(context==null)return null;
		PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);  
		return powerManager;
	}
	
	/**
	 * get WindowManager
	 */
	public static WindowManager getWindowManger(Context context){
		if(context==null)return null;
		WindowManager windowManager = null;
		windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		return windowManager;
	}
	
	/**
	 * get TelephonyManager
	 */
	public static TelephonyManager getTelephonyManager(Context context){
		if(context==null)return null;
		TelephonyManager telephonyManager=null;
		telephonyManager=(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager;
	}
	
	/**
	 * get ConnectivityManager
	 */
	public static ConnectivityManager getConnectivityManager(Context context){
		if(context==null)return null;
		ConnectivityManager connectivityManager=null;
		connectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		return connectivityManager;
	}
	
	/**
	 * 
	 */
	public static NetworkInfo getNetworkInfo(Context context){
		if(context==null)return null;
		NetworkInfo ni = getConnectivityManager(context).getActiveNetworkInfo();
	    return ni;
	}
	
	/**
	 * wifi's state can change
	 * so everytime calling this method we must get a new WifiManager
	 * @param context
	 * @return
	 */
	public static WifiManager getWifiManager(Context context){
		if(context==null)return null;
		WifiManager wifiManager=null;
		wifiManager=(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		return wifiManager;
	}

	/**
	 * get WifiInfo
	 * @param context
	 * @return
	 */
	public static WifiInfo getWifiInfo(Context context){
		if(context==null)return null;
		return getWifiManager(context).getConnectionInfo();
	}

	public static LocationManager getLocationManager(Context context) {
		if(context==null)return null;
		return (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
	}
	
	public static NotificationManager getNotificationManager(Context context) {
		if(context==null)return null;
		return (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}
	
	public static WakeLock getWakeLock(Context context){
		PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
		WakeLock mWakeLock=pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
		return mWakeLock;
	}
}

package com.lxl.lotteries.util;


import java.lang.reflect.Field;


import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;


/**
 * get any infomation about phone from this class
 * @author liu
 */
public class PhoneUtil {
	public static int screenHeight;
	public static int screenWidth;
	public static int unitHeight;
	public static int sbar;
	public static int itemHeight;
	
	public static void init(Context context){
		int[] resolution=getResolution(context);
		screenWidth=resolution[0];
		screenHeight=resolution[1];
		unitHeight=screenHeight/10;
		sbar = getSbar(context);
		itemHeight=screenHeight-2*unitHeight-sbar;
	}
	
    public double getDensity(Context context){
    	DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    	return displayMetrics.density;
    }
    
    /**
     * get resolution
     * @param context
     * @return
     */
    public static int[] getResolution(Context context){
    	int resolution[]=new int[2];
    	DisplayMetrics dm = new DisplayMetrics();
    	PhoneManager.getWindowManger(context).getDefaultDisplay().getMetrics(dm);
    	resolution[0]=dm.widthPixels;
    	resolution[1]=dm.heightPixels;
    	return resolution;
    }
    
	/**
	 * get Imsi(International Mobile Subscriber Identification Number)
	 * @param context
	 * @return Imsi
	 */
	public static String getImsi(Context context){
		String imsi = PhoneManager.getTelephonyManager(context).getSubscriberId();
		return imsi;
	}
    
	public static String getDeviceId(Context context){
		String deviceId=PhoneManager.getTelephonyManager(context).getDeviceId();
		return deviceId;
	}
	
	/**
	 * ��ȡ��վ��λ��Ϣ
	 * ��������Ϊ int[0]=cid int[1]=lac 
	 * @param context
	 */
    public static int[] getCellLocation(Context context){
    	int[] i=new int[2];
    	String simType=getPhoneType(context);
    	if(simType.equals("GSM")){
    		GsmCellLocation gsm = (GsmCellLocation) PhoneManager.getTelephonyManager(context).getCellLocation();  
    		i[0] = gsm.getCid();  
    		i[1] = gsm.getLac();  
    	}else{
    		 CdmaCellLocation cdma = (CdmaCellLocation) PhoneManager.getTelephonyManager(context).getCellLocation(); 
    		 i[0] = cdma.getBaseStationId(); 
    		 i[1] = cdma.getNetworkId(); 
    	}
    	return i;
    }
    
    /**��ȡ�������� GSM ���� CDMA*/
	public static String getPhoneType(Context context) {
		String simType="NONE";
		TelephonyManager tm = PhoneManager.getTelephonyManager(context);
		int phoneType = tm.getPhoneType();
		if (phoneType == TelephonyManager.PHONE_TYPE_GSM) {
			simType = "GSM";
		} else if (phoneType == TelephonyManager.PHONE_TYPE_CDMA) {
			simType = "CDMA";
		} else {
			simType = "NONE";
		}
		return simType;
	}
    
	/**
	 * �ж��Ƿ�Ϊƽ��
	 * @return
	 */
	public static boolean isPad(Context context) {
		WindowManager wm = PhoneManager.getWindowManger(context);
		Display display = wm.getDefaultDisplay();
		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
		double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
		// ��Ļ�ߴ�
		double screenInches = Math.sqrt(x + y);
		// ����6�ߴ���ΪPad
		if (screenInches >= 6.0) {
			return true;
		}
		return false;
	}
    
	/**
	 * get fit width for different resolution,input param is width for 720
	 * @param width
	 */
	public static int getFitWidth(Context context,int inwidth){
		if(context==null)
			return inwidth;
		int screenwidth=getResolution(context)[0];
		return (inwidth*screenwidth)/720;
	}
	
	/**
	 * get fit width for different resolution,input param is width for 1180
	 * @param width
	 */
	public static int getFitHeight(Context context,int inheight){
		if(context==null)
			return inheight;
		int screenheight=getResolution(context)[1];
		return (inheight*screenheight)/1280;
	}
	
	public static int getFitTextSize(Context context,int textsize){
		if(context==null)
			return textsize;
		int screenheight=getResolution(context)[1];
		return (textsize*screenheight)/1280;
	}
	
	public static int getSbar(Context context){
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0, sbar = 0;
		try {
		    c = Class.forName("com.android.internal.R$dimen");
		    obj = c.newInstance();
		    field = c.getField("status_bar_height");
		    x = Integer.parseInt(field.get(obj).toString());
		    sbar = context.getResources().getDimensionPixelSize(x);
		} catch (Exception e1) {
		    e1.printStackTrace();
		} 
		return sbar;
	}
	
	public static void showToast(final Context context,final String msg){
		((Activity)context).runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
			}
		});
	}
}

package com.lxl.lotteries;

import com.lxl.lotteries.show.ErinieShow;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;

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();
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值