抽奖和圆环

1.MainActivity
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

2.MyLuckView
public class MyLuckView extends View implements View.OnClickListener {

private String[] contents = new String[]{"美 女", "女 神", "热 舞", "丰 满", "性 感", "知 性", "男 神", "感 性"};
public int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700")};
private Context mContext;
private Paint mPaint;
private int mWidth;
private String mStr = "start";
private int start=0;


//第二个参数:属性值的集合
public MyLuckView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    //画笔
    mPaint = new Paint();
    //监听事件
    setOnClickListener(this);

}

//绘画
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

/* mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
//设置边缘锯齿
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(2);
Log.e(“width”, mWidth + “”);
canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mPaint);*/

    //因为要画扇形 里面有个RectF
    //因为那个园其实是占全屏的,所以我这个RectF的空间也是全屏
    RectF rectF = new RectF(0, 0, mWidth, mWidth);


    mPaint.setStyle(Paint.Style.FILL);
    for (int i = 0; i < colors.length; i++) {
        mPaint.setColor(colors[i]);
        //起始角度
        int startjd = i * 45;
        canvas.drawArc(rectF, startjd, 45, true, mPaint);
    }


    //循环画字
    //字体的颜色
    mPaint.setColor(Color.BLACK);
    //字体大小
    mPaint.setTextSize(24);
    //循环添加
    for (int i = 0; i < contents.length; i++) {
        int startjd = i * 45;
        //Path 代表路径 想怎么画就怎么画
        Path path = new Path();
        path.addArc(rectF, startjd, 45);
        // 循环画字 路径  30代表左右  50代表上下  画笔
        canvas.drawTextOnPath(contents[i], path, 30, 50, mPaint);
    }

    /*//内圆
    mPaint.setColor(Color.GREEN);
    //确定圆心点  半径
    canvas.drawCircle(mWidth / 2, mWidth / 2, 50, mPaint);
    */



    //在最中心的位置画一个start (150,150) 我们要得到我们写的字的高和宽
  /* mPaint.setColor(Color.BLACK);
    mPaint.setTextSize(24);
    Rect rect = new Rect();
    mPaint.getTextBounds(mStr, 0, mStr.length(), rect);
    int width = rect.width();
    int height = rect.height();
    canvas.drawText(mStr, mWidth / 2 - width / 2, mWidth / 2 + height / 2, mPaint);*/


}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(300, 300);
    //得到测量过后的高和宽
    mWidth = getMeasuredWidth();

}

@Override
public void onClick(View v) {
    // Toast.makeText(mContext, "memeda", Toast.LENGTH_SHORT).show();
    //随机数
    Random random = new Random();
    int du = random.nextInt(1000);
    RotateAnimation rotateAnimation = new RotateAnimation(start, du+360, mWidth / 2, mWidth / 2);
    rotateAnimation.setDuration(1000);
    //保留最后执行完的位置
    rotateAnimation.setFillAfter(true);
    startAnimation(rotateAnimation);

     start =du%360;

}

}

3.布局
</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:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">

<com.bawei.wangyaxiao.choujiang.weight.MyLuckView
android:layout_centerInParent=“true”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
</ImageView
android:layout_width=“80dp”
android:layout_height=“80dp”
android:layout_centerInParent=“true”
android:src="@mipmap/abc"/>

<//RelativeLayout>

2222222222222圆环
1.MainActivity
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

2.主布局
<android.support.constraint.ConstraintLayout 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:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">

<com.bawei.wangyaxiao.yuan.demo.MyView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
app:startAngle=“30”
app:sweepAngle=“240”
app:text=“hehe”
app:textSize=“30sp”
app:textColor="@color/colorPrimaryDark"
app:circleColor="@color/colorPrimary"
app:arcColor="@color/colorAccent"
/>

</android.support.constraint.ConstraintLayout>

