xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/relative"
tools:context="com.example.lianxi2.MainActivity">
<com.example.lianxi2.View.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Activity:
package com.example.lianxi2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.example.lianxi2.View.MyView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = findViewById(R.id.relative);
MyView myView = new MyView(this);
relativeLayout.addView(myView);
}
}
MyView:
package com.example.lianxi2.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by 你家大林哥 on 2018/4/12.
*/
public class MyView extends View implements View.OnClickListener {
private Paint paint;
private Paint paintB;
private Paint pai;
private int[] colors;
private int stepAngle;
private RectF rect;
private int width;
private int height;
private int centerX;
private int centerY;
public MyView(Context context) {
this(context,null);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
setOnClickListener(this);
paint = new Paint();
paint.setColor(Color.RED);
paintB = new Paint();
paintB.setColor(Color.BLUE);
pai = new Paint();
colors = new int[]{Color.BLUE,Color.RED,Color.YELLOW,Color.CYAN,Color.BLACK,Color.GREEN,Color.BLUE,Color.RED,Color.YELLOW,Color.CYAN,Color.BLACK,Color.GREEN};
stepAngle = 360/colors.length;
rect = new RectF(-200,-200,200,200);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
centerX = width/2;
centerY = height/2;
Log.e("myMessage","onMeasure");
}
private Canvas mycanvas;
int dd = 60;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mycanvas = canvas;
canvas.translate(centerX,centerY);
canvas.rotate(d);
drawCircleWithColor(canvas);
Log.e("myMessage","onDraw");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.e("myMessage","ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e("myMessage","ACTION_MOVE");
dd+=10;
postInvalidate();
break;
case MotionEvent.ACTION_UP:
Log.e("myMessage","ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
private void drawCircleWithColor(Canvas canvas) {
int angleStart = 0 ;
for (int i = 0; i < colors.length;i++){
int c = colors[i];
pai.setColor(c);
canvas.drawArc(rect,angleStart,stepAngle,true,pai);
angleStart += stepAngle;
}
}
int d = 60;
@Override
public void onClick(View v) {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
d += 5 ;
postInvalidate();
}
};
Timer timer = new Timer();
timer.schedule(timerTask,2000,50);
}
}
Android 扇形圆 点击转动
最新推荐文章于 2018-10-22 15:30:43 发布