我制作自定义视图。我也做了我的自定义视图(制作圆圈)。但在我的自定义视图中,Alpha动画只是不起作用....
我的自定义视图扩展了linearlayout
public class mylayout extends FrameLayout implements View.OnClickListener {
......
public mylayout(Context context){
super(context);
init(context);
}
void init(Context context){
this.setLayoutParams(new
LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mButton = new Button(getContext());
mButton.setText("Delete all Created Circle.");
mButton.setId(10);
mButton.setOnClickListener(this);
bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.change_sea01);
this.addView(mButton,new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN) {
this.addView(new MyCircleView(getContext(), (int)event.getX(),
(int)event.getY()));
return true;
} else if(event.getAction() == MotionEvent.ACTION_MOVE) {
this.addView(new MyCircleView(getContext(),(int)event.getX(),
(int)event.getY()));
return true;
}
return super.onTouchEvent(event);
}
这是myCircle Class,它在触摸点(x,y)
上形成圆圈class MyCircleView extends ImageView{
.............
public MyCircleView(Context context, int xPos, int yPos) {
super(context);
pnt = new Paint();
pnt.setAntiAlias(true);
bDelete = false;
bmpCircle = BitmapFactory.decodeResource(context.getResources(),
R.drawable.img_click);
this.xPos = xPos;
this.yPos = yPos;
xWidth = bmpCircle.getWidth()/2;
yWidht = bmpCircle.getHeight()/2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(setAnimation == null){
createAnimation(canvas);
}
canvas.drawBitmap(bmpCircle, xPos - xWidth, yPos - yWidht, pnt);
}
public void createAnimation(final Canvas canvas) {
AlphaAnimation alpha;
ScaleAnimation scale;
alpha = new AlphaAnimation(1.0f, 0.0f);
scale = new ScaleAnimation(1, 2, 1, 2, Animation.ABSOLUTE, xPos- xWidth,
Animation.ABSOLUTE, yPos - yWidht);
setAnimation = new AnimationSet(false);
setAnimation.addAnimation(alpha);
setAnimation.addAnimation(scale);
setAnimation.setDuration(3000);
startAnimation(setAnimation);
}
}