xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:id="@+id/toolbar">
</android.support.v7.widget.Toolbar>
<com.bwie.a20181104_a.Haha
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_weight=“1”/>
MainActivity.this
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);
//设置之后 toolbar 能取代原先的Actionbar
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(true);
//决定上边的图标是否可以点击
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 void onPanelClosed(int featureId, Menu menu) {
super.onPanelClosed(featureId, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
return super.onMenuOpened(featureId, menu);
}
}
haha.this
public class Haha extends View implements View.OnClickListener {
//转盘颜色
private int[] color = new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW,Color.GRAY,Color.WHITE};
//转盘内容
private String[] textColor = new String[]{"11111","22222","33333","44444","55555","66666"};
//屏幕的中心宽高
private int mWidth;
private int mHeight;
private Paint mPaint;
private Animation mAnimation;
private boolean isRote;
public Haha(Context context) {
this(context,null);
}
public Haha(Context context,AttributeSet attrs) {
this(context, attrs,-1);
}
public Haha(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取屏幕
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
//获取屏幕的宽和高
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
//获取屏幕宽高的一半(以便获取屏幕的中心点)
mWidth = widthPixels / 2;
mHeight = heightPixels / 2;
//初始化画笔Paint
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 onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//首先平移到中间
canvas.translate(mWidth,mHeight);
//外边的大圆
RectF rectF = new RectF(-260,-260,260,260);
//定义每个的大小
float start = 60;
//循环 分为6个
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(25);
//创建
Rect rect = new Rect();
//输入字 中间的字
mPaint.getTextBounds("srart",0,5,rect);
//定义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.BLACK);
//设置颜色的大小
mPaint.setTextSize(25);
//创建一个Path
Path path = new Path();
//把文字设置进去
path.addArc(rectF1,start*i+20,60);
//放着圆盘里边
canvas.drawTextOnPath(textColor[i],path,0,0,mPaint);
}
}
/**
* 动画
*/
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(){
isRote = true;
//开始旋转
startAnimation(mAnimation);
}
private void stop(){
isRote = false;
//停止旋转
clearAnimation();
}
//点击事件
@Override
public void onClick(View v) {
if (isRote){
//如果为true
stop();
}else {
start();
}
}
}
创建一个menu
<item
android:icon="@mipmap/ic_launcher"
app:showAsAction="always"
android:title="嘻嘻"></item>
<item
android:icon="@mipmap/ic_launcher"
app:showAsAction="never"
android:title="哈哈"></item>
- 记得要把styles.xml里边
- 把这个 Theme.AppCompat.Light.DarkActionBar
- 改成 Theme.AppCompat.Light.NoActionBar