toolbar 轮盘抽奖

toolbar 轮盘抽奖_第1张图片

创建自定义控件 Chou

package com.example.lenovo.lunban;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

/**

  • Created by lenovo on 2018-11-4.
    */

public class Chou extends View implements View.OnClickListener{

private int[] color = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
private String[] textColor = new String[]{"美 女","女 神","热 舞","丰 满","性 感","知 性"};
private int mWidthPixels;
private int mHeightPixels;
private int mWidth;
private int mHeight;
private Paint mPaint;
private Animation mAnimation;
private boolean isRote;


public Chou(Context context) {
    this(context,null);
}

public Chou(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs,-1);
}

public Chou(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    mWidthPixels = displayMetrics.widthPixels;
    mHeightPixels = displayMetrics.heightPixels;
    mWidth = mWidthPixels / 2;
    mHeight = mHeightPixels / 2;
    initPaint();
    initAnimation();
    setOnClickListener(this);
}


private void initPaint() {
    mPaint = new Paint();
    mPaint.setColor(Color.RED);
    mPaint.setStrokeWidth(2);
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.FILL);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.translate(mWidth,mHeight);
    RectF rectF = new RectF(-260,-260,260,260);
    float start = 60;
    for (int i = 0;i<6;i++){
        mPaint.setColor(color[i]);
        canvas.drawArc(rectF,start*i,60,true,mPaint);
    }
    mPaint.setColor(Color.RED);
    canvas.drawCircle(0,0,100,mPaint);

    mPaint.setColor(Color.WHITE);
    mPaint.setTextSize(20);

    Rect rect = new Rect();
    mPaint.getTextBounds("start",0,5,rect);
    int width = rect.width();
    int height = rect.height();
    canvas.drawText("start",-width/2,height/2,mPaint);

    RectF rectF1 = new RectF(-200,-200,200,200);
    for (int i = 0;i<6;i++){
        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(20);
        Path path = new Path();
        path.addArc(rectF1,start*i+20,60);
        canvas.drawTextOnPath(textColor[i],path,0,0,mPaint);
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
}
private void initAnimation() {
    mAnimation = new RotateAnimation(0,360,mWidth,mHeight);

    //设置重复次数
    mAnimation.setRepeatCount(-1);

    mAnimation.setFillAfter(true);
    mAnimation.setDuration(400);
    //是Animation的xml的一个属性
    mAnimation.setInterpolator(new LinearInterpolator());
    //设置重复模式
    mAnimation.setRepeatMode(Animation.RESTART);
}

private void start(){
    startAnimation(mAnimation);
    isRote = true;
}

private void stop(){
    isRote = false;
    clearAnimation();
}
@Override
public void onClick(View v) {
    if(isRote){
        stop();

    }else {
        start();
    }
}

// private void radom() {
// double random = Math.random();
// RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (random*360),mWidth,mHeight);
// rotateAnimation.setDuration(400);
// rotateAnimation.setFillAfter(true);
// startAnimation(rotateAnimation);
// }
}

mainactivity页面

package com.example.lenovo.lunban;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
/getSupportActionBar().setTitle(“第一个”);
mToolbar.setTitleTextColor(Color.RED);
mToolbar.setSubtitle(“2”);
mToolbar.setSubtitleTextColor(Color.BLACK);
/
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

@Override
public void onPanelClosed(int featureId, Menu menu) {
    super.onPanelClosed(featureId, menu);
}

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    return super.onMenuOpened(featureId, menu);
}

}

activity_xml布局页面


android:id="@+id/toolbar"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:background="#ff0" >


    
    
    

创建menu

修改values的styles.xml

你可能感兴趣的:(toolbar 轮盘抽奖)