自定义View
先自己写一个view的类,继承view,写出它的构造器,一共有四个,一般写2到3个,然后复写onMeasure和onDraw方法,onMeasure是用于测量宽高。而onDraw是有UI主线程自动调用,只需在此绘制图形即可,onDraw中写canvas(画布)。
这是一个时钟的程序
package com.example.administrator.mywidgetdemo.MyView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import java.util.Calendar;
/**
* Created by Administrator on 2015/9/16.
*/
public class MyView extends View {
private int width;
private int height;
private Paint mPaintLine;
private Paint mPaintCircle;
private Paint mPaintText;
private Calendar mCalendar;//定义一个日历变量
public static final int NEED_INVALIDATE = 0X23;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case NEED_INVALIDATE:
mCalendar = Calendar.getInstance();//日历实例化
invalidate();//告诉UI主线程重新绘制
handler.sendEmptyMessageDelayed(NEED_INVALIDATE,1000);
break;
}
}
};
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mCalendar = Calendar.getInstance();
mPaintLine = new Paint();
mPaintLine.setColor(Color.RED);//设置线的颜色
mPaintLine.setStrokeWidth(10);//设置线的宽度
mPaintLine.setAntiAlias(true);//设置线抗锯齿
mPaintCircle = new Paint();
mPaintCircle.setColor(Color.GREEN);
mPaintCircle.setStrokeWidth(10);
mPaintCircle.setStyle(Paint.Style.STROKE);
mPaintCircle.setAntiAlias(true);
mPaintText = new Paint();
mPaintText.setColor(Color.BLACK);
mPaintText.setTextSize(30);
mPaintText.setTextAlign(Paint.Align.CENTER);
handler.sendEmptyMessageDelayed(NEED_INVALIDATE,2000);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width,height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawLine(0,0,300,300,mPaintLine);
canvas.drawCircle(width / 2, height / 2, 300, mPaintCircle);
canvas.drawCircle(width / 2, height / 2, 10, mPaintCircle);
for (int i = 1;i<=12;i++){
canvas.rotate(30,width/2,height/2);
canvas.drawLine(width / 2, height / 2 - 300, width / 2, height / 2 - 280, mPaintLine);
canvas.drawText("" + i, width / 2, height / 2 - 250, mPaintText);
}
int minute = mCalendar.get(Calendar.MINUTE);
int hour = mCalendar.get(Calendar.HOUR);
int second = mCalendar.get(Calendar.SECOND);
float degree = minute/60f*360;
canvas.save();
canvas.rotate(degree, width / 2, height / 2);
canvas.drawLine(width / 2, height / 2 - 200, width / 2, height / 2 + 30, mPaintLine);
canvas.restore();
float hourDegree = (hour*60+minute)/12f/60*360;
canvas.save();
canvas.rotate(hourDegree,width/2,height/2);
canvas.drawLine(width / 2, height / 2 - 150, width / 2, height / 2 +20, mPaintLine);
canvas.restore();
float secondDegree = 6*second;
canvas.save();
canvas.rotate(secondDegree,width/2,height/2);
canvas.drawLine(width / 2, height / 2 - 220, width / 2, height / 2 +20, mPaintLine);
canvas.restore();
}
}
注意布局中要写包名的全称
<com.example.administrator.mywidgetdemo.MyView.MyView
android:layout_width="match_parent"
android:layout_height="match_parent" />
模拟下载的圆形进度条
package com.example.administrator.mywidgetdemo2.Widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Administrator on 2015/9/16.
*/
public class MyProgressArc extends View {
private int width;
private int height;
private Paint mPaintArc;
private Paint mPaintCircle1;
private Paint mPaintCircle2;
private Paint mPaintText;
private int currentProgress;
public int getCurrentProgress() {
return currentProgress;
}
public void setCurrentProgress(int currentProgress) {
this.currentProgress = currentProgress;
invalidate();
}
public MyProgressArc(Context context) {
super(context);
}
public MyProgressArc(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintArc = new Paint();
mPaintArc.setColor(Color.BLUE);
mPaintArc.setStrokeWidth(50);
mPaintArc.setAntiAlias(true);
mPaintArc.setStyle(Paint.Style.STROKE);
mPaintCircle1 = new Paint();
mPaintCircle1.setColor(Color.GRAY);
mPaintCircle1.setStrokeWidth(1);
mPaintCircle1.setStyle(Paint.Style.STROKE);
mPaintCircle1.setAntiAlias(true);
mPaintCircle2 = new Paint();
mPaintCircle2.setColor(Color.GRAY);
mPaintCircle2.setStrokeWidth(1);
mPaintCircle2.setStyle(Paint.Style.STROKE);
mPaintCircle2.setAntiAlias(true);
mPaintText = new Paint();
mPaintText.setColor(Color.BLACK);
mPaintText.setTextSize(100);
mPaintText.setTextAlign(Paint.Align.CENTER);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(width / 2, height / 2, 200, mPaintCircle1);
canvas.drawCircle(width/2,height/2,150,mPaintCircle2);
canvas.drawText(currentProgress + "%", width / 2, height / 2, mPaintText);
RectF oval = new RectF();
oval.set(width/2-175,height/2-175,width/2+175,height/2+175);
canvas.drawArc(oval,0,360*currentProgress/100,false,mPaintArc);
}
}
package com.example.administrator.mywidgetdemo2;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import com.example.administrator.mywidgetdemo2.Widget.MyProgress;
import com.example.administrator.mywidgetdemo2.Widget.MyProgressArc;
/**
* Created by Administrator on 2015/9/16.
*/
public class ProgressArcActivity extends Activity {
private MyProgressArc myProgressArc;
public static final int PROGRESS=0X23;
private Button mButtonStart;
private int progress = 0;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case PROGRESS:
progress++;
if (progress<=100){
myProgressArc.setCurrentProgress(progress);
handler.sendEmptyMessageDelayed(PROGRESS,200);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arc);
myProgressArc = (MyProgressArc) findViewById(R.id.progress_arc);
mButtonStart = (Button) findViewById(R.id.button_start_arc);
mButtonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.sendEmptyMessageDelayed(PROGRESS, 1000);
}
});
}
}