activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.ch.view.DrawView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
MainActivity.class:
package com.ch.onedot; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
com.ch.view.DrawView:
package com.ch.view; import com.example.day4_two.R; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class DrawView extends View{ float currentX=70; float currentY=90; Paint paint=new Paint(); Paint paint1=new Paint(); /** * 圆环的颜色 */ private int roundColor; /** * 圆环进度的颜色 */ private int roundProgressColor; public DrawView(Context context) { super(context); // TODO Auto-generated constructor stub } public DrawView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub //自定义RoundProgressBar TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar); //获取自定义属性和默认值 roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED); roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN); //重复利用 mTypedArray.recycle(); } public DrawView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //自定义RoundProgressBar TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar); //获取自定义属性和默认值 roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED); roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN); //重复利用 mTypedArray.recycle(); } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub //设置空心 paint.setStyle(Paint.Style.STROKE); //消除锯齿 paint.setAntiAlias(true); paint.setStrokeWidth(10); /* paint.setColor(Color.RED); paint1.setColor(Color.BLUE); paint1.setTextSize(30); canvas.drawCircle(currentX, currentY, 50, paint);//小圆 canvas.drawCircle(currentX, currentY, 70, paint);//大圆 canvas.drawLine(currentX, currentY, currentX, currentY+70, paint); canvas.drawText("你好", currentX-32, currentY+18, paint1);*/ paint.setColor(roundColor); canvas.drawCircle(currentX, currentY, 50, paint); RectF oval = new RectF(currentX - 75, currentY - 75, currentX + 75, currentY + 75); //用于定义的圆弧的形状和大小的界限 paint1.setStrokeWidth(10); //设置空心 //paint1.setStyle(Paint.Style.STROKE); //消除锯齿 paint1.setAntiAlias(true); paint1.setColor(roundProgressColor); canvas.drawArc(oval, 10,(float) (Math.random()* 360), true, paint1); //字体 super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub /*currentX=(float) (Math.random()*event.getX()); currentY=(float) (Math.random()*event.getY());*/ currentX=(float) (Math.random()*getWidth()); currentY=(float) (Math.random()*getHeight()); invalidate(); return true; } }
attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <!--完成进度条--> <declare-styleable name="RoundProgressBar" > <attr name="roundColor" format="color"/> <attr name="roundProgressColor" format="color"/> <attr name="roundWidth" format="dimension"></attr> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable> </resources>