您的位置:首页 > 其它

自定义View关于画圆弧的一些笔记

2017-10-10 13:51 274 查看
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
paint.setAntiAlias(true);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(300,300,150,paint);
canvas.drawCircle(300,300,130,paint);

paint.setStrokeWidth(20);
RectF oval=new RectF(160,160,440,440);
canvas.drawArc(oval,0,90,false,paint);
}
1.首先这个300,300是圆的圆心,但是到了模拟器上并不一定会显示出300dp的长,涉及到屏幕适配等等.
2.这里的画圆弧Rectf 的 160 , 160   是通过  圆心X轴300-半径150 +  (150-130)/2=160
440 , 440  是通过圆心Y轴300+(150+130)/2=440;
接来下是关于画一个动态的
1.View
public class MyView extends View {

private Paint paint;
private float progress;
private int countProgress;

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

public MyView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
paint.setAntiAlias(true);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStrokeWidth(0);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(300,300,150,paint);
canvas.drawCircle(300,300,130,paint);
paint.setTextSize(30);

canvas.drawText(countProgress+"%",280,300,paint);

paint.setStrokeWidth(20);
RectF oval=new RectF(160,160,440,440);
canvas.drawArc(oval,270,progress,false,paint);
}

public void setProgress(float progress) {
this.progress = progress;
countProgress= (int) (progress*100/360);
invalidate();
}

public float getProgress() {
return progress;
}
}
2.MainXML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation=
ccca
"vertical"
tools:context="com.bawei.day1010.MainActivity">

<com.bawei.day1010.MyView
android:id="@+id/myview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/butStart"
android:text="开始绘制圆弧"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginBottom="132dp"/>
</RelativeLayout>
3.MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mMyview.setProgress(mMyview.getProgress() + 1);
}
};
private MyView mMyview;
/**
* 开始绘制圆弧
*/
private Button mButStart;

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

private void initView() {
mMyview = (MyView) findViewById(R.id.myview);
mButStart = (Button) findViewById(R.id.butStart);
mButStart.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.butStart:
new Thread() {
@Override
public void run() {
for (int i = 0; i <= 360; i++) {
//360°  30s  那么 1s12°  即  1°  1/12S
SystemClock.sleep(1000 / 12);
handler.sendEmptyMessage(0);
}
}
}.start();
break;
}
}
}
4.shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
方法2:
new AsyncTask<String, Integer, String>() {@Overrideprotected String doInBackground(String... params) {for (int i = 0; i <= 360; i++) {SystemClock.sleep(100 / 12);publishProgress(i);}return null;}@Overrideprotected void onProgressUpdate(Integer... values) {if (mMyview.getProgress() >= 360) {mMyview.setProgress(360);}mMyview.setProgress(values[0]);}}.execute();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: