最近做了一个转盘随机旋转获取对应的值,做了一个山寨的转盘抽奖,关键的代码如下:
该类控抽旋转,注:mPlayPlateBitmap 、mPlayPlatePointBitmap是两张图片,这两张图片的大小是一样的并关注他们的中心点要在一起,那样子才更有视觉效果!,
- import android.content.Context;
- import android.content.res.Resources;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Matrix;
- import android.os.Handler;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.View;
- //自定义的转盘View
- public class PlayPlateView extends View implements Runnable {
- // 界面需要的图片
- private Bitmap mPlayPlateBitmap;
- private Bitmap mPlayPlatePointBitmap;
- private int mNumberOfTurns = 10;
- // 旋转矩阵
- private Matrix mPlayPlateMatrix = new Matrix();
- // 平移矩阵
- private Matrix mPlatePointMatrix = new Matrix();
- private int mRotationAngle = 0;
- private boolean isRotate = false;
- private int mStopAngle = -1;
- private int mScreenWidth = 0;// 屏幕的分辨率
- private int mRotateSpeed = 100;// 控制转速!
- private Thread mThread;
- private int mStopPosition;// 停止的位置,表示在那一个等份停止,从PlayPlateActivity传过来,然后在停止之后又传回去
- private Handler mHandler;
- public PlayPlateView(Context context) {
- super(context);
- }
- public PlayPlateView(Context context, AttributeSet attrs) {
- super(context, attrs);
- Resources r = context.getResources();
- // 设置指针平移矩阵为按向量(160,160-指针的高度)平移
- int index = 0;
- int index2 = 0;
- mPlatePointMatrix.setTranslate(index, index2);//控制转盘底座的位置
- // 生成图片
- mPlayPlateBitmap = BitmapFactory.decodeStream(r
- .openRawResource(R.drawable.playplate)); //转盘底座图片
- mPlayPlatePointBitmap = BitmapFactory.decodeStream(r
- .openRawResource(R.drawable.plate_points)); //指针图片
- }
- // 重写View类的onDraw()函数
- @Override
- protected void onDraw(Canvas canvas) {
- int radius = mPlayPlateBitmap.getWidth() / 2;// 以底座生成的圆心
- if(TurntableDemoActivity.SCREEN_WIDHT==320){
- mPlayPlateMatrix.setRotate(mRotationAngle, radius -1, radius - 2);// 指针的位置
- }else{
- mPlayPlateMatrix.setRotate(mRotationAngle, radius-1, radius -2);
- }
- Log.e("zhuanpan", "x: " + mRotationAngle);
- canvas.drawBitmap(mPlayPlateBitmap, mPlatePointMatrix, null);// 画转盘底
- canvas.drawBitmap(mPlayPlatePointBitmap, mPlayPlateMatrix, null);// 画指针
- }
- public void run() {
- try {
- while (true) {
- if (isRotate) {
- this.mRotationAngle += 20;
- if (mRotationAngle <= mStopAngle) {
- this.postInvalidate();
- } else {
- mHandler.sendEmptyMessage(mStopPosition);
- stopRotate();
- }
- if (mRotateSpeed > 20 && mRotationAngle < mStopAngle - 700) {// 加速,通过angle,睡眠多久来表示转动一下
- Log.i("runing", "加速:andgle=" + mRotateSpeed + " x="
- + mRotationAngle + " stopAngle=" + mStopAngle);
- mRotateSpeedmRotateSpeed = mRotateSpeed - 2;
- Thread.sleep(mRotateSpeed);
- } else if (mRotationAngle > mStopAngle - 700) {// 减速,最后700
- Log.i("runing", "减速:andgle=" + mRotateSpeed + " x="
- + mRotationAngle + " stopAngle=" + mStopAngle);
- mRotateSpeedmRotateSpeed = mRotateSpeed + 2;
- Thread.sleep(mRotateSpeed);
- } else {// 均速
- Thread.sleep(mRotateSpeed);
- }
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public void startRotate(Handler handler) {
- this.mRotationAngle = 0;
- mHandler = handler;// 调用playPlateAcitvity的通道
- if (mThread == null) {
- mThread = new Thread(this);
- mThread.start();
- } else {
- Log.e("tag", "not null");
- mThread.interrupt();
- mThread = null;
- mThread = new Thread(this);
- mThread.start();
- }
- this.isRotate = true;
- }
- public void stopRotate() {
- this.isRotate = false;
- if (mThread != null) {
- mThread.interrupt();
- mThread = null;
- }
- }
- public boolean getRotate() {
- return this.isRotate;
- }
- public void setStopLocation(int angle, int stopLocation) {
- this.mStopAngle = 0;
- this.mStopPosition = 0;
- this.mStopPosition = stopLocation;
- this.mRotationAngle = 0;
- this.mStopAngle = angle + (mNumberOfTurns * 360);
- }
- }
通过下面这个类开启转盘Activity,
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class PlayplateActivity extends Activity {
- /** Called when the activity is first created. */
- PlayPlateView panView = null;
- private ImageView p_w_picpath;
- private int awarded = 0;
- boolean bClickPanView = true;
- private TextView turntable_Text;
- Handler myHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // TODO Auto-generated method stub
- super.handleMessage(msg);
- try {
- switch (msg.what) {
- case 0:
- awarded = 70;// 340
- break;
- case 1:
- awarded = 10;// 280
- break;
- case 2:
- awarded = 100;// 240
- break;
- case 3:
- awarded = 20;// 200
- break;
- case 4:
- awarded = 10;// 160
- break;
- case 5:
- awarded = 50;// 120
- break;
- case 6:
- awarded = 20;// 80
- break;
- case 7:
- awarded = 5;// 20
- break;
- default:
- break;
- }
- postData(awarded);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- };
- public void postData(int i) {
- if (i > 0) {
- Toast.makeText(this, "转盘积分为:" + i, Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(this, "转盘获取积分失败!", Toast.LENGTH_LONG).show();
- }
- bClickPanView = true;
- }
- /**
- * 获取触屏的坐标!
- */
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // TODO Auto-generated method stub
- return super.onTouchEvent(event);
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.turntable);
- panView = (PlayPlateView) findViewById(R.id.relativeLayout2);
- setTextView();
- p_w_picpath = (ImageView) findViewById(R.id.playplate_bank);
- panView.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if (bClickPanView) {
- bClickPanView = false;
- panView.startRotate(myHandler);
- testLottery();
- }
- }
- });
- p_w_picpath.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- panView.stopRotate();
- }
- });
- }
- public void setTextView() {
- turntable_Text = (TextView) findViewById(R.id.turntable_text);
- turntable_Text.setTextSize(TurntableDemoActivity.TurntableSize);
- turntable_Text.setText(TurntableDemoActivity.TurntableText);
- turntable_Text.invalidate();
- }
- /**
- * 控制旋转后指针指向转盘的位置
- */
- private int Angle = 0;
- private void testLottery() {
- int j = 0;
- int p = 8;
- int W = (int) (Math.random() * p + j);
- Log.i("W", W + "");
- switch (W) {
- case 0:
- Angle = 22;// 340
- break;
- case 1:
- Angle = 67;// 280
- break;
- case 2:
- Angle = 123;// 240
- break;
- case 3:
- Angle = 167;// 200
- break;
- case 4:
- Angle = 203;// 160
- break;
- case 5:
- Angle = 257;// 120
- break;
- case 6:
- Angle = 313;// 80
- break;
- case 7:
- Angle = 357;// 20
- break;
- default:
- break;
- }
- panView.setStopLocation(Angle, W);
- }
- }
- public class TurntableDemoActivity extends Activity {
- /** Called when the activity is first created. */
- public static int SCREEN_WIDHT = 0;
- public static int SCREEN_HEIGHT = 0;
- public static int TurntableSize = 12;
- public static String TurntableText = "转盘抽奖";
- private Button startTurntable;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- this.setContentView(R.layout.main);
- startTurntable = (Button) findViewById(R.id.play_turntable);
- startTurntable.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(TurntableDemoActivity.this,
- PlayplateActivity.class);
- startActivity(intent);
- }
- });
- }
- }
mail.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <Button
- android:id="@+id/play_turntable"
- android:layout_width="300dip"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dip"
- android:text="启动转盘" />
- </LinearLayout>
turntable.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <FrameLayout
- android:id="@+id/zhuanpan_layout"
- android:layout_width="280dip"
- android:layout_height="320dip"
- android:gravity="center" >
- <LinearLayout
- android:id="@+id/zhuanpan_layout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/turn_bg"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/turntable_text"
- android:layout_width="215dip"
- android:layout_height="wrap_content"
- android:ellipsize="end"
- android:layout_marginTop="10dip"
- android:lines="2"
- android:textColor="@color/playplate_text"
- />
- <com.wacosoft.turntable.PlayPlateView
- android:id="@+id/relativeLayout2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <View style="@style/FootBarSpringH" />
- <ImageView
- android:id="@+id/playplate_bank"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/turntable_bank" />
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
在Values文件创建colors.xml
- <resources>
- <color name="playplate_text">#282828</color>
- <color name="transparent">#0000</color>
- </resources>
styles.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="FootBarSpringH">
- <item name="android:layout_width">fill_parent</item>
- <item name="android:layout_height">0sp</item>
- <item name="android:layout_weight">1</item>
- </style>
- <style name="Transparent">
- <item name="android:windowBackground">@color/transparent</item>
- <item name="android:windowIsTranslucent">true</item>
- <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
- </style>
- </resources>
转盘的背景色是用一张小图片平铺的
drawable/turn_bg.xml
- <?xml version="1.0" encoding="utf-8"?>
- <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
- android:src="@drawable/turntable_bg"
- android:tileMode="repeat" >
- </bitmap>
图片没有提供,图片也有相应的说明!