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

效果图

TurntableActivity

package com.bawei.myapplication.turntable;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.view.animation.RotateAnimation;

import com.bawei.myapplication.R;

import com.bawei.myapplication.turntable.CustomTurntableView;

/**

* 转盘

* @author hasee

*/

public class TurntableActivity extends AppCompatActivity {

CustomTurntableView customTurntableView;

boolean isTouchInSide = false;

float mDownX, mDownY;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_turntable);

initView();

}

private void initView() {

customTurntableView = findViewById(R.id.custom);

// findViewById(R.id.custom_inside).setOnClickListener(new View.OnClickListener() {

// @Override

// public void onClick(View v) {

// float degrees = (float)(720 + Math.random() * 1000);

// RotateAnimation rotateAnimation = new RotateAnimation(0, -degrees, 450, 450);

// rotateAnimation.setDuration(5000);

// rotateAnimation.setFillAfter(true);

// customCircleView.startAnimation(rotateAnimation);

// }

// });

findViewById(R.id.custom_inside).setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN &&

event.getX() > 200 &&

event.getX() < 300 &&

event.getY() > 200 &&

event.getY() < 300) {

isTouchInSide = true;

mDownX = event.getX();

mDownY = event.getY();

return true;

}else if(event.getAction() == MotionEvent.ACTION_MOVE && (

event.getX() < mDownX -10 ||

event.getX() > mDownX + 10 ||

event.getY() < mDownY -10 ||

event.getY() > mDownY + 10) ){

isTouchInSide = false;

} else if (event.getAction() == MotionEvent.ACTION_UP &&

event.getX() > mDownX -10 &&

event.getX() < mDownX + 10 &&

event.getY() > mDownY -10 &&

event.getY() < mDownY + 10 &&

isTouchInSide) {

float degrees = (float) (720 + Math.random() * 1000);

RotateAnimation rotateAnimation = new RotateAnimation(0, -degrees, 250, 250);

rotateAnimation.setDuration(5000);

rotateAnimation.setFillAfter(true);

customTurntableView.startAnimation(rotateAnimation);

}

isTouchInSide = false;

return false;

}

});

}

}

对应的布局

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/ll"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:id="@+id/custom"

android:layout_width="wrap_content"

android:layout_height="500dp"

/>

android:id="@+id/custom_inside"

android:layout_width="wrap_content"

android:layout_height="500dp"

app:text="开始"

android:background="#3300ff00" />

自定义CustomTurntableView继承view

package com.bawei.myapplication.turntable;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.RectF;

import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.view.View;

/**

* 这里是画转盘的

* @author hasee

*/

public class CustomTurntableView extends View{

Paint mPaint;

int mCircleCount = 6;

float mStartAngle = 0;

RectF rectF;

public CustomTurntableView(Context context) {

super(context);

init();

}

public CustomTurntableView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

init();

}

private void init(){

mPaint = new Paint();

mPaint.setColor(Color.BLUE);

mPaint.setStrokeWidth(10);

mPaint.setTextSize(60);

mPaint.setStyle(Paint.Style.FILL);

rectF = new RectF();

rectF.top = 100;

rectF.left = 100;

rectF.right = 400;

rectF.bottom = 400;

}

String[] textColor = {"一 等 奖","二 等 奖","三 等 奖","四 等 奖","五 等 奖","六 等 奖"};

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

for(int i = 0; i < mCircleCount; i++){

//按角标单双号设置扇形颜色,

if(i % 2 == 0 ){

mPaint.setColor(Color.BLUE);

}else{

mPaint.setColor(Color.GREEN);

}

canvas.drawArc(rectF, mStartAngle, 60, true, mPaint);

//设置转盘上的文字

mPaint.setColor(Color.BLACK);

mPaint.setTextSize(20);

Path path = new Path();

path.addArc(rectF,mStartAngle+20,60);

canvas.drawTextOnPath(textColor[i],path,-10,40,mPaint);

mStartAngle += 60;

}

}

}

自定义CustomTurntableInsideView继承view

package com.bawei.myapplication.turntable;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.view.View;

import com.bawei.myapplication.R;

/**

* 转盘中间开始按钮和指针

* @author hasee

*/

public class CustomTurntableInsideView extends View {

/**

* 画笔

*/

Paint mPaint;

RectF mRectF;

String mStr;

public CustomTurntableInsideView(Context context) {

super(context);

init();

}

public CustomTurntableInsideView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

//自定义属性,如何添加自定义属性如下(考点)

//第一步:在values文件夹下创建attrs.xml

//第二步:详见attrs.xml文件内部

//第三步:在所在的布局文件的根layout中添加xmlns:app="http://schemas.android.com/apk/res-auto"

//第四步:在布局文件的控件中添加app:"你在attrs中设置的attr name"="你的值"

//第五步:调用下面这句话,最后的为R.styleable.你在attrs中设置的declare-styleable name

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTurntableView);

//第六步:调用下面这句话,根据你在attrs中设置的format,选择getXXX方法,

//入参为 R.styleable. 加上 你在attrs中设置的declare-styleable name 加上 _ 加上 你在attrs中设置的attr name

mStr = typedArray.getString(R.styleable.CustomTurntableView_text);

init();

}

