自己写的一个渐变view,点击后可缩放

自己写的一个小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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值