经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android自定义view制作抽奖转盘
来源:jb51  时间:2018/12/17 9:22:57  对本文有异议

本文实例为大家分享了Android自定义view制作抽奖转盘的具体代码,供大家参考,具体内容如下

效果图

TurntableActivity

  1. package com.bawei.myapplication.turntable;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.animation.RotateAnimation;
  8.  
  9. import com.bawei.myapplication.R;
  10. import com.bawei.myapplication.turntable.CustomTurntableView;
  11.  
  12. /**
  13. * 转盘
  14. * @author hasee
  15. */
  16. public class TurntableActivity extends AppCompatActivity {
  17.  
  18. CustomTurntableView customTurntableView;
  19. boolean isTouchInSide = false;
  20. float mDownX, mDownY;
  21.  
  22.  
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_turntable);
  27.  
  28. initView();
  29. }
  30.  
  31. private void initView() {
  32. customTurntableView = findViewById(R.id.custom);
  33. // findViewById(R.id.custom_inside).setOnClickListener(new View.OnClickListener() {
  34. // @Override
  35. // public void onClick(View v) {
  36. // float degrees = (float)(720 + Math.random() * 1000);
  37. // RotateAnimation rotateAnimation = new RotateAnimation(0, -degrees, 450, 450);
  38. // rotateAnimation.setDuration(5000);
  39. // rotateAnimation.setFillAfter(true);
  40. // customCircleView.startAnimation(rotateAnimation);
  41. // }
  42. // });
  43.  
  44. findViewById(R.id.custom_inside).setOnTouchListener(new View.OnTouchListener() {
  45. @Override
  46. public boolean onTouch(View v, MotionEvent event) {
  47. if (event.getAction() == MotionEvent.ACTION_DOWN &&
  48. event.getX() > 200 &&
  49. event.getX() < 300 &&
  50. event.getY() > 200 &&
  51. event.getY() < 300) {
  52. isTouchInSide = true;
  53. mDownX = event.getX();
  54. mDownY = event.getY();
  55. return true;
  56. }else if(event.getAction() == MotionEvent.ACTION_MOVE && (
  57. event.getX() < mDownX -10 ||
  58. event.getX() > mDownX + 10 ||
  59. event.getY() < mDownY -10 ||
  60. event.getY() > mDownY + 10) ){
  61. isTouchInSide = false;
  62. } else if (event.getAction() == MotionEvent.ACTION_UP &&
  63. event.getX() > mDownX -10 &&
  64. event.getX() < mDownX + 10 &&
  65. event.getY() > mDownY -10 &&
  66. event.getY() < mDownY + 10 &&
  67. isTouchInSide) {
  68. float degrees = (float) (720 + Math.random() * 1000);
  69. RotateAnimation rotateAnimation = new RotateAnimation(0, -degrees, 250, 250);
  70. rotateAnimation.setDuration(5000);
  71. rotateAnimation.setFillAfter(true);
  72. customTurntableView.startAnimation(rotateAnimation);
  73. }
  74. isTouchInSide = false;
  75. return false;
  76. }
  77. });
  78. }
  79. }
  80.  

对应的布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:id="@+id/ll"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:orientation="vertical"
  10. tools:context=".MainActivity">
  11.  
  12. <com.bawei.myapplication.turntable.CustomTurntableView
  13. android:id="@+id/custom"
  14. android:layout_width="wrap_content"
  15. android:layout_height="500dp"
  16. />
  17.  
  18. <com.bawei.myapplication.turntable.CustomTurntableInsideView
  19. android:id="@+id/custom_inside"
  20. android:layout_width="wrap_content"
  21. android:layout_height="500dp"
  22. app:text="开始"
  23. android:background="#3300ff00" />
  24. </RelativeLayout>

自定义CustomTurntableView继承view

  1. package com.bawei.myapplication.turntable;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Path;
  8. import android.graphics.RectF;
  9. import android.support.annotation.Nullable;
  10. import android.util.AttributeSet;
  11. import android.view.View;
  12.  
  13. /**
  14. * 这里是画转盘的
  15. * @author hasee
  16. */
  17. public class CustomTurntableView extends View{
  18. Paint mPaint;
  19. int mCircleCount = 6;
  20. float mStartAngle = 0;
  21.  
  22. RectF rectF;
  23.  
  24. public CustomTurntableView(Context context) {
  25. super(context);
  26. init();
  27. }
  28.  
  29. public CustomTurntableView(Context context, @Nullable AttributeSet attrs) {
  30. super(context, attrs);
  31. init();
  32. }
  33.  
  34. private void init(){
  35. mPaint = new Paint();
  36. mPaint.setColor(Color.BLUE);
  37. mPaint.setStrokeWidth(10);
  38. mPaint.setTextSize(60);
  39. mPaint.setStyle(Paint.Style.FILL);
  40.  
  41. rectF = new RectF();
  42. rectF.top = 100;
  43. rectF.left = 100;
  44. rectF.right = 400;
  45. rectF.bottom = 400;
  46. }
  47. String[] textColor = {"一 等 奖","二 等 奖","三 等 奖","四 等 奖","五 等 奖","六 等 奖"};
  48. @Override
  49. protected void onDraw(Canvas canvas) {
  50. super.onDraw(canvas);
  51. for(int i = 0; i < mCircleCount; i++){
  52. //按角标单双号设置扇形颜色,
  53. if(i % 2 == 0 ){
  54. mPaint.setColor(Color.BLUE);
  55. }else{
  56. mPaint.setColor(Color.GREEN);
  57. }
  58. canvas.drawArc(rectF, mStartAngle, 60, true, mPaint);
  59. //设置转盘上的文字
  60. mPaint.setColor(Color.BLACK);
  61. mPaint.setTextSize(20);
  62. Path path = new Path();
  63. path.addArc(rectF,mStartAngle+20,60);
  64. canvas.drawTextOnPath(textColor[i],path,-10,40,mPaint);
  65. mStartAngle += 60;
  66. }
  67. }
  68. }

