android 代码绘制转盘抽奖的实现
来源:互联网 发布:js字符串中的双引号 编辑:程序博客网 时间:2024/05/17 00:09
android 代码绘制转盘抽奖的实现
先上图
第一个是 整体的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.animationtest.MyView android:id="@+id/myview" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="抽奖" android:layout_centerInParent="true" /></RelativeLayout>第二个是 自定义View
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.util.AttributeSet;import android.view.View;public class MyView extends View {public MyView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}private Paint mPaint;@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);mPaint = new Paint();// 必选设定画笔属性mPaint.setAntiAlias(false);// 抗锯齿// 获取视图 宽和 高 便于 确定中心点int w = getWidth();int h = getHeight();// 画 外边黄圆圈mPaint.setColor(Color.YELLOW);mPaint.setStyle(Paint.Style.STROKE);// 空心mPaint.setStrokeWidth(30.0f);// 画笔粗canvas.drawCircle(w / 2, h / 2, 320, mPaint);// 半径 350// 第2个圈mPaint.setARGB(255, 181, 28, 24);// 第一个参数是透明度 0是透明255 不透明mPaint.setStrokeWidth(40.0f);// 画笔粗canvas.drawCircle(w / 2, h / 2, 290, mPaint);// 半径 290// 里边的实心小圆 分24分 15°一份mPaint.setStyle(Paint.Style.FILL);mPaint.setColor(Color.YELLOW);canvas.drawCircle(w / 2, h / 2 - 290, 10, mPaint);for (int i = 0; i < 24; i++) {canvas.save();canvas.rotate(15 * i, w / 2, h / 2);if (i % 2 == 0) {mPaint.setColor(Color.BLUE);} elsemPaint.setColor(Color.YELLOW);canvas.drawCircle(w / 2, h / 2 - 290, 10, mPaint);canvas.restore();}RectF rectF3 = new RectF(w / 2 - 270, h / 2 - 270, w / 2 + 270,h / 2 + 270);// 外切红圆圈的半径减去画笔一半for (int i = 0; i < 8; i++) {if (i%2==0) mPaint.setColor(Color.GREEN);else mPaint.setColor(Color.YELLOW);canvas.drawArc(rectF3, 45*i, 45, true, mPaint);mPaint.setColor(Color.BLACK);}// 扇形// 画弧线 参数 外切矩形 起始角度 终角度 是否封闭//扇形上边写字 // 写一个转八次Path path = new Path();path.moveTo(w/2, h/2);path.lineTo(w/2-100, w / 2 - 270);path.close();mPaint.setTextSize(42);// 沿路径绘制图片for (int j = 1; j < 9; j++) {canvas.save();canvas.rotate(45 * j, w / 2, h / 2);canvas.drawTextOnPath(j+"等奖", path, 80, 0, mPaint);canvas.restore();}//画箭头 canvas.restore();mPaint.setStyle(Paint.Style.FILL);// 实心mPaint.setStrokeWidth(8);//mPaint.setColor(Color.RED);Path path1 = new Path();path1.moveTo(w/2-40, h/2);path1.lineTo(w/2+30, h/2);path1.lineTo(w/2, h/2-120);path1.close();canvas.drawPath(path1, mPaint);// 最里层 实心园mPaint.setColor(Color.CYAN);mPaint.setStrokeWidth(2f);//canvas.drawCircle(w / 2, h / 2, 60, mPaint);}}
最后一个Java文件 是第一个布局对应的。获取结果
import java.util.Random;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.RotateAnimation;import android.widget.TextView;import android.widget.Toast;public class ChouActivity extends Activity {TextView star;MyView mView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_chou);star=(TextView) findViewById(R.id.start);mView=(MyView) findViewById(R.id.myview);star.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint min=1,max=359; final int s=(new Random()).nextInt(max)%(max-min-1)+min;RotateAnimation rotate = new RotateAnimation(0, 720+s,Animation.ABSOLUTE, mView.getWidth() / 2,Animation.ABSOLUTE, mView.getHeight() / 2);rotate.setDuration(1500);rotate.setFillAfter(true);//rotate.setInterpolator(new LinearInterpolator());mView.startAnimation(rotate);rotate.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub 13591774480}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubint a=8-s/45;Toast.makeText(getApplicationContext(), "恭喜您"+a+"等奖"+s, Toast.LENGTH_SHORT).show();}});}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.chou, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
算是自己写的比较完善的小DEMO吧。
加油
1 0
- android 代码绘制转盘抽奖的实现
- Android 抽奖转盘的实现
- Android抽奖转盘的实现
- Android抽奖转盘代码
- Android实现抽奖转盘
- Android——抽奖转盘的实现
- Android实现抽奖转盘动画
- 抽奖转盘的jquery实现
- 实现转盘抽奖的SurfaceViewDemo
- Js实现简易大转盘抽奖代码
- jQuery实现旋转转盘抽奖代码
- Canvas实现微信大转盘抽奖代码
- Android 抽奖 转盘 动画 实现原理详解
- jquery实现转盘抽奖--基于奖品转动的转盘
- ios 抽奖大转盘的实现
- java实现转盘抽奖;
- SurfaceView 实现 转盘抽奖
- SurfaceView 实现 转盘抽奖
- 【SQlite关系型数据库】SQLiteOpenHelper
- 计算机视觉、机器学习相关领域论文和源代码大集合--持续更新……
- 自定义导航控制器,统一设置Push的时候隐藏底部的tabbar
- iOS——个人公司型开发者账号申请流程(含免费邓白氏码申请流程)
- 《王保明老师----Linux开发学习笔记》------讲03:进程 -----fork( )函数 的疑问
- android 代码绘制转盘抽奖的实现
- Ubuntu安装ssh
- http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html
- 抽象类设计计算二维平面图形面积
- 二叉搜索树的后序遍历
- 简单的Python爬虫程序
- 对CMMI的认识
- Excel2016绘图新功能
- 树莓派装系统+远程登录