1. 程式人生 > >抽獎和圓環

抽獎和圓環

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>