package com.example.ceshi;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;public class MyViwe extends View implements OnClickListener{
private Paint _paintPaint;
private int count;public MyViwe(Context context, AttributeSet attrs) {
super(context, attrs);
_paintPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
count = 0;
setOnClickListener( this);
}@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(count<5){
_paintPaint.setColor(Color.BLUE);
}else if(count<10){
_paintPaint.setColor(Color.GRAY);
}else if(count<15){
_paintPaint.setColor(Color.YELLOW);
}else{
_paintPaint.setColor(Color.CYAN);
Toast.makeText(getContext(), "呵呵", 1).show();
}canvas.drawRect(0, 0, getWidth(), getHeight(), _paintPaint);
_paintPaint.setTextSize(20);
_paintPaint.setColor(Color.RED);
String string = "你好!李浩"+ count;
canvas.drawText(string, 0, getHeight()/2, _paintPaint);}
@Override
public void onClick(View v) {
count++;
//重新加载
invalidate();
}}
自定义组件