public class AddView extends LinearLayout implements View.OnClickListener { private final TextView viewById; private final ImageView btn_jian; private final ImageView btn_add; int max_num=4;//最大值 int min_num=0;//最小值 int deful_num=0;//默认值 /** * 通过默认值与最大值对比 设置默认值 * @param context */ // public AddView(Context context) { // this(context,null); // } // // public AddView(Context context, AttributeSet attrs) { // this(context, null,0); // } public AddView(Context context, AttributeSet attrs) { super(context, attrs); View inflate = View.inflate(context, R.layout.addview, this); viewById = (TextView) inflate.findViewById(R.id.tv); btn_jian = ( ImageView) inflate.findViewById(R.id.btn_jian); btn_add = ( ImageView) inflate.findViewById(R.id.btn_add); int deful_num = getDeful_num(); setDeful_num(deful_num); btn_add.setOnClickListener(this); btn_jian.setOnClickListener(this); } public void onClick(View view){ switch (view.getId()){ case R.id.btn_add: // Toast.makeText(getContext(), "----", Toast.LENGTH_SHORT).show(); addNum(view); break; case R.id.btn_jian: //Toast.makeText(getContext(), "----", Toast.LENGTH_SHORT).show(); addJian(view); break; } } private void addJian(View view) { if(deful_num>min_num){ deful_num--; } setDeful_num(deful_num); if(OnListnerChanged!=null){ OnListnerChanged.changedNum(deful_num); } deleteGoods(view); } private void addNum(View view) { if(deful_num<max_num){ deful_num++; } setDeful_num(deful_num); if(OnListnerChanged!=null){ OnListnerChanged.changedNum(deful_num); } addGoods( view); } public int getMax_num() { return max_num; } public void setMax_num(int max_num) { this.max_num = max_num; } public int getMin_num() { return min_num; } public void setMin_num(int min_num) { this.min_num = min_num; } public int getDeful_num() { String nums = viewById.getText().toString().trim(); if(!TextUtils.isEmpty(nums)){ deful_num= Integer.parseInt(nums); } return deful_num; } public void setDeful_num(int deful_num) { this.deful_num = deful_num; viewById.setText(deful_num+""); } /** * 对外提供接口显示数据 */ public interface OnListnerChanged{ void changedNum(int deful_num); } public void setOnListnerChanged(AddView.OnListnerChanged onListnerChanged) { OnListnerChanged = onListnerChanged; } private OnListnerChanged OnListnerChanged; /** * 创建动画 */ private void deleteGoods( View view) { //点中的商品的数量是否为1件,是1件执行(滚动+透明+平移) if(deful_num<=0){ //1.旋转 RotateAnimation rotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //2.透明 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); //3.平移(y轴不动,x轴由离自己右侧2个宽度的距离,回到初始位置) TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0); //4.动画的集合 AnimationSet animationSet = new AnimationSet(false); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(translateAnimation); animationSet.setDuration(500); btn_jian.startAnimation(animationSet); animationSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { btn_jian.setVisibility(View.GONE); viewById.setVisibility(View.GONE); } @Override public void onAnimationRepeat(Animation animation) { } }); } } /** * 添加时的动画 * */ private void addGoods(View view) { //以下动画,只有在商品数量是1件的时候触发 if (deful_num ==1){ //构建3组动画 //1.旋转 RotateAnimation rotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //2.透明 AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); //3.平移(y轴不动,x轴由离自己右侧2个宽度的距离,回到初始位置) TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0); //4.动画的集合 AnimationSet animationSet = new AnimationSet(false); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(translateAnimation); animationSet.setDuration(500); btn_jian.startAnimation(animationSet); btn_jian.setVisibility(View.VISIBLE); viewById .setVisibility(View.VISIBLE); } //获取此view(点击的加号)在屏幕上的xy坐标的方法 int[] sourceLocation = new int[2]; view.getLocationInWindow(sourceLocation); Log.i("","x轴坐标 sourceLocation[0] = "+sourceLocation[0]); Log.i("","y轴坐标 sourceLocation[1] = "+sourceLocation[1]); //在点击的加号的位置,添加一个用于飞行图片 ImageView imageView = new ImageView(view.getContext()); imageView.setBackgroundResource(R.drawable.address_add_btn_icon); } }
测试
<com.example.shopcar2.AddView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/adds" />