自绘式自定义控件_旋转轮盘+ToolBar标题

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        Toolbar toolbar = findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
    }

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

}

activity_main.xml

<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"
    android:orientation="vertical">

   <android.support.v7.widget.Toolbar
       android:id="@+id/toolBar"
       android:background="#8470ff"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:navigationIcon="@mipmap/ic_launcher_round"
       app:subtitle="子标题"
       app:subtitleTextColor="#fff"
       app:title="标题"
       app:titleTextColor="#fff"
       app:popupTheme="@style/ToolbarPopupTheme"
       >


   </android.support.v7.widget.Toolbar>
   <fanruiqi.bwie.com.zhuanpan.DiskView
       app:layout_constraintTop_toBottomOf="@+id/toolBar"
       android:layout_width="400dp"
       android:layout_height="400dp" />

</android.support.constraint.ConstraintLayout>

DiskView.java

public class DiskView extends View implements View.OnClickListener{ //自定义控件类,轮盘操作

    private RotateAnimation rotateAnimation;
    private Paint mPaint;
    private Paint strPaint;
    private int mWidth;
    private int mPadding;
    private boolean isStart = false;
    private RectF rectF;
    private String str="start";
    private String[] contents = new String[]{"美 女","女 神","热 舞","丰 满","性 感","知 性"};

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

    public DiskView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DiskView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
        initAnim();
        setOnClickListener(this);
    }

    private void initPaint() {
        strPaint = new Paint();
        strPaint.setStyle(Paint.Style.STROKE);
        strPaint.setAntiAlias(true);
        strPaint.setColor(Color.WHITE);
        strPaint.setStrokeWidth(5);

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(3);
    }

    private void initAnim() {
        rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setRepeatCount(5);
        rotateAnimation.setFillAfter(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mPadding = 5;
        initRect();
    }

    private void initRect() {
        rectF = new RectF(0,0,mWidth,mWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制圆
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2-mPadding,mPaint);
        //绘制 6个椭圆
        mPaint.setStyle(Paint.Style.FILL);

        initArc(canvas);

        //绘制里面的小圆
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(mWidth/2,mWidth/2,50,mPaint);


        mPaint.setColor(Color.WHITE);
        mPaint.setTextSize(24);
        Rect rect = new Rect();
        mPaint.getTextBounds(str, 0, str.length(), rect);
        int strWidth = rect.width();//文本的宽度
        int textHeight = rect.height();//文本的高度
        canvas.drawText(str,mWidth/2-25+25-strWidth/2,mWidth/2+textHeight/2,mPaint);
    }

    private void initArc(Canvas canvas) {
        for(int i=0;i<6;i++){
            mPaint.setColor(colors[i]);
            canvas.drawArc(rectF,(i-1)*60+60,60,true,mPaint);
        }
        for(int i=0;i<6;i++){
            mPaint.setColor(Color.BLACK);
            Path path = new Path();
            path.addArc(rectF,(i-1)*60+60,60);
            canvas.drawTextOnPath(contents[i],path,60,60,mPaint);
        }
    }
    public  int[] colors = new int[]{Color.parseColor("#8EE5EE"),
            Color.parseColor("#FFD700"),
            Color.parseColor("#FFD39B"),
            Color.parseColor("#FF8247"),
            Color.parseColor("#FF34B3"),
            Color.parseColor("#F0E68C")};

    @Override
    public void onClick(View view) {
        if(!isStart){
            isStart = true;
            rotateAnimation.setDuration(1000);
            rotateAnimation.setInterpolator(new LinearInterpolator());//不停顿
            startAnimation(rotateAnimation);
        }else{
            isStart = false;
            stopAnim();
        }
    }
    public void stopAnim() {
        clearAnimation();
    }
}

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"  //ToolBar设置点击弹出
    >

    <item android:id="@+id/action_settings"
        android:title="settings"
        />
    <item android:id="@+id/action_share"
        android:title="settings"
        />
    <item android:id="@+id/action_search"
        android:title="settings"
        />
    
</menu>

style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">  //toolbar设置字体颜色等
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">#fff</item>
        <item name="colorControlNormal">@android:color/white</item>
    </style>
    <style name="ToolbarPopupTheme"
        parent="@style/ThemeOverlay.AppCompat">
        <item name="android:colorBackground">#c1cdc1</item>
    </style>

猜你喜欢

转载自blog.csdn.net/FanRQ_/article/details/83718698