android 代码绘制转盘抽奖的实现

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;

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.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);
			} else
				mPaint.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;
	
	
	@Override
	protected 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() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int 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() {
					
					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub 13591774480				
					}
					
					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						int a=8-s/45;
						Toast.makeText(getApplicationContext(), "恭喜您"+a+"等奖"+s, Toast.LENGTH_SHORT).show();
					}
				});
				
				
				
			}
		});
		
		
		
		
		
		
	}

	@Override
	public 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;
	}

	@Override
	public 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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值