Android自定义view制作抽奖转盘

 更新时间:2020年07月29日 12:25:26   作者:ren18234073466  
这篇文章主要为大家详细介绍了Android自定义view制作抽奖转盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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;
  }
 });
 }
}

对应的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 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">

 <com.bawei.myapplication.turntable.CustomTurntableView
 android:id="@+id/custom"
 android:layout_width="wrap_content"
 android:layout_height="500dp"
 />

 <com.bawei.myapplication.turntable.CustomTurntableInsideView
 android:id="@+id/custom_inside"
 android:layout_width="wrap_content"
 android:layout_height="500dp"
 app:text="开始"
 android:background="#3300ff00" />
</RelativeLayout>

自定义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

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <!-- name为想要调用这个属性的类名即可 -->

 <declare-styleable name="CustomTurntableView">
 <!-- name为属性的名字,可以随意起,只要符合规则看得懂 -->
 <!-- format为属性内容的类型 -->
 <attr name="text" format="string"></attr>
 </declare-styleable>
</resources>

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

相关文章

  • Android编程实现为应用添加菜单的方法

    Android编程实现为应用添加菜单的方法

    这篇文章主要介绍了Android编程实现为应用添加菜单的方法,涉及Android菜单界面布局与功能实现的相关技巧,需要的朋友可以参考下
    2016-01-01
  • android操作XML的几种方法总结

    android操作XML的几种方法总结

    在android中,操作xml文件,一般有几种方式:SAX操作,Pull操作,DOM操作等。其中DOM的方式,可能是大家最熟悉的,也是符合W3C标准的
    2013-10-10
  • 深入探究Android动态代理的原理及用途

    深入探究Android动态代理的原理及用途

    动态代理是一种在编程中非常有用的设计模式,它允许你在运行时创建一个代理对象来代替原始对象,以便在方法调用前后执行额外的逻辑,本文将深入探讨Android动态代理的原理、用途和实际示例
    2023-09-09
  • Android RecyclerView实现点击条目删除

    Android RecyclerView实现点击条目删除

    这篇文章主要为大家详细介绍了Android RecyclerView实现点击条目删除,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • Android自定义View实现自动吸附功能

    Android自定义View实现自动吸附功能

    这篇文章主要为大家详细介绍了Android自定义View实现自动吸附功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • Android逐帧动画实现代码

    Android逐帧动画实现代码

    这篇文章主要为大家详细介绍了Android逐帧动画实现代码,可以通过xml或java代码实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android系列之Intent传递对象的几种实例方法

    Android系列之Intent传递对象的几种实例方法

    Android系列之Intent传递对象的几种实例方法,需要的朋友可以参考一下
    2013-05-05
  • Android12四大组件之Activity生命周期变化详解

    Android12四大组件之Activity生命周期变化详解

    虽然说我们天天都在使用Activity,但是你真的对Activity的生命机制完全了解了吗?Activity的生命周期方法只有七个,但是其实那只是默认的情况。也就是说在其他情况下,Activity的生命周期可能不会是按照我们以前所知道的流程,本章着重讲解Activity的生命周期变化
    2022-07-07
  • Android开发之StackView用法和遇到的坑分析

    Android开发之StackView用法和遇到的坑分析

    这篇文章主要介绍了Android开发之StackView用法和遇到的坑,结合实例形式分析了Android StackView图片操作用法及常见问题解决方法,需要的朋友可以参考下
    2019-03-03
  • Android实现左上角(其他边角)倾斜的标签(环绕效果)效果

    Android实现左上角(其他边角)倾斜的标签(环绕效果)效果

    这篇文章主要介绍了Android实现左上角(其他边角)倾斜的标签(环绕效果)效果,本文通过图文实例代码相结合的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10

最新评论