3.MyView
public class MyView extends View {

private  int circleColor;
private  int textColor;
private  int arcColor;
private  float textSize;
private  int sweepAngle;
private  int startAngle;
private  String text;
private int mCircleXY;   //注意这个是int类型
private float mRadius;
private Paint mCirclePaint;
private RectF mRectF;
private Paint mArcPaint;
private Paint mTextPaint;
                                //属性值的集合
public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    //初始化的时候获取自定义的属性,获取的是主布局中自己赋给自定义属性的值,第二个参数是如果你没有赋值,就是用默认值。

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);
    if(ta!=null){
        circleColor =ta.getColor(R.styleable.MyView_circleColor,0);
        arcColor =ta.getColor(R.styleable.MyView_arcColor,0);
        textColor =ta.getColor(R.styleable.MyView_textColor,0);
        textSize =ta.getDimension(R.styleable.MyView_textSize,0);
        text =ta.getString(R.styleable.MyView_text);
        startAngle =ta.getInt(R.styleable.MyView_startAngle,0);
        sweepAngle =ta.getInt(R.styleable.MyView_sweepAngle,90);
        ta.recycle();//释放资源
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    drawSth(canvas);
}

private void drawSth(Canvas canvas){

    //初始化方法
    init();
    //画一个圆形,第一个是参数是圆形的x坐标,第二个参数是圆形的y坐标,第三个是圆的半径,第四个是画笔。
    canvas.drawCircle(mCircleXY, mCircleXY, mRadius, mCirclePaint);
    //绘制一个扇形,第一个参数是确定外切矩形的范围,(即位置和大小),第二个是扇形的起始角度,第三个是结束角度,第四个(false)是代表空心扇形,true代表充满的扇形,最后一个是画笔
    canvas.drawArc(mRectF,startAngle,sweepAngle,false, mArcPaint);
    //绘制一个字符串,第一个字符串是自定义属性自己赋值的字符串,第二个字符串的x轴位置,第三个是文字的y轴位置,最后是画笔。

    canvas.drawText(text, mCircleXY, mCircleXY +textSize/4, mTextPaint);

}

private void init(){

    //获取当前控件的宽和高
    int length=Math.min(getWidth(),getHeight());
    //得到当前控件一半的大小
    mCircleXY=length/2;

    mRadius=length*0.5f/2;
    //设置画笔的抗锯齿
    mCirclePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    mCirclePaint.setColor(circleColor);
    mRectF=new RectF(length*0.1f,length*0.1f,length*0.9f,length*0.9f);

    mArcPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    mArcPaint.setColor(arcColor);
    //设置画笔风格(空心或实心)
    mArcPaint.setStyle(Paint.Style.STROKE);
    //设置空心边框的宽度
    mArcPaint.setStrokeWidth((getWidth()*0.1f));


    mTextPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setTextSize(textSize);
    mTextPaint.setColor(textColor);
    //设置文本居中
    mTextPaint.setTextAlign(Paint.Align.CENTER);

}

}

4.values下的attrs.xml
<//resources>
<!/–给自定义控件设置自定义属性–>
</!–name就是自己起的名字,可以是a,是b,但最好见名知意,format的意思就是属性类型的意思–>

</attr name=“textSize” format=“dimension”/>
</attr name=“text” format=“string”/>
</attr name=“circleColor” format=“color”/>
</attr name=“arcColor” format=“color”/>
</attr name=“textColor” format=“color”/>
</attr name=“startAngle” format=“integer” />
</attr name=“sweepAngle” format=“integer” />

</declare-styleable>

/!–
Ⅰ、textSize——对应中间文本文字的大小
  Ⅱ、text——对应中间文本
  Ⅲ、circleColor——对应内圆的颜色
  Ⅳ、arcColor——对应外环的颜色
  Ⅴ、textColor——对应文本的颜色
  Ⅵ、startAngle——对应外环的起始角度
  Ⅶ、sweepAngle——对应外环扫描角度
–>

<//resources>

猜你喜欢

转载自blog.csdn.net/qq_43603325/article/details/84726936