首先是Toolbar的代码
private Toolbar toab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//获取控件
toab = findViewById(R.id.toab);
/* 设置标题
toab.setTitle("标题吗");
toab.setLogo(R.mipmap.ic_launcher_round);*/
//添加一个menu的布局文件
toab.inflateMenu(R.menu.menu);
//设置一个默认点击的图片
toab.setNavigationIcon(R.mipmap.ic_launcher);
//setSupportActionBar(toab);
//为这个menu设置点击事件
toab.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.item1:
Toast.makeText(MainActivity.this,"111", Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(MainActivity.this,"222", Toast.LENGTH_LONG).show();
break;
}
return false;
}
});
toab.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"dianyix", Toast.LENGTH_LONG).show();
}
});
}
//设置menu必须要实现的方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
之后是menu的代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="sdfsdf"
android:id="@+id/item1"
app:showAsAction="never"
></item>
<item
android:title="sdfsdf"
android:id="@+id/item2"
app:showAsAction="never"
></item>
<item
android:title="sdfsdf"
android:id="@+id/item3"
app:showAsAction="ifRoom"
></item>
</menu>
后面是布局文件
<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">
<android.support.v7.widget.Toolbar
android:id="@+id/toab"
android:layout_width="match_parent"
android:background="#987"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入内容"/>
</android.support.v7.widget.Toolbar>
<bwie.com.tobbor.customview.DiscView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
最后是自定义View
private Paint mPaint;
private RectF mRectF;
private RectF mRectF1;
private RotateAnimation mRotateAnimatio;
private int mx;
private int my;
private boolean flag;
int color[]={Color.GREEN,Color.RED,Color.BLUE,Color.BLACK,Color.GRAY,Color.YELLOW};
String text[]={"666","777","999","555","4545","464"};
public DiscView(Context context) {
this(context,null);
}
public DiscView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public DiscView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取屏幕信息
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
//获取屏幕宽高
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
mx=width /2;
my=height /2;
initpaint();
initAnim();
this.setOnClickListener(this);
}
private void initpaint() {
mPaint= new Paint();
//设置画笔的宽度
mPaint.setStrokeWidth(5);
//设置画笔的颜色
mPaint.setColor(Color.RED);
//设置抗锯齿
mPaint.setAntiAlias(true);
/**
* Paint.Style.STROKE 只绘制图形轮廓(描边)
* Paint.Style.FILL 只绘制图形内容
* Paint.Style.FILL_AND_STROKE 既绘制轮廓也绘制内容
*/
mPaint.setStyle(Paint.Style.FILL);
}
private void initAnim() {
//设置补间动画
mRotateAnimatio = new RotateAnimation(0,360,mx,my);
mRotateAnimatio.setRepeatCount(-1);
mRotateAnimatio.setFillAfter(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置圆点
canvas.translate(mx,my);
//设置矩形
mRectF = new RectF(-200,-200,200,200);
//设置弧
for (int i = 0; i < 6; i++) {
//给弧设置颜色
mPaint.setColor(color[i]);
//1、矩形 2、每次开始的度数 3、每个弧的度数 4、设置半径 5、设置画笔
canvas.drawArc(mRectF,360/6*i,60,true,mPaint);
}
//设置弧的展示字体的矩形
mRectF1 = new RectF(-100,-100,100,100);
//设置字体的大小
mPaint.setTextSize(40);
for (int i = 0; i <6 ; i++) {
//设置字体的颜色
mPaint.setColor(Color.WHITE);
//设置字体展示的位置
Path path = new Path();
path.addArc(mRectF1,360/6*i+15,60);
//设置具体展示的字体
/**
* 参数2:路径
* 参数3:距离路径开始位置的偏移量
* 参数4:距离路径上下的偏移量(可以为负数)
* 参数5:画笔对象
*/
canvas.drawTextOnPath(text[i],path,0,0,mPaint);
}
mPaint.setColor(Color.WHITE);
//1,2:坐标 3、半径 4、画笔
canvas.drawCircle(0,0,50,mPaint);
//设置中心的字体
Rect rect = new Rect();
mPaint.setColor(Color.BLACK);
mPaint.getTextBounds("start",0,5,rect);
//获取设置字体的宽高
int width = rect.width();
int height = rect.height();
//字体位置居中
canvas.drawText("start",-width/2,height/2,mPaint);
}
//停止方法
private void stopView(){
flag=false;
clearAnimation();
}
//开始方法
private void startView(){
flag=true;
//设置延迟
mRotateAnimatio.setDuration(1000);
mRotateAnimatio.setInterpolator(new LinearInterpolator());
startAnimation(mRotateAnimatio);
}
@Override
public void onClick(View view) {
if (flag){
stopView();
}else {
startView();
}
}