private void init() {

//以下注释请看CustomBingView里面

mPaint = new Paint();

mPaint.setColor(Color.RED);

mPaint.setStrokeWidth(10);

mPaint.setTextSize(20);

mPaint.setStyle(Paint.Style.FILL);

mRectF = new RectF();

mRectF.top = 50;

mRectF.bottom = 300;

mRectF.right = 300;

mRectF.left = 200;

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

// setMeasuredDimension(300, 300);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

//设置画笔颜色为黑色,

mPaint.setColor(Color.BLACK);

//画出指针,用一个扇形,然后盖住后面补分来简单表示

canvas.drawArc(mRectF, 60, 60, true, mPaint);

mPaint.setColor(Color.RED);

//画一个红色的圆形,就是中间的大按钮

canvas.drawCircle(250, 250, 50, mPaint);

mPaint.setColor(Color.BLACK);

//添加按钮上的文字

canvas.drawText(mStr, 230, 260, mPaint);

//画三角,第一步,创建路径

// Path path = new Path();

//第二步,moveTo第一个顶点

// path.moveTo(300, 300);

//后续相继lineTo其他顶点

// path.lineTo(300, 400);

// path.lineTo(400, 400);

//闭合

// path.close();

// 画

// canvas.drawPath(path, mPaint);

}

}

自定义属性attrs.xml

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

android自定义抽奖,Android自定义view制作抽奖转盘相关推荐

  1. android伸缩动画自定义,Android干货:自定义带动画的View

    对于一个自定义View来说,onMeasure只是用来计算View尺寸,onDraw()才是真正执行View的绘制,所以一般我们都需要重写onDraw()函数来绘制我们期望的UI界面,下面我以一个具体 ...

  2. Android实现雪花特效自定义view

    一.前言 这个冬天,老家一直没有下雨, 正好圣诞节,就想着制作一个下雪的特效. 圣诞祝福:平安夜,舞翩阡.雪花飘,飞满天.心与心,永相伴. 圣诞节是传统的宗教节日,对于基 督徒,那是庆祝耶稣的诞生,纪 ...

  3. Android 气泡动画(自定义View类)

    Android 气泡动画(自定义View类) 一.前言 二.代码 1. 随机移动的气泡 2.热水气泡 一.前言 最近有需求制作一个水壶的气泡动画,首先在网上查找了一番,找到了一个文章. https:/ ...

  4. android炫酷的自定义view,Android自定义View实现炫酷进度条

    本文实例为大家分享了Android实现炫酷进度条的具体代码,供大家参考,具体内容如下 下面我们来实现如下效果: 第一步:创建attrs文件夹,自定义属性: 第二步:自定义View: /** * Cre ...

  5. android组件什么时候加载到r文件,Android自定义加载loading view动画组件

    我写写使用步骤 自定义view(CircleProgress )的代码 package com.hysmarthotel.view; import com.hysmarthotel.roomcontr ...

  6. android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]

    http://blog.csdn.net/jj120522/article/details/8095852 示意图就不展示了,和上一节的一样,滑动菜单SlidingMenu效果如何大家都比较熟悉,在这 ...

  7. android sqlite自定义函数,Android中自定义一个View的方法详解

    本文实例讲述了Android中自定义一个View的方法.分享给大家供大家参考,具体如下: Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到 ...

  8. Android Paint应用之自定义View实现进度条控件

    在上一篇文章<Android神笔之Paint>学习了Paint的基本用法,但是具体的应用我们还没有实践过.从标题中可知,本文是带领读者使用Paint,自定义一个进度条控件. 上图就是本文要 ...

  9. 【Android 应用开发】自定义View 和 ViewGroup

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...

最新文章

  1. 【Dlib】在GPU环境中运行dlib中的例子dnn_mmod_ex报错...dlib::cuda_error...Error while calling cudaMalloc...
  2. 服!AI 让兵马俑“活”起来,颜值惊艳!
  3. Fragment 源码解析
  4. SD-WAN为企业业务出海提供网络保障
  5. 全球及中国人寿保险产业盈利能力与十四五营销策略咨询报告2022版
  6. 使用C语言中的宏来定位出错信息
  7. C++用参数返回结果与用返回值返回结果的思考
  8. xp系统web服务器搭建教程,Windows_XP配置WEB服务器教程(图)
  9. gitbook新版本 build命令导出的html不能跳转?
  10. .NET或将引入类型类和扩展
  11. 在浏览器中输入url地址 - 显示主页的过程
  12. STM32——HAL版——定时器ms和us延时函数
  13. css网站常用字体,网站常用字体那些事
  14. 启动mongoDB服务
  15. matlab绘制二元二次曲线图,MAtlab 做出二元二次方程的曲线
  16. PTA习题 计算某年某月某日是该年中的第几天
  17. 12306接口协议分析
  18. 51单片机汇编密码锁(可修改密码,课程设计,含论文)!(大三上)
  19. 转:程序员应该怎样去学习和掌握计算机英语呢?
  20. 航信软件里面的虚拟服务器,航天信息网络应用平台

热门文章

  1. SAP 外向交货的包装功能实现
  2. alv导出本地文件DUMP
  3. SAP ABAP逻辑数据库
  4. SAP 常用查看库存的T-CODES
  5. abap 中的语法 div / mod 的用法区别
  6. 腾讯阿里字节扎堆做公益,互联网技术也要“内卷”?
  7. 澳优、伊利、君乐宝、贝因美等入局,羊奶能否迎来“牛市”?
  8. 什么?口红输给口罩了?
  9. python3函数可变输入参量
  10. Python函数的递归调用