自定义CustomTurntableInsideView继承view

  1. package com.bawei.myapplication.turntable;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.RectF;
  9. import android.support.annotation.Nullable;
  10. import android.util.AttributeSet;
  11. import android.view.View;
  12.  
  13. import com.bawei.myapplication.R;
  14.  
  15. /**
  16. * 转盘中间开始按钮和指针
  17. * @author hasee
  18. */
  19. public class CustomTurntableInsideView extends View {
  20. /**
  21. * 画笔
  22. */
  23. Paint mPaint;
  24. RectF mRectF;
  25. String mStr;
  26.  
  27. public CustomTurntableInsideView(Context context) {
  28. super(context);
  29. init();
  30. }
  31.  
  32. public CustomTurntableInsideView(Context context, @Nullable AttributeSet attrs) {
  33. super(context, attrs);
  34.  
  35. //自定义属性,如何添加自定义属性如下(考点)
  36. //第一步:在values文件夹下创建attrs.xml
  37. //第二步:详见attrs.xml文件内部
  38. //第三步:在所在的布局文件的根layout中添加xmlns:app="http://schemas.android.com/apk/res-auto"
  39. //第四步:在布局文件的控件中添加app:"你在attrs中设置的attr name"="你的值"
  40. //第五步:调用下面这句话,最后的为R.styleable.你在attrs中设置的declare-styleable name
  41. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTurntableView);
  42. //第六步:调用下面这句话,根据你在attrs中设置的format,选择getXXX方法,
  43. //入参为 R.styleable. 加上 你在attrs中设置的declare-styleable name 加上 _ 加上 你在attrs中设置的attr name
  44. mStr = typedArray.getString(R.styleable.CustomTurntableView_text);
  45. init();
  46. }
  47.  
  48. private void init() {
  49. //以下注释请看CustomBingView里面
  50. mPaint = new Paint();
  51. mPaint.setColor(Color.RED);
  52. mPaint.setStrokeWidth(10);
  53. mPaint.setTextSize(20);
  54. mPaint.setStyle(Paint.Style.FILL);
  55.  
  56. mRectF = new RectF();
  57. mRectF.top = 50;
  58. mRectF.bottom = 300;
  59. mRectF.right = 300;
  60. mRectF.left = 200;
  61. }
  62.  
  63. @Override
  64. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  65. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  66.  
  67. // setMeasuredDimension(300, 300);
  68. }
  69.  
  70. @Override
  71. protected void onDraw(Canvas canvas) {
  72. super.onDraw(canvas);
  73.  
  74. //设置画笔颜色为黑色,
  75. mPaint.setColor(Color.BLACK);
  76. //画出指针,用一个扇形,然后盖住后面补分来简单表示
  77. canvas.drawArc(mRectF, 60, 60, true, mPaint);
  78.  
  79. mPaint.setColor(Color.RED);
  80. //画一个红色的圆形,就是中间的大按钮
  81. canvas.drawCircle(250, 250, 50, mPaint);
  82.  
  83. mPaint.setColor(Color.BLACK);
  84. //添加按钮上的文字
  85. canvas.drawText(mStr, 230, 260, mPaint);
  86.  
  87. //画三角,第一步,创建路径
  88. // Path path = new Path();
  89. //第二步,moveTo第一个顶点
  90. // path.moveTo(300, 300);
  91. //后续相继lineTo其他顶点
  92. // path.lineTo(300, 400);
  93. // path.lineTo(400, 400);
  94. //闭合
  95. // path.close();
  96. // 画
  97. // canvas.drawPath(path, mPaint);
  98. }
  99. }

自定义属性attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- name为想要调用这个属性的类名即可 -->
  4.  
  5. <declare-styleable name="CustomTurntableView">
  6. <!-- name为属性的名字,可以随意起,只要符合规则看得懂 -->
  7. <!-- format为属性内容的类型 -->
  8. <attr name="text" format="string"></attr>
  9. </declare-styleable>
  10. </resources>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue,关注w3xue公众号的更多精彩内容。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号