android简单抽奖

来源:互联网 发布:vs2013怎么写c语言 编辑:程序博客网 时间:2024/05/19 11:49

--------------------------.xml布局文件中的布局如下:

<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/lightLv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/light"
        />
    
    <ImageView
          android:id="@+id/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>


--------------------显示Activity代码如下:

public class MainActivity extends Activity {

    private ImageView lightlv, wheel, point;
    private static final long ONE_WHEEL_TIME = 500;
    private boolean flag;
    private int[] laps = { 5, 7, 10, 13 };
    private int[] angles = { 0, 60, 120, 180, 240, 300 };
    private int startangle = 0;
    private String[] wheelMessage={"索尼PSP GO","10元红包","谢谢参与","DNF钱包","OPPO MP3","5元红包"};
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 0:
                if (flag) {
                    lightlv.setVisibility(View.INVISIBLE);
                    flag = false;
                } else {
                    lightlv.setVisibility(View.VISIBLE);
                    flag = true;
                }
                break;

            default:
                break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lightlv = (ImageView) findViewById(R.id.lightLv);
        wheel = (ImageView) findViewById(R.id.wheel);
        point = (ImageView) findViewById(R.id.point);
        Flash();
        point.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int lap = laps[(int) (Math.random() * 4)];
                int angle = angles[(int) (Math.random() * 6)];
                int increaseDegree = lap * 360 + angle;
                RotateAnimation rotateAnimation = new RotateAnimation(
                        startangle, startangle + increaseDegree,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                startangle+=increaseDegree;
                long time=(lap+angle/360)*ONE_WHEEL_TIME;
                rotateAnimation.setDuration(time);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setAnimationListener(animationListener);
                point.startAnimation(rotateAnimation);
            }
        });
    }
    
    private AnimationListener animationListener=new 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 message=wheelMessage[startangle%360/60];
            Toast.makeText(MainActivity.this,"恭喜你抽中"+message,Toast.LENGTH_LONG).show();
        }
    };

    private void Flash() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        };
        timer.schedule(task, 0, ONE_WHEEL_TIME);
    }







0 0