自己写的一个小view,使用RadialGradient画的渐变圆,点击后可缩放。附上代码:
public class SweepGradientView extends View {
Paint mPaint = null;
// 梯度渲染
Shader mRadialGradient = null;
private int x = 150;
private int y = 150;
private int radius = 100;
private boolean suoanimFlag = false;//缩小的布尔值
private boolean fanganimFlag = false;//是否放大的布尔值
private Handler myhandler = new Handler();
RunSuoXiaoAnimRunnable suoAnim;
RunFangDaAnimRunnable fangAnim;
private int round = 1;// 1为放大到120,2为缩小到90,3为放大到100
private int jiange = 10;//动画间隔时间
public SweepGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
suoAnim = new RunSuoXiaoAnimRunnable();
fangAnim = new RunFangDaAnimRunnable();
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// mSweepGradient = new SweepGradient(240, 200, new int[] { Color.CYAN,
// Color.DKGRAY, Color.GRAY, Color.LTGRAY, Color.MAGENTA,
// Color.GREEN, Color.TRANSPARENT, Color.BLUE }, null);
mRadialGradient = new RadialGradient(x, y, radius, new int[] {
Color.GREEN, Color.RED, Color.BLUE, Color.WHITE, Color.BLACK },
null, Shader.TileMode.REPEAT);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(mRadialGradient);
// mPaint.setStyle(Paint.Style.STROKE);
// mPaint.setStrokeWidth(30);
canvas.drawCircle(x, y, radius, mPaint);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(fanganimFlag){
break;
}
suoanimFlag = true;
if (!fanganimFlag) {
myhandler.postDelayed(suoAnim, jiange);
}
break;
case MotionEvent.ACTION_UP:
fanganimFlag = true;
if (!suoanimFlag) {
myhandler.postDelayed(fangAnim, jiange);
}
break;
default:
break;
}
return true;
}
class RunSuoXiaoAnimRunnable implements Runnable {
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
if (!suoanimFlag) {
if (fanganimFlag) {
myhandler.postDelayed(fangAnim, jiange);
}
return;
}
radius -= 5;
if (radius <= 30) {
suoanimFlag = false;
}
invalidate();
myhandler.postDelayed(suoAnim, jiange);
}
}
class RunFangDaAnimRunnable implements Runnable {
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
if (!fanganimFlag) {
if (suoanimFlag) {
myhandler.postDelayed(suoAnim, jiange);
}
return;
}
if (round == 1) {
radius += 5;
if (radius >= 120) {
round = 2;
}
invalidate();
} else if (round == 2) {
radius -= 5;
if (radius <= 90) {
round = 3;
}
invalidate();
} else if (round == 3) {
radius += 5;
if (radius >= 100) {
round = 1;
fanganimFlag = false;
}
invalidate();
}
myhandler.postDelayed(fangAnim, jiange);
}
}
}
代码下载地址:http://download.csdn.net/detail/ahao1985507/